• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 615
  • Last Modified:

Accessing MDI parent menu from MDI child

I have a menu on a MDI parent form. From the child form, I would like to set one of the menu items to checked. How can I do this?
0
lmred
Asked:
lmred
  • 14
  • 14
1 Solution
 
pauljk1619Commented:
'0 is the index of your menuItem.

this.MdiParent.Menu.MenuItems[0].Checked= true
0
 
lmredAuthor Commented:
Wow, that was a fast response!!! Ok, clear me up something. I don't know how to find the index. Say my menu is the 7th one. And say I want to check the 2nd menu item of that menu. What would the index be?
0
 
lmredAuthor Commented:
I got the following error message when I put 6 in place of 0.

System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
 
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
pauljk1619Commented:
on the error...  

did you set the MdiParent?  

I'm assuming you are creating the child from from the main container.  If so....


yourForm.MdiParent = this;
yourForm.Show();
0
 
lmredAuthor Commented:
I think the line you gave me needs to be casted. But this is what I have:


On the main form:

 private void recorderControlToolStripMenuItem_Click(object sender, EventArgs e)
        {
            recorderControlToolStripMenuItem.Visible = recorderControlToolStripMenuItem.Checked;
            frmRecorderControl f = new frmRecorderControl();
            f.MdiParent = this;
            f.Show();
        }

On the child form:

private void button8_Click(object sender, EventArgs e)
        {
            this.MdiParent.Menu.MenuItems[1].Checked = true;
        }
0
 
pauljk1619Commented:
It's working for me...    Are you getting a different error now?
0
 
lmredAuthor Commented:
Its the same error.
0
 
pauljk1619Commented:
Which line are you getting the error on....  
This one?

this.MdiParent.Menu.MenuItems[1].Checked = true;

Put a break point on the line and before it fires, mouseover each part to make sure it is assigned an object.

Are you using the MainMenu object and it's assigned to the Menu property of your parent form?
0
 
lmredAuthor Commented:
this.MdiParent.Menu is returning null.
0
 
lmredAuthor Commented:
The View menu is the 3rd menu. I am selecting one of the menu items under that. When I select that, it opens the child form.
0
 
lmredAuthor Commented:
I don't understand this question:

Are you using the MainMenu object and it's assigned to the Menu property of your parent form?
0
 
pauljk1619Commented:
yes... the last question might be the problem.  In the properties for you main form, what is in the Menu property?
0
 
lmredAuthor Commented:
I have a mainmenustrip property. I don't have a menu property. The mainmenustrip is set to menuStrip.
0
 
pauljk1619Commented:
Oh... this is 2005?

Try this instead...

this.MdiParent.ToolStripMenuItem[1].Checked = true;



0
 
pauljk1619Commented:
wait...
except use the correct case for mainmenustrip

this.MdiParent.mainmenustrip.ToolStripMenuItem[1].Checked = true;
0
 
lmredAuthor Commented:
We're getting close, but ToolStripMenuItem does not show up in the Intellisense.
0
 
lmredAuthor Commented:
Yeah this is 2005. Sorry, I should've mentioned that.
0
 
pauljk1619Commented:
So you probably have an Items in the list...

this.MdiParent.MainMenuStrip.Items["File"].Checked = true;
0
 
pauljk1619Commented:
Use your name instead of File
0
 
lmredAuthor Commented:
It does not allow the .Checked. I get an error:

ToolstripItem does not contain a definition for 'Checked'.

Why does MS make something that should be simple, be so hard??!!!!!
0
 
pauljk1619Commented:
Adding too much functionality makes everything more complicated....

I wish I had my 2005 installed here instead of at home...   Try casting it into a ToolStripmenuItem which has the Checked property.

((ToolStripMenuItem) this.MdiParent.MainMenuStrip.Items["File"]).Checked = true;
0
 
lmredAuthor Commented:
pauljk1619 you have really been great! I'm new at C#. Soon as I start learning 2003, we get 2005.

I did the following:

((ToolStripMenuItem)this.MdiParent.MainMenuStrip.Items["TransportMenuItem"]).Checked = true;

No error occurred. However the menu wasn't checked. And that is really the top most menu. I want one of the options under that menu checked.
0
 
pauljk1619Commented:
Why don't you try using the name of the Option as opposed to the topmost value.  
0
 
lmredAuthor Commented:
Because I get the error from above when I do.
0
 
pauljk1619Commented:
It's a pain in the @ss way to do it, but try this....

The first index is your top level.  The secondIndex are the items below the top level.

((ToolStripMenuItem)((ToolStripMenuItem)this.MainMenuStrip.Items[0]).DropDownItems[0]).Checked = true;
0
 
pauljk1619Commented:
Just realized I did it from my main form...   you would use....

((ToolStripMenuItem)((ToolStripMenuItem)this.MdiParent.MainMenuStrip.Items[0]).DropDownItems[0]).Checked = true;
0
 
lmredAuthor Commented:
Hallelujah!!!!!!!!!!!!!!!!! It worked! Thank you so much pauljk1619!!!!!! You stuck right with me.


Lmred
0
 
pauljk1619Commented:
Thank Goodness!!!    I couldn't leave you hanging!     Good luck!

Paul
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 14
  • 14
Tackle projects and never again get stuck behind a technical roadblock.
Join Now