|
Hi all,
My application is using MDI forms structure. So the menu items in pull down menu of the main form are merged by main form itself and several mdi forms.
So you can imagine that the "About" menu in the main form will be push inside (shift left) because of new menu items was come from new MDI forms.
So how can I let the "About" menu keeping on the right most of the menu? Furture more I want to exactly control the ordering of all the menu items.
Thank you very much.
Best regards, Diviner. | | Diviner Chan Friday, December 19, 2008 10:41 PM | Hi Diviner Chan, It seems that you are setting the MergeIndex of the MenuStrip
of the main form, actually, we need to set the MenuStrip of the child
form instance. Besides, it seems you haven't set the MergeAction in
your code. To set the MenuStrip of the childForm, we need to
define a public property to return the MenuStrip of the childForm for
the MenuStrip field is private by default. Suppose Form2 is the child
form in my application. Here is the code. | publicpartialclassForm2:Form | {
| | publicForm2() | {
| | InitializeComponent(); | | } | | ///<summary> | | ///letotherclassescanaccess the MenuStripinthisform | | ///</summary> | | publicMenuStripChildFormMenuStrip | | { | | get | | { | | returnthis.menuStrip1; | | } | | } | | } |
In the main form, we can write the following code to set the MergeAction and MergeIndex of the child form instance. | publicForm1() | | { | | InitializeComponent(); | | showMDIChildForm(); | | | } | | privatevoidshowMDIChildForm() | | { | | Form2f=newForm2(); | | f.MdiParent=this; | | f.Show(); | | intindex=this.menuStrip1.Items.Count; | | foreach(ToolStripMenuItemchildIteminf.ChildFormMenuStrip.Items) | | { | | index++; | | childItem.MergeAction=MergeAction.Insert; | | childItem.MergeIndex=index; | | } | | } |
Note
that there is a MenuStrip control in both Main form and child form
which I dragged from the designer, so you may not see any
initialization code in Form1 and Form2 class. Best regards, Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't. - Marked As Answer byDiviner Chan Tuesday, December 23, 2008 10:49 PM
-
| | Bruce.Zhou Tuesday, December 23, 2008 2:19 AM | | | BinaryCoder Saturday, December 20, 2008 1:47 PM | BinaryCoder,
I have read the article. But still don't know how to shift "About" back to the right most position in menu. Would you please write a snippet to me?
Thank you very much.
Best regards, Diviner. | | Diviner Chan Saturday, December 20, 2008 8:18 PM | Hi Diviner Chan, If we use Visual Studio designer to achieve this goal, it would be much easier. See the following steps. - Select the ToolStripMenuItem you want to merge in the ChildForm.
- Set the ToolStripsMenuItem's MergeAction property to Insert in the PropertyGrid. By default, the value is "append".
- Set the ToolStiprsMenuItem's MergeIndex property to the location you want in the MDI parent form. (Here if the "About" MenuStripItem is the fifth one the main form, and then you can set the MergeIndex to 5).
Of course we can try to achive this by coding. The following C sharp code are for your reference. | publicChildForm() | | { | | InitializeComponent(); | | childMenuToolStripMenuItem.MergeAction=MergeAction.Insert; | | childMenuStripMenuItem.MergeIndex= 5; | | } | |
If you have further problems, please let me know Best regards, Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't. | | Bruce.Zhou Monday, December 22, 2008 9:30 AM | Bruce Zhou,
Thank you very much.
In my case, the "About" menu item is belong to Main Form which is resided on right most. After I add a new MDI form to Main Form which has some menu items, the "About" menu of the Main Form is not the right most item anymore, because some menu item of MDI form has been placed next to the "About" menu.
So I try to do something on Main Form class. The code snippet like this: public partial class FmMain : Form { .... private void ShowMdiForm(Form form) { form.MdiParent = this; form.WindowState = FormWindowState.Maximized; form.Show();
// Put the "About" menu to right most. foreach (ToolStripItem item in menuStrip1.Items) { if (item.Text == "About") { item.MergeIndex = menuStrip1.Items.Count; // Here, but no effect... } } } }
Any tips for me? Thank you very much.
Best regards, Diviner.
| | Diviner Chan Monday, December 22, 2008 5:26 PM | Hi Diviner Chan, It seems that you are setting the MergeIndex of the MenuStrip
of the main form, actually, we need to set the MenuStrip of the child
form instance. Besides, it seems you haven't set the MergeAction in
your code. To set the MenuStrip of the childForm, we need to
define a public property to return the MenuStrip of the childForm for
the MenuStrip field is private by default. Suppose Form2 is the child
form in my application. Here is the code. | publicpartialclassForm2:Form | {
| | publicForm2() | {
| | InitializeComponent(); | | } | | ///<summary> | | ///letotherclassescanaccess the MenuStripinthisform | | ///</summary> | | publicMenuStripChildFormMenuStrip | | { | | get | | { | | returnthis.menuStrip1; | | } | | } | | } |
In the main form, we can write the following code to set the MergeAction and MergeIndex of the child form instance. | publicForm1() | | { | | InitializeComponent(); | | showMDIChildForm(); | | | } | | privatevoidshowMDIChildForm() | | { | | Form2f=newForm2(); | | f.MdiParent=this; | | f.Show(); | | intindex=this.menuStrip1.Items.Count; | | foreach(ToolStripMenuItemchildIteminf.ChildFormMenuStrip.Items) | | { | | index++; | | childItem.MergeAction=MergeAction.Insert; | | childItem.MergeIndex=index; | | } | | } |
Note
that there is a MenuStrip control in both Main form and child form
which I dragged from the designer, so you may not see any
initialization code in Form1 and Form2 class. Best regards, Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't. - Marked As Answer byDiviner Chan Tuesday, December 23, 2008 10:49 PM
-
| | Bruce.Zhou Tuesday, December 23, 2008 2:19 AM | Bruce,
Yes. It is work for me. Thank you very much.
Best regards, Diviner. | | Diviner Chan Tuesday, December 23, 2008 10:49 PM |
|