Newbie: Best way to populate a context menu from DB?
-
What is the best way to populate a context menu with data from a DB when it is clicked on? Basically I am trying to create a context menu from a notifyIcon and populate it with names & numbers when it is right clicked... I can get the data from the DB, I just need to know how to put this data into the menu... Any ideas or pointers? Thanks in advance, Phil
-
What is the best way to populate a context menu with data from a DB when it is clicked on? Basically I am trying to create a context menu from a notifyIcon and populate it with names & numbers when it is right clicked... I can get the data from the DB, I just need to know how to put this data into the menu... Any ideas or pointers? Thanks in advance, Phil
You can dynamically create the context menu when the click is performed. Also you can use one context menu and just clear the menu items and add new items at runtime. The code below is .NET 2 same logic different control in .NET 1.1
contextMenuStrip1.Items.Clear();
ToolStripItem ts = null;
for(int i=0;i<5;i++){
ts = contextMenuStrip1.Items.Add(i.ToString());
}
contextMenuStrip1.Show();A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane
-
You can dynamically create the context menu when the click is performed. Also you can use one context menu and just clear the menu items and add new items at runtime. The code below is .NET 2 same logic different control in .NET 1.1
contextMenuStrip1.Items.Clear();
ToolStripItem ts = null;
for(int i=0;i<5;i++){
ts = contextMenuStrip1.Items.Add(i.ToString());
}
contextMenuStrip1.Show();A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane
We can create context menu in this way but also we need to handle events for these context menus. To do so we can create a common event for all menu items and then based on the context name respective code can be executed. Impossible itself says I M Possible.
-
We can create context menu in this way but also we need to handle events for these context menus. To do so we can create a common event for all menu items and then based on the context name respective code can be executed. Impossible itself says I M Possible.
duh
private void button1_Click(object sender, EventArgs e) {
contextMenuStrip1.Items.Clear();
ToolStripItem ts = null;
for(int i=0;i<5;i++){
ts = contextMenuStrip1.Items.Add(i.ToString());
ts.Click += new EventHandler(ts_Click);
ts.Tag = [PUT SOMETHING HERE TO ID IT]
}
contextMenuStrip1.Show();
}private void ts_Click(object sender, EventArgs e){
...
}A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane
-
We can create context menu in this way but also we need to handle events for these context menus. To do so we can create a common event for all menu items and then based on the context name respective code can be executed. Impossible itself says I M Possible.
All you have to do is bind your events at the time you create each context menu item. You would basically be doing in abstract code, what the designer does with hard coded code.
foreach ( string element in myListofThings )
{
ToolStripMenuItem menu = new ToolStripMenuItem();
menu.Name = element.ToLower();
menu.Text = element;
menu.Click+=new SystemEventHandler(HandleContextClick);
myContextMenu.Add( menu );
foreach ( string subElement in SubElementList )
{
ToolStripMenuItem submenu = new ToolStripMenuItem();
blah blah blah
submenu.Click+=new SystemEventHandler(HandleSubContextClick);
}
}:suss: