Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Children of a control incorrectly respond to the mouse popup menu interaction during design time.
 

Children of a control incorrectly respond to the mouse popup menu interaction during design time.

I have a control that has a panel displaying bunch of labels. Each label also has a popup menu attached:
label.ContextMenu = new ContextMenu();
label.ContextMenu.Popup += new EventHandler(delegate
{
// popup menu created
});

If I display the control in the designer, the menu for these labels is suppressed by the designer, that is correct. However, if I open my custom Editor and recreate the labels in the design time control, then popups for the labels start working on right mouse click in the designer, that is incorrect.
Do you know why this would happen and how to suppress these custom popup menus in the design time? I think the key to this problem is that labels are not placed on the control directly, but placed on the child panel instead.

I have a test project to duplicate the problem. I can send you the test program if you need it!
ilya_goberman  Tuesday, May 26, 2009 10:22 PM

Hi ilya_goberman,

Based on your description, I think you can use the DesignMode property of the control to achieve your goal. Below is my code:
if(!DesignMode)
{
// the code belowwill notexecute in design mode.
label.ContextMenu = new ContextMenu();
label.ContextMenu.Popup += new EventHandler(delegate
{
// popup menu created
});
}

Let me know if this helps,

Best regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't.
Aland Li  Thursday, May 28, 2009 7:44 AM

Aland,
I have isolated the problem to a small test I can send to you in case you do not understand the problem. The workaround above is not acceptable. I have a lot of controls I need to recreate during the design time and cannot write custom code above for all of them (some of them are 3rd party controls). Also it does not work for the following reason: the control does not behave correctly in the User Control designer - if I click on it and focus is on the Form, the focus is not set to the User Control, which is incorrect.
Again the problem is related to creating child of a child of the User Control in the design time. I have wasted couple days on it I would rather spend on something else. Is there some way to "register" newly created control in the designer? Is there another way to get prompt expert support besides these forums?

ilya_goberman  Thursday, May 28, 2009 2:10 PM

Hi MethodMas,

Based on your description, you wanted to create a new user control by combining some third party controls and met lots of errors.

I cannot reproduce the problem because I don’t know what third party controls you used. I just used some labels to test and they work well. Could you provide me with the test program you mentioned above.

I’m sorry form the late replying. If I have misunderstood you, please feel free to tell me. Thanks.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, June 03, 2009 10:03 AM
Aland, how can I attach a test program? I do not see a way to do it.
It does not matter what user controls I use. The point is as soon as I recreate them in the designer, they stop behaving as "design" time controls and behave as if they were running in the applicaton.
I am ready to send you very detailed steps of reproducing the problem along with source code of the test program.
ilya_goberman  Sunday, June 21, 2009 8:22 PM

Hi ilya_goberman,

Thanks for replying. You can move the core code to a small project and make sure the error can be reproduced in the project. Please send me a email with the project source as an attachment.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, June 22, 2009 3:52 AM
When you say "send me a email", what e-mail address should I be using?
ilya_goberman  Monday, June 22, 2009 8:30 PM

Hi ilya_goberman,

This my email: alala666888@hotmail.com or v-shunli@microsoft.com .

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Tuesday, June 23, 2009 1:38 AM
Aland,
I have sent you DesignerBug.zip to alala666888@hotmail.com.
These are the steps to reproduce the problem:
1) Unip DesignerBug.zip
2) Open DesignerBug\DesignerTest\DesignerTest.sln in VS
3) Open Form1 in the designer. You will see 4 lables with borders. Right click on one of the labels. You will see standard VS designer menu with "Properties" menu item.
4) Now open Properties for the TestControl and click on the "DesignerProperty" value designer "...". You will see a new label added to the design view after every click
5) Now right click on one of the labels and you will see that Desinger Menu is gone. It is replaced with run time popup menu that is inclorrect.
Please let me know if you have questions
ilya_goberman  Tuesday, June 23, 2009 1:37 PM

Hi ilya_goberman,

I haven’t received your email yet. Could you please send it to v-shunli@microsoft.com. Please rename the zip file, such as aa.zip.file. The attachment might be blocked if it is a zip file.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, June 24, 2009 1:48 AM
done
ilya_goberman  Wednesday, June 24, 2009 2:24 AM

Hi ilya_goberman,

I received your email. I will work on it these days. Thanks.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, June 24, 2009 2:27 AM

Hi ilya_goberman,

The key problem is still related to design mode. When you design the main control, you need to send its design mode to the sub control, the sub control is not in design mode. This is the core code snippet:


1.In the set function of the property(DesignerProperty) of the main control(TestControl):
//Send design mode to free form control.

freeFormControl.SetupField(this, new Rectangle(10, 30 * i, 100, 16),DesignMode);


2.In the method(SetField) of the sub control(FreeFormControl):
//Check if the main control is in design mode. If so, don't add context menu.

if (!isDesign)

{

label.ContextMenu = new ContextMenu();

label.ContextMenu.Popup += new EventHandler(delegate

{

label.ContextMenu.MenuItems.Add(new MenuItem("test1"));

label.ContextMenu.MenuItems.Add(new MenuItem("test2"));

label.ContextMenu.MenuItems.Add(new MenuItem("test3"));

label.ContextMenu.MenuItems.Add(new MenuItem("test4"));

});

}

I would send you a email with the modified project as an attachment.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, June 24, 2009 3:41 AM
Aland,
As I mentined before this workaround is not acceptable for me.
What if it is not a labelbut a 3rd party control? I may not be ablego toa control I have no code and disable its functionality.
Also, even this workaround still has problems: follow steps 1)-5) above and then click on the Form1. You see it get selected in the designer. Now click on any "label". It is supposed to select parent control testControl1, but it does not (Form1 is still selected). The reason is that it still behaves like a run-time control.

Are you positive it is the only workaround available? I asked this question before, but is there any other way to get support for WinForms related questions besides these forums, including a paid support?
Thanks
ilya_goberman  Wednesday, June 24, 2009 1:47 PM

Hi ilya_goberman,

From my experience, you have to rebuild the project when you set the DesignerProperty property. It is because the visual studio designer has a cache to store the states of the control. If you do not rebuild, some data might not be refreshed.

I think you can develop your own control designer to control the design behavior of the control.

Regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, June 25, 2009 5:25 AM

You can use google to search for other answers

Custom Search

More Threads

• Import 16x16 bmp & use as an icon for forms
• Basic Question on Properties returning a class
• Ressources embedded problem with a compilation on the fly
• EnableDesignMode with Inherited UserControl
• Setting selected state of properties in propertygrid...
• Highlight a column in tableLayoutPanel after a button click
• Can't get inheritance or association to work in class designer (Visual Studio 2005 Standard Academic)
• Unspecified error - in Form Designer of Visual C# 2008 Express
• Windows Forms Designer
• Controls Resize Problem