Windows Develop Bookmark and Share   
 index > Windows Forms Designer > ShowUI method crashes when coupled with Form.Close
 

ShowUI method crashes when coupled with Form.Close

I am using VS2005, and I have created a custom control that uses a ControlDesigner to set its properties. The designer displays a SmartTags panel, with some basic properties andwith some links that display custom editor dialogs for more complex setups. One of these editors specifiesa second Size for the control, which will be tied to some event, say MouseDown. (You can guess thatthere's a lot more to it than this, but this is the gist of it.)

Obviously, I want a user to be able to modify this second size on the design surface, rather than just punching numbers into a PropertyGrid, so he can see what it will look like in relation to the other components. The floating editor allows me to do just that. In mySmartTags panel'sActionItem method, I create an instance of the editor, passing it a reference to the designer,and call its Show method. The editor appears and the SmartTags list hides. The user uses the handles to edit the control size and hits OK. The editor calls back to the designer to save the current control size as the second size, set the control back to its size prior to calling the editor, and re-display the SmartTags panel -designer.MouseDownPropertiesComplete().

That last part is the kicker, though. The SmartTags panel does not re-appear. Instead, I get a gray rectangle which is the size of the panel, but does not contain any text or controls, and the designer display locks up. The problem seems to be that the call to ShowUI is coming as the editor form is closed. I have tried putting the call to MouseDownPropertiesComplete in the OnClosing override for the editor, and I have tried it in the event handler for the OK button, which also has a call to the editor's Close method. Either way, it crashes.

It does not crash if I instead call the editor's Hide method. In that case, the editor hides and the SmartTags panel appears properly. However, I now have an instance of that editor floating around in memory somewhere, don't I? The last thing I need is design-time memory leaks!

Does anyone know why this is happening, and how I can get around it? I can post some code if you think it would help, but the error is easily reproducible if you create a basic control & designer pair, and an "editor" form with a single button that calls the designer to ShowUI and then calls its form's Close method.


Thanks,
Doug
Douglas Hauck  Monday, August 17, 2009 5:54 PM
Ok, update: I found a way around this - in the designer method that calls ShowUI, I first make a call to Control.Focus. Then the editor can close ok, and the SmartTags panel displays properly.

However, this leads to another question: when the designer calls Control.Focus, the control's LostFocus event fires. I know because I attached a handler to this event in my editor form:

designer.Control.LostFocus +=

new EventHandler(Control_LostFocus);

The code in the Control_LostFocus handler also closes the form- it is meant to cancel any editsin case the user clicks on another control while the editor is open, to prevent confusion. It has never fired until now - I assumed that was because the designer, not the actual control, had the focus. But now, the event triggers when the control gets focus. Yet I double-checked, and the handler is definitely tied to the control's LostFocus, not anything of the designers.

Although this is technically the answer to this thread's question, I am not going to mark it as such right away. I would first like to see if anyone has any thoughts on the focus behavior. If I don't get any responses, I'll come back in a couple days to mark this as the answer.

Thanks,
Doug

Douglas Hauck  Monday, August 17, 2009 6:52 PM

You can use google to search for other answers

Custom Search

More Threads

• Unspecified error - in Form Designer of Visual C# 2008 Express
• Anchor controls on Inherited forms
• Desginer TypeLoad Failure
• Ambiguous match found
• Designer wont save property on overridden datagridviewcolumn
• Brief introduction to designer architecture and how to handle drag&drop events [semi-solved]
• Bug, anchored control on inherited form, System.Windows.Forms
• How to Display a dialog box for a property?
• How to change the properties of a control at run time
• Combining already programmed projects to one big'n