Converting from VS2003 to VS2005

  • Thread starter Thread starter Henry
  • Start date Start date
H

Henry

I am trying to convert code that worked in a Visual Studio 2003 project to
code that will work in a Vsual Studio 2005 project
and need help.

The following is the VS2003 code:
private void mnuColorItem_Click(

object sender, System.EventArgs e)

{

MenuItem mnuItem = (MenuItem)sender;

//Get the color name from the menu item text

//while removing the & character in that text

rbText.SelectionColor = Color.FromName(

mnuItem.Text.Replace("&", ""));

//Uncheck all menu items inside the color menu

foreach (MenuItem m in mnuItem.Parent.MenuItems)

m.Checked = false;

mnuItem.Checked = true;

}
In VS 2005 this doesn't work because the menu is now a ToolStrip and
MenuItem is now a ToolStripMenuItem...
But when I change that then I can't access the parent because it is
protected. I am not sure what I have to change to get at that data....
That is what I need help with right now.
 
Henry,

You can use in version 2005 the same menus as in version 2003, there is no
need for the language to use toolstrips, which are of course other classes
with their own methods.

Cor
 
I might be able to do that, but it would mean rebuilding the entire menu
system.
I am hoping that the solution is simpler than that.

It appears to be an access issue. Here is the error message:

'System.Windows.Forms.ToolStripItem.Parent' is inaccessible due to its
protection level E:\Projects\70-316\Chapter2\StepByStep2_18.cs 74 44
Chapter2

I have tried to address this by changing the access modifier for the parent
control(s) of the specific menu items to public from private. I am still
getting the error message though.
 
I found the answer! See the code below:

It appears that the parent object is revealed by a method rather than a
property.

So the code I needed after changing to ToolStripMenuItem types was

"mnuItem.GetCurrentParent().Items"
private void mnuColorItem_Click(

object sender, System.EventArgs e)

{

ToolStripMenuItem mnuItem = (ToolStripMenuItem)sender;

//Get the color name from the menu item text

//while removing the & character in that text

rbText.SelectionColor = Color.FromName(

mnuItem.Text.Replace("&", ""));

//Uncheck all menu items inside the color menu

foreach (ToolStripMenuItem m in

mnuItem.GetCurrentParent().Items)

m.Checked = false;

mnuItem.Checked = true;

}
 
Back
Top