Author | Message | Time |
---|---|---|
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: [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); [/code] 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: [list] [li]Make my own control that is inhereted from the MenuItem class.[/li] [li]Use the MouseMove event to find the position of the mouse at the time of clickage[/li] [li]Say fuck it[/li] [/list] | May 24, 2006, 7:59 PM |
K | So what's the problem? Looks like everything should be set up ok. This line here is adding the click event handler: [code] mnu_textures[i].Click += new EventHandler(mnutextures_Click); [/code] so in your event... [code] private void mnutextures_Click(object sender, System.EventArgs e) { MenuItem m = (MenuItem)sender; MessageBox.Show("You clicked: " + m.Text); } [/code] | May 24, 2006, 8:34 PM |
shout | [quote author=K link=topic=15047.msg153114#msg153114 date=1148502842] So what's the problem? Looks like everything should be set up ok. This line here is adding the click event handler: [code] mnu_textures[i].Click += new EventHandler(mnutextures_Click); [/code] so in your event... [code] private void mnutextures_Click(object sender, System.EventArgs e) { MenuItem m = (MenuItem)sender; MessageBox.Show("You clicked: " + m.Text); } [/code] [/quote] OMG WHY AM I SO STUPID? I forgot about the sender object... | May 24, 2006, 8:44 PM |
Myndfyr | As a note, one of the things I like to do to associate data with a menu item are to subclass it. For instance: [code] 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; } } } [/code] Just a thought. ;-) | May 24, 2006, 10:25 PM |