EricL (3) [Avatar] Offline
#1
Hi,
I am trying the examples from your book, using Visual Studio 2005 Beta 2.

I replaced the MainMenu you're using in Chapter 3 by a MenuStrip object containing ToolStripMenuItem objects: but these do not have any Index property.

Is there a way to convert your menuImage_ChildClick handler exposed on page 92 of your book so that it uses ToolStripMenuItem objects instead of MenuItem objects?

Eric
eebrown (89) [Avatar] Offline
#2
Re: ToolStripMenuItem vs MenuItem
Hey Eric,

Yes, all very different in 2005. Working on this as part of a new edition, hopefully out the end of this year. See the post from John on March 24 on a similar issue. His message and my response are probably helpful.

Let me know after you look at this other post if you still have problems.

Erik
EricL (3) [Avatar] Offline
#3
Re: ToolStripMenuItem vs MenuItem
Hi Erik,

Thank you very much for your fast answer.

I have read your answers to John's topic on March 24 and it has helped me a lot.

I am an absolute beginner to C#, but thanks to your help I came up with (my naive first attempts are commented):

private int menuImage_ChildIndex(object sender)
{
//if (sender == (object)menuStretch)
//{ return 0; }
//else if (sender == (object)menuActual)
//{ return 1; }
//else if (sender == (object)menuCenter)
//{ return 2; }
//else
//{ return -1; }
return this.menuImage.DropDownItems.IndexOf((ToolStripItem)sender);
}

private void menuImage_ChildClick(object sender, EventArgs e)
{
//if (sender == (object)menuStretch)
//{
// pbxPhoto.SizeMode = PictureBoxSizeMode.StretchImage;
//}
//else if (sender == (object)menuActual)
//{
// pbxPhoto.SizeMode = PictureBoxSizeMode.Normal;
//}
//else if (sender == (object)menuCenter)
//{
// pbxPhoto.SizeMode = PictureBoxSizeMode.CenterImage;
//}
if (sender is ToolStripMenuItem)
{
ToolStripMenuItem mi = (ToolStripMenuItem)sender;
_selectedImageMode = menuImage_ChildIndex(sender);
pbxPhoto.SizeMode = modeMenuArray[_selectedImageMode];
pbxPhoto.Invalidate();
}
}

private void menuImage_DropDownOpening(object sender, EventArgs e)
{
if (sender is ToolStripMenuItem)
{
bool bImageLoaded = (pbxPhoto.Image != null);
foreach (ToolStripMenuItem mi in ((ToolStripMenuItem)sender).DropDownItems)
{
mi.Enabled = bImageLoaded;
mi.Checked = (this._selectedImageMode == this.menuImage_ChildIndex(mi));
}
}
}

private void DefineContextMenu()
{
this.menuView.DropDown = ctxtMenuView;
}

and the application is now working fine with Studio 2005
(without having required much change from your book code except the menu cloning which is great in Studio 2005).

Thanks again.

Eric
EricL (3) [Avatar] Offline
#4
Re: ToolStripMenuItem vs MenuItem
Erik,

Talking about menu cloning, it would have seemed more natural to me to assign the View menu to the Context menu rather than the opposite, since regular menus are generally built before thinking about adding context menus (and as a matter of fact, this is the order you're using in your book).

Is this feasable?

Eric
eebrown (89) [Avatar] Offline
#5
Re: ToolStripMenuItem vs MenuItem
Yes, you can assign menus either way. Microsoft recommends building the context menu first, and assigning it as I proposed. I agree that this seems a bit backwards. Logically it works okay in code, but realistically it's not the order you typically do this in.

That said, I could imagine building an app with a menu, and later deciding to move it into a context menu. This could then be hooked up to the original menu. So it works either way.

Enjoy,

Erik