I'm using a drop down menu, and I'd like to make an item unavailable once it's been selected. Is there a method for that? As of now, the only action I have on it is a visible=true on some input form panels. I'm using C# and ASP.NET in visual studio 2010. Fair warning: if it isn't obvious from the nature of my question, this is actually my first go-round in the world of programming, so an advanced concept in an answer may just inspire more questions.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//Do Code with the Item
if(comboBox1.SelectedItem != null) //I forgot that ,better to prevent
comboBox1.Items.Remove(comboBox1.SelectedItem);
}
Usually in a drop-down menu, you intercept the OnOpening event and either set Visible=false or Enabled=false just before the menu opens. It sounds like you want to disable after the menu item was selected? Just put Enabled = false at the end of your item selected handler
Related
In Visual C++ MFC there is an inbuilt mechanism for setting the menu item states. I am trying to do the same with C# and a WindowsForm object.
I found this which is not quite the same:
Grey out menustrip items when certain forms are open/active/focused
Here is my menu structure:
So, I decided to try this:
private void viewToolStripMenuView_Click(object sender, EventArgs e)
{
zoomExtentsToolStripMenuItem.Enabled = viewCtrl != null;
}
It kind of works. But I am a bit picky. I can see the menu displayed with the item enabled and then I see it change to disabled.
What is the right way to set the menu item states before the menu is displayed? I know this sounds like a simple issue but I can't find the equivalent methodology to ON_UPDATE_COMMAND_UI.
I was using the wrong event handler!
private void viewToolStripMenuView_DropDownOpening(object sender, EventArgs e)
{
zoomExtentsToolStripMenuItem.Enabled = viewCtrl != null;
}
I'm working on a VB Winforms project (although I'm just as fine with a C# solution) and have the following set up:
I have a ContextMenuStrip on the form, msCreateReports
I have a MenuStrip at the top of the form with one menu item being Create Reports and its DropDown set to msCreateReports
I have a command button on the form cmdCreateReports
Now, for my command button, I have the following code for its click event:
Private Sub cmdCreateReports_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreateReports.Click
msCreateReports.Show(Cursor.Position.X, Cursor.Position.Y)
End Sub
EDIT (Update / Explanation):
Basically, what I'm looking for functionality-wise is to have this contextmenustrip be able to show up in one of 2 different places, depending on where the user clicks...
If they click on the menu option, the context menu appears as a drop-down on the Create Reports menu item OR if the user clicks the command button, the menu will apeear as a context menu on the side of the mouse pointer. I want the same menu to be able to appear on either one of these two locations depending on where the user clicks to make this menu appear.
Now, my problem is that only the first time the command button is clicked, the ContextMenuStrip appears up top by the menu, rather than on top of the command button, as I would like it to.
After the first click, the menu appears in the correct location... What did I do wrong / how can I fix this??
Thanks!!
I have the same problem (I'm using Visual Studio 2010 SP1, and C#). I don't think we did anything wrong, it looks like a Winforms bug to me.
I fixed it like this:
1) I have unset (using the visual designer) the DropDown property of the main strip item.
2) I have defined the Opening event on the contextMenuStrip, and the DropDownOpening event on the main strip item like this:
private void toolStripMyMenuItem_DropDownOpening(object sender, EventArgs e)
{
toolStripMyMenuItemMyLists.DropDown = contextMenuStrip;
}
private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
{
toolStripMyMenuItemMyLists.DropDown = contextMenuStrip;
}
And I don't have this problem anymore. Hope it will help you too :-)
Cursor.Position.X and Cursor.Position.Y are relative to Form, you need to use the Overloaded method ContextMenuStrip.Show(Control control, Point pos)
Example :
//control = the control you have added context menu
msCreateReports.Show(control, new Point(Cursor.Position.X, Cursor.Position.Y));
I had the same problem. I believe this to be a bug also as this behavior only happens the first time. So I force it to open and then close it before the user has a chance to interact with it. So in effect I use it once, before the user does and then it behaves correctly. Here is the code I added in the form load method. My combo box is used for allowing the user to select from a list of printers.
printerToolStripMenuItem.ShowDropDown();
toolStripComboBoxPrinter.Owner.Hide();
printerToolStripMenuItem.Owner.Hide();
I currently have a tabcontrol on my windows form in visual studio, i would like it so that when the user clicks on a different tab that i can execute some code (for example populate a listbox).
when i double click on the tab it only brings up an onclick event for the body of the tabcontrol.
i was thinking that i may have to create a thread in the form load that will constantly check whether the tab index changes and if it does then execute some code. but surely there must be an easier way?
You can use TabControl.Selecting or TabControl.SelectedIndexChanged event
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
//Your code goes here.
}
You should look at TabControl events
http://msdn.microsoft.com/en-ie/library/system.windows.forms.tabcontrol.selecting.aspx
I have a right click menu on c# winforms and the question is when I right click to get the menu how do I not get it to select the item when I right click?
I found a similar question, but it is for WPF here at this link
The problem being that i have index_changed event that is affected by the right click and i don't want it to be.
I am guessing that you want this to be handled in the Listviews selected index changed event..... The sample below shows how to determine the button pressed. Al it does is tell you yes or no if the right mouse button was clicked. This is the simplest example I can show without knowing your code.
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(Form.MouseButtons.HasFlag(MouseButtons.Right).ToString());
}
If the user click on a item in the listbox, the listboxItems_SelectedIndexChanged is called. But, even if the user miss an item and randomly clicks inside the listbox (not on items) the listboxItems_SelectedIndexChanged is still called.
How can I change this? I only want action on item click.
Note: removing the ability to navigate the application with keyboard is not a option.
I guess that in some cases you don't have enough list items in your control, therefore you have some space that you can click on and then SelectedIndexChanged is fired.
I guess you cannot dynamically resize the control to always fit the number of list items or else you wouldn't be asking this question.
Now, what should happen when the user click (selects) the same list item? Should some logic happen even though the selected index is the same (so when it was clicked the first time the same logic happend)?
If you require that selecting the same index more than once should be ignored then you could use the following hack:
Keep a variable at the form scope (the form containing the listbox control) and each time the selection index changes set that variable. Then use it later to check if the same selection has been made to ignore handling the event. Here is an example:
private int _currSelIdx = -1; // Default value for the selected index when no selection
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == _currSelIdx)
return;
Console.WriteLine(listBox1.SelectedIndex);
_currSelIdx = listBox1.SelectedIndex;
}
It ain't pretty, but hey...whatever works!
Maybe SelectedIndexChanged is not the right place to put your logic, since it is triggered even when you change the selection with the keyboard.
I would use MouseClick instead, checking if the click occurred over the selected item, i.e. something like this:
private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
if (listBox1.SelectedIndex < 0 || !listBox1.GetItemRectangle(listBox1.SelectedIndex).Contains(e.Location))
MessageBox.Show("no click");
else
MessageBox.Show("click on item " + listBox1.SelectedIndex.ToString());
}
This link may help, instead of double click, implement the same for single click
i want to detect an item double click in a winforms listbox control. [how to handle click on blank area?]