I have a List of strings that is regenerated every 5 seconds. I want to create a Context Menu and set its items dynamically using this list.
The problem is that I don't have even a clue how to do that and manage the Click action for every item generated (which should use the same method with different parameter DoSomething("item_name")).
How should I do this?
Thanks for your time.
Best regards.
So, you can clear the items from the context menu with:
myContextMenuStrip.Items.Clear();
You can add an item by calling:
myContextMenuStrip.Items.Add(myString);
The context menu has an ItemClicked event. Your handler could look like so:
private void myContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
DoSomething(e.ClickedItem.Text);
}
Seems to work OK for me. Let me know if I misunderstood your question.
Another alternative using a ToolStripMenuItem object:
//////////// Create a new "ToolStripMenuItem" object:
ToolStripMenuItem newMenuItem= new ToolStripMenuItem();
//////////// Set a name, for identification purposes:
newMenuItem.Name = "nameOfMenuItem";
//////////// Sets the text that will appear in the new context menu option:
newMenuItem.Text = "This is another option!";
//////////// Add this new item to your context menu:
myContextMenuStrip.Items.Add(newMenuItem);
Inside the ItemClicked event of your myContextMenuStrip, you can check which option has been chosen (based on the name property of the menu item)
private void myContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
ToolStripItem item = e.ClickedItem;
//////////// This will show "nameOfMenuItem":
MessageBox.Show(item.Name, "And the clicked option is...");
}
Related
I am trying to catch a click event on a context menu submenu created dynamically by the following code. The context menu cmList is created in the designer, and the click event code is added from the properties menu.
for (int i = 0; i <= sTagsContext.GetUpperBound(0); i++)
{
cmListTags.Items.Add(sTagsContext[i]);
ToolStripMenuItem submenu = new ToolStripMenuItem();
submenu.Text = i.ToString();
submenu.Image = Properties.Resources.InfoBig;
(cmListTags.Items[i] as ToolStripMenuItem).DropDownItems.Add(submenu);
chkListTags.ContextMenuStrip = cmListTags;
}
How can I create code to be executed when the submenu of any of the context menu items is clicked and have the identity of the submenu item (set in the text property) available?
I have tried adding an event handler using
(cmListTags.Items[i] as ToolStripMenuItem).DropDownItems.Add(i.ToString(), Properties.Resources.InfoBig, new EventHandler(InfoClicked));
where I create the function
public void InfoClicked(object sender, EventArgs e)
{
}
This function is called when the sub-menu is clicked but neither sender nor e has any information about the sub-menu item clicked - sender is null and e is empty.
If I set e to be type ToolStripItemClickedEventArgs and change the Dropdown addition line to
(cmListTags.Items[i] as ToolStripMenuItem).DropDownItems.Add(i.ToString(), Properties.Resources.InfoBig, new ToolStripItemClickedEventHandler(InfoClicked));
I get a compile time type mismatch for the last parameter of DropDownItems.Add.
You can use an anonymous method - a method body without a name.
int index = i;
cmListTags.Items[i] as ToolStripMenuItem).DropDownItems.Add(
i.ToString(),
Properties.Resources.InfoBig,
(s, args) => {
MessageBox.Show(index.ToString();
} ));
Since the anonymous method is declared in place, it has access to the local variable i. So in this way you don't need to use sender.
Edit: Turns out i is being modified in the for loop. So I have to use a local copy index to keep its value.
And as to your 2nd question,
I get a compile time type mismatch for the last parameter of DropDownItems.Add.
This is because the signature of InfoClicked does not match the signature of delegate ToolStripItemClickedEventHandler.
I am trying to do something simple here. I just want to pass selected items from a listview as a list to another window so I can bind it to combobox. Below is the code for my button that generates the list.
public void Button_Click(object sender, RoutedEventArgs e)
{
Attendees = new List<Person>();
foreach (Person attendee in lvUsers.SelectedItems)
{
Attendees.Add(attendee);
}
this.Close();
}
How do I pass this list to another window and bind it please?
snap Attendees list as filed in your window like
private List<Attendee> Attendees;
then on button click assign it and when you displaying new window of other form pass to constructor like
From1 myForm = new Form1(Attendees);
This was easy enough to do I just didn't have experience at the time. what I should have done is declared a public list and referenced it from another page. Or create a user control where I can change the datacontext so it can be binded.
I have added a context menu strip 'View' on my listview with some menu items, like Large icons/ Small icons/ Tiles.
Now whenever I select any of the options the respective view changes, but the menu doesn't get any Mark/ indication like that happens in Windows file explorer, where it shows bullet/ dot against the selected menu item.
Can someone please show, how I can get the similar dot/ bullet for my context menu?
I have tried CheckOnClick property which gets me a tick mark, but is there any other way that I could get that dot there?
Thanks in advance!
I couldn't find a way to get bullets/ dots like Windows File Explored's View options, but I used below logic and used checked states to indicate the selections made.
private void toolStripViewOptions_Click(object sender, EventArgs e)
{
ToolStripMenuItem selectedOption = sender as ToolStripMenuItem;
SetIndicationForSelectedOption(selectedOption);
}
private void SetIndicationForSelectedOption(ToolStripMenuItem selectedMenuItem)
{
ToolStripItemCollection menuItems = (contextMenuStrip.Items[(Int32)toolStripView.Tag] as ToolStripMenuItem).DropDownItems;
// Set checked state for only the selected view option and disable same for others.
foreach (ToolStripMenuItem item in menuItems)
{
if (selectedMenuItem == item)
selectedMenuItem.Checked = true;
else
item.Checked = false;
}
}
This worked for my requirement.
I am trying to create a dropdown so that users can see the names of accounts. How do i do that using DevExpress DropDown Button?
You should associate the drop-down button with a popup control/context menu. To accomplish this task use the DropDownControl property.
DXPopupMenu menu = new DXPopupMenu();
menu.Items.Add(new DXMenuItem("Admin"));
menu.Items.Add(new DXMenuItem("Guest"));
// ... add more items
dropDownButton1.DropDownControl = menu;
// subscribe item.Click event
foreach(DXMenuItem item in menu.Items)
item.Click += item_Click;
// setup initial selection
dropDownButton1.Text = menu.Items[0].Caption;
//...
void item_Click(object sender, EventArgs e) {
// synchronize selection
dropDownButton1.Text = ((DXMenuItem)sender).Caption;
// ... do something specific
}
The following objects can be used as popup controls:
PopupMenu - represents a popup menu managed by a BarManager or RibbonControl component.
PopupControlContainer - represents a container for other controls. This control is also managed by a BarManager component.
DXPopupMenu - represents a popup menu.
Im using a ApplicationBarIconButton to enable and disable if the user can edit a list (Microsoft.Phone.Controls.Toolkit's MultiselectList).
When the user clicks the ApplicationBarIconButton the icon changes from select icon to delete icon.
But if the status of the list changes IsSelectionEnabled to false again (the user unchecked the item) I can't seem to get a hold of the ApplicationBarIconButton... its null!
Here is the list's event for if the editing "mode" (IsSelectionEnabled) has changed:
private void ListOfFriends_IsSelectionEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var list = sender as MultiselectList;
if( list.IsSelectionEnabled )
EditMenuButton.IconUri = new Uri("/Images/ApplicationBar/Select.png", UriKind.Relative);
Here it throws telling EditMenuButton is null :-(
How can i fix this? Isnt it all running in UI thread?
Try this:
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IconUri = new Uri("SplashScreenImage.jpg", UriKind.Relative);