Don't have bugs in usercontrols.
So once again the curse of non trivial toy examples strikes. After spending about an hour trying to place a custom usercontrol onto a simple windows form in Visual Studio and C# I give up. In the most trivial example that I came up with it works fine, but as soon as I try to do this in my own little project (the niftysolution plugin) visual studio does nothing but crash every time I try to drag my wizard generated empty user control onto the canvas of my slightly modified form. Every time. I'm getting desperate as to why the heck it doesn't work, at all. And of course there is no callstack, no message, just a dump of the registers and a dialog asking me if I want to send this to Microsoft. As if anyone actually reads these crashdumps...
I just want to place a control in a dialog! Oh, and get this, the whole reason why I'm doing this is due to the fact that stuffing a whole lot of strings into a standard listcontrol apparently while working fine on windows XP 32 bit, crashes under Windows XP 64 bit. So my niftysolution fast open dialog isn't working anymore, as it sometimes crashes visual studio under XP 64. So I have to circumvent the standard controls and write my own, but of course that crashes visual studio while I'm designing it. The irony is too much for me. Anyhow, after a little headscratching I realized that I had a bug in my rendering code, which was running in design mode of course. After putting the following at the very top of my draw routine it all worked, Visual Studio stopped crashing and rays of light shone down from the heavens.
private void SearchResultControl_Paint(object sender, PaintEventArgs e) { if(DesignMode == true) return; // lots of code that crashes }
So it would have been less frustrating if Microsoft would have put all sorts of safeguards in place while running code in designer mode. It's a known fact that programmers mess up while developing. A lot. And making their editor is just an attempt to make grown men cry.
Enabling Design Mode for an User Control
On another note, it's apparently blindingly obvious on how to get the custom control to actually behave in the designer. It's so obvious that nobody actually talks about it. So for us C# noobs out there, here are the steps:
1. Download the example project.
2. Here you will find a Form and a UserControl (MyControl) in the same project.
3. Compile it once so that you will have an .exe on disk.
4. Right click on the Toolbox and choose "Choose Items". Click on the browse button and look for your .exe and select it.
5. You should now have a new item in the toolbox that says "MyControl". You can drag it to the form now and do all the normal stuff with it. That is unless visual studio decides to crash on you.
In closing
That is basically it, hopefully one can place all sorts of nice things in the form for now. Ok, next time I need to remember to check back on this page on how to do it. Hope it helped someone out there as well.