-1

I use visual studio 2019 community edition and my program is a big winform application. When I try to open the winforms to edit them, I always get the error:

Error creating window handle.

bei System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
bei System.Windows.Forms.Control.CreateHandle()
bei System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
bei System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
bei System.Windows.Forms.Control.CreateControl()
bei System.Windows.Forms.Control.ControlCollection.Add(Control value)
bei System.Windows.Forms.Form.ControlCollection.Add(Control value)
bei System.Windows.Forms.Design.ControlDesigner.DesignerControlCollection.Add(Control c)  

I don't know how to fix it and I can't figure out what causes this error. In my forms there is one panel which gets injected in another panel.

Edit: I can build and start the application without errors but only the designer in visual studio is not working.

Edit2: I can open all windows which has no panel in it, if they have one I get the error.

Edit3: Sometimes the designer write that all of the controls and variables was not declared or was not assigned.

  • Try: Close all edit windows. Clear Project, Build Project, Run Project, close running instance. Then try opening an editor again. Post errors along the way here. – Fildor Mar 11 '20 at 09:42
  • @Fildor I have done this now and I have now the same error like I posted over there. – tester123hdhd Mar 11 '20 at 09:44
  • You need to use an instance of the form. See my two form project : https://stackoverflow.com/questions/34975508/reach-control-from-another-page-asp-net You need to use something like this.Control.CreateHandle(); – jdweng Mar 11 '20 at 09:50
  • @jdweng I already tried this, but it is not working. Next problem, I inject a panel in another the main panel. (it depends which form I inject in another one when I click a button) – tester123hdhd Mar 11 '20 at 09:54
  • Explain better. When accessing a control you must use the instance and not the type (System.Windows.Forms). What errors are you getting? If you can access then the property in the designer.cs file is private and you can change private to public. – jdweng Mar 11 '20 at 10:05
  • Basically I have one panel where all other panels gets loaded in. When I try to open forms without an panel it is working completely fine. When I try to open the main panel (where all other panels gets loaded in) it is working fine to. But if I open an form with an panel in it (which gets loaded in the main panel) it throws me this error. – tester123hdhd Mar 11 '20 at 10:07
  • @jdweng I can't access to the file to set the property to public. – tester123hdhd Mar 11 '20 at 10:32
  • The file is solution explorer under the form name. – jdweng Mar 11 '20 at 10:34
  • @jdweng oh you mean in the projectmap explorer, when I go in it and go to the properties, I can't set anything to public: look at the screen here: https://prnt.sc/reoo0y – tester123hdhd Mar 11 '20 at 10:49
  • No. In the VS menu View : Solution Explorer. – jdweng Mar 11 '20 at 11:00
  • @jdweng that is the solution explorer, like I said I can't set there anything to public because this properties don't gets loaded. – tester123hdhd Mar 11 '20 at 11:09
  • Then you need to pass the property (instance of the object) between forms in a parameter list of a method. The variable/property still need to be public. Looks like you are dynamically creating object on the form. – jdweng Mar 11 '20 at 11:14
  • This error can technically be caused by another set of processes that run in the same desktop session. There is a maximum of 65535 windows per session. So logging out and logging back in might clear the error condition. Otherwise you'd have to look for a problem in your code that runs at design-time that recursively creates new controls. That code will bomb on this error before it can crash VS with a stack overflow. – Hans Passant Mar 11 '20 at 11:15
  • @HansPassant No it is not because of that, I can open an older version from me too, but this not. – tester123hdhd Mar 11 '20 at 11:57

2 Answers2

0

I suggest moving all of the form's run time code into a separate method that executes via this.Load and is blocked at design time using a boolean property...

this.Load += delegate {
    if(!THISISLOADED) return; //Exits -> will always be false in Design Time  
    OnLoad();
};

private void OnLoad()
{
   //place all run time code here - will not execute at design time 
   //...
   THISISLOADED = true; //just in case 
}


private bool m_THISISLOADED = false; //false at design time
public bool THISISLOADED {
    get { return m_THISISLOADED; }
    set {
        m_THISISLOADED = value;
    }
}

Then just before showing the form, set the boolean property THISISLOADED to true...

myWinForm.THISISLOADED = true;
myWinForm.ShowDialog();

EDIT: Just found this Detecting design mode from a Control's constructor

You can test for Design Mode using LicenseManager...

this.Load += delegate {
    if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) return;
    OnLoad();
};
  • You don't understand my question. It is working when I build the application, it is not working when I open the designer in visual studio. Your code is just for the application itself for building. – tester123hdhd Mar 12 '20 at 07:46
  • Yes, I understand that it works in run-time but not in Designer. It's exactly for that reason I'm suggesting you block the "offending code" from executing when you open Designer. Another option is to first comment out all the run-time code, check that Designer opens (it should), and then reintroduce the code line by line until you identify the code that Designer is unhappy with. – KryptoKnight Mar 12 '20 at 08:20
  • I thought that too but it is on 3 forms from my application and they all have a designer with 10k lines+. – tester123hdhd Mar 12 '20 at 08:22
  • You need to narrow down the problem. Are all of the forms "broken" or do some open fine in Designer? – KryptoKnight Mar 12 '20 at 08:41
0

I found now the error. I had contains code which prevents flickering. This code got executed before the window handle was created and thats why this error caused.