• Welcome to Valhalla Legends Archive.
 

Dynamic Menus

Started by shout, May 24, 2006, 02:59 PM

Previous topic - Next topic

shout

I have been trying to make a menu that has items based on how many of a certain type of file are found. I can make the menus, but when it comes time to set events to them I get stuck.

My current code:

mnu_textures = new MenuItem[files.Length];
for (int i = 0; i < files.Length; i++)
{
imglist.Images.Add(new Bitmap(files[i]));
mnu_textures[i] = new MenuItem();
mnu_textures[i].Index = i;
mnu_textures[i].Text = files[i].Split(new char[] {'_', '.', '\\'},100)
[files[i].Split(new char[] {'_', '.', '\\'}, 100).Length - 3];
mnu_textures[i].Click += new EventHandler(mnutextures_Click);
}
this.menuItem10.MenuItems.AddRange(mnu_textures);


I got to that, realized my problem, and I cannot for the life of me find a way to fix it.

Some ugly ways I was thinking of to fix it:

  • Make my own control that is inhereted from the MenuItem class.
  • Use the MouseMove event to find the position of the mouse at the time of clickage
  • Say fuck it

K

So what's the problem?  Looks like everything should be set up ok.

This line here is adding the click event handler:

mnu_textures[i].Click += new EventHandler(mnutextures_Click);


so in your event...

private void mnutextures_Click(object sender, System.EventArgs e)
{
     MenuItem m = (MenuItem)sender;
     MessageBox.Show("You clicked: " + m.Text);
}

shout

Quote from: K on May 24, 2006, 03:34 PM
So what's the problem? Looks like everything should be set up ok.

This line here is adding the click event handler:

mnu_textures[i].Click += new EventHandler(mnutextures_Click);


so in your event...

private void mnutextures_Click(object sender, System.EventArgs e)
{
MenuItem m = (MenuItem)sender;
MessageBox.Show("You clicked: " + m.Text);
}


OMG WHY AM I SO STUPID?

I forgot about the sender object...

MyndFyre

As a note, one of the things I like to do to associate data with a menu item are to subclass it.  For instance:


public class FileMenuItem : MenuItem {
  private string m_path;
  public FileMenuItem(string path)
  {
    this.m_path = path;
    this.Text = Path.GetFileName(path);
  }

  public string FilePath { get { return m_path; } }
}

Just a thought.  ;-)
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.