Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / July 2004

Tip: Looking for answers? Try searching our database.

I'm stuck on this (menuitems)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
M - 22 Jul 2004 22:07 GMT
<see code snippet below>

I'm trying to determine how I can tell when a user clicks
A->1->a     OR
A->3->b     OR
D->4->c  ...

While in the handler I cannot determine what sequence of menu items and sub
menu items brought me there.

If anyone has any ideas or can show me in code what I should do I would
highly appreciate it.

Thanks

public class MyPopUpSubSubMenu : ContextMenu
{
public MyPopUpSubSubMenu()
{

 MenuItems.Add(new MenuItem("a"));
 MenuItems.Add(new MenuItem("b"));
 MenuItems.Add(new MenuItem("c"));
 MenuItems.Add(new MenuItem("d"));
}
};

public class MyPopUpSubMenu : ContextMenu
{
public MyPopUpSubMenu()
{
   MenuItems.Add(new MenuItem("1"));
   MenuItems.Add(new MenuItem("2"));
   MenuItems.Add(new MenuItem("3"));
   MenuItems.Add(new MenuItem("4"));

   m_SubSubMenu = new MyPopUpSubSubMenu();

   MenuItems[0].MergeMenu(m_SubSubMenu);
   MenuItems[1].MergeMenu(m_SubSubMenu);
   MenuItems[2].MergeMenu(m_SubSubMenu);
   MenuItems[3].MergeMenu(m_SubSubMenu);
}

   public MyPopUpSubSubMenu m_SubSubMenu;

};

public class MyPopUpMenu : ContextMenu
{
   public MyPopUpMenu()
   {
       MenuItems.Add(new MenuItem("A"));
       MenuItems.Add(new MenuItem("B"));
       MenuItems.Add(new MenuItem("C"));
       MenuItems.Add(new MenuItem("D"));

       m_SubMenu = new MyPopUpSubMenu();

       MenuItems[0].MergeMenu(m_SubMenu);
       MenuItems[1].MergeMenu(m_SubMenu);
       MenuItems[2].MergeMenu(m_SubMenu);
       MenuItems[3].MergeMenu(m_SubMenu);
}

public MyPopUpSubMenu m_SubMenu;

};

public class Form1 : System.Windows.Forms.Form
{
public MyPopUpMenu myPopUpMenu;

private void Form1_Load(object sender, System.EventArgs e)
{

    myPopUpMenu = new MyPopUpMenu();
    //myPopUpMenu.MenuItems[0].MenuItems[0].Popup += new
EventHandler(this.WhoseCallingMe);

    myPopUpMenu.MenuItems[0].MenuItems[0].MenuItems[0].Click += new
EventHandler(this.WhoseCallingMe);
    myPopUpMenu.MenuItems[0].MenuItems[0].MenuItems[1].Click += new
EventHandler(this.WhoseCallingMe);
    myPopUpMenu.MenuItems[0].MenuItems[0].MenuItems[2].Click += new
EventHandler(this.WhoseCallingMe);
    myPopUpMenu.MenuItems[0].MenuItems[0].MenuItems[3].Click += new
EventHandler(this.WhoseCallingMe);

    this.ContextMenu = myPopUpMenu;
}

public void WhoseCallingMe(object sender, System.EventArgs e)
{
 MessageBox.Show("How can I tell who called me?");
}
}
AlexS - 22 Jul 2004 23:52 GMT
M,

you can test sender reference, which you get in handler as parameter, for
which menu item invoked the handler. Just cast it to MenuItem and you can
access all properties and methods. You can use Text, Mnemonic or Shortcut at
least.

HTH
Alex

> <see code snippet below>
>
[quoted text clipped - 93 lines]
>  }
> }
M - 23 Jul 2004 00:24 GMT
Thanks for answering.

I know I can cast it to the menuitem and get the text but I can only get its
text (a, b or c) but I cannot get its parents text.

I'm guessing that I should add an event handler to the second level menu
items to catch the Popup event. Then record the current active items text.

Something along those lines, I'm not sure though because its not clear right
now.

I am hoping someone would read it and go "oh you just need to do this...."
or like many of my past posts I'll think of the answer about an hour after I
send the original post but that has yet to happen.

> M,
>
[quoted text clipped - 104 lines]
> >  }
> > }
AlexS - 23 Jul 2004 00:31 GMT
M,

you can get reference to parent menu using MenuItem.Parent property. From
parent menu you can go up using IsParent and GetMainMenu

