C# compile error: “Invoke or BeginInvoke cannot be called on a control until the window handle has been created.”

前端 未结 9 904
时光取名叫无心
时光取名叫无心 2020-12-16 23:16

I just posted a question about how to get a delegate to update a textbox on another form. Just when I thought I had the answer using Invoke...this happens. Here is my code:<

9条回答
  •  误落风尘
    2020-12-17 00:12

    When you get this error, it almost always means that you've attempted to act on a control or form before it was actually created.

    In WinForms, GUI elements have two semi-independent lives: as classes in memory and as entities in the operating system. As such, it's possible to reference a control in .net that hasn't actually been created yet. The "handle being created" refers to having a number assigned to the control by the OS to allow programs to manipulate its properties.

    In this case, most errors can be eliminated by setting a flag at the end of the form's load event and only attempting to manipulate the form's controls after that flag has been set.

提交回复
热议问题