I am not sure now what is your problem

HTH
Alex

> Thanks for answering.
>
[quoted text clipped - 120 lines]
> > >  }
> > > }
M - 23 Jul 2004 14:10 GMT
ok. The other responder Jacob showed me the light.

I was having a mental block and didn't make the connection to the parent
item being a menuitem itself. I kept thinking 'ok I have the parent item
which is a menu, how do I figure out which menuitem was the active one."

Now it is clear.

Thanks for you help though

Best Regards
M

> M,
>
[quoted text clipped - 138 lines]
> > > >  }
> > > > }
Jacob Munk-Stander - 23 Jul 2004 00:44 GMT
Hi,
I'm just wondering... what you're adding now is a reference to the same menu
item to all the subsubmenuitems. Try replacing your MyPopUpSubSubMenu with

public class MyPopUpSubSubMenu : ContextMenu
{
static int i = 0;
public MyPopUpSubSubMenu()
{
 MenuItems.Add(new MenuItem("a" + i++));
 MenuItems.Add(new MenuItem("b"));
 MenuItems.Add(new MenuItem("c"));
 MenuItems.Add(new MenuItem("d"));
}
};

and you'll see that there's only one reference being added (namely the
same). Instead try using

MenuItems[0].MergeMenu(new MyPopUpSubSubMenu());
MenuItems[1].MergeMenu(new MyPopUpSubSubMenu());
MenuItems[2].MergeMenu(new MyPopUpSubSubMenu());
MenuItems[3].MergeMenu(new MyPopUpSubSubMenu());

in MyPopUpSubMenu and you'll see the difference.

As far as I can see this creates a problem regarding the Parent property. I
tried coding something different:

  ContextMenu popup = new ContextMenu();

  popup.MenuItems.Add(new MenuItem("A"));
  popup.MenuItems[0].MenuItems.Add(new MenuItem("1"));
  popup.MenuItems[0].MenuItems.Add(new MenuItem("2"));
  popup.MenuItems[0].MenuItems.Add(new MenuItem("3"));
  popup.MenuItems[0].MenuItems.Add(new MenuItem("4"));
  popup.MenuItems[0].MenuItems[0].MenuItems.Add(new MenuItem("a"));
  popup.MenuItems[0].MenuItems[0].MenuItems.Add(new MenuItem("b"));
  popup.MenuItems[0].MenuItems[0].MenuItems.Add(new MenuItem("c"));
  popup.MenuItems[0].MenuItems[0].MenuItems.Add(new MenuItem("d"));

  popup.MenuItems.Add(new MenuItem("B"));
  popup.MenuItems.Add(new MenuItem("C"));
  popup.MenuItems.Add(new MenuItem("D"));

and used the following in the WhoseCallingMe function to traverse from child
to parent:

 public void WhoseCallingMe(object sender, System.EventArgs e)
 {
  MenuItem menuItem = (MenuItem) sender;

  string path = "";

  while(menuItem.GetType() == menuItem.Parent.GetType())
  {
   path = "-> " + menuItem.Text + path;

   menuItem = (MenuItem) menuItem.Parent;
  }

  path = "-> " + menuItem.Text + path;

  MessageBox.Show(path);
 }

If you know the menuitems on beforehand the individual menu items could get
their own event handler, though that could lead up to quite a lot of event
handlers.

What exactly are you trying to do? It sounds as if you want to create your
own ContextMenu class?

Hope this helps a bit!

Signature

Yours sincerely,
Jacob Munk-Stander | http://jacob.munk-stander.dk

> <see code snippet below>
>
[quoted text clipped - 93 lines]
>  }
> }
M - 23 Jul 2004 14:17 GMT
ok great, I've got it now. Thanks

This line >     menuItem = (MenuItem) menuItem.Parent;

cleared it up for me. I kept thinking that I have the parent but how do I
determine which menuitem was the 'active' one. I forget it was a menuitem
itself.

And thanks for pointing out the reference to the same menu item to all the
subsubmenuitems, that would of cause some problems.

Right now I know the menu items but at some point I'd like to make the
second tier menuitems added dynamically as the users see fit but that's down
the road a little bit.

Actually I'm going to have one handler for all the items. In the one handler
I needed to determine which second level item (and possibly first level too)
called me so it can open the correct form.

Thanks for your help. I do appreciate it.

Best Regards,
M

> Hi,
> I'm just wondering... what you're adding now is a reference to the same menu
[quoted text clipped - 169 lines]
> >  }
> > }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.