C# WinForms Right Click Context Menu without Selecting item - c#

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());
}

Related

How to have a split button show context menu only when the split part is clicked?

I have found many question concerning split buttons, but I need clarification.
I am using a Wisej split button. The following code will show a context menu to the bottom left of the button part(left-side of the split button which has button text) of a split button.
private void splitButton1_Click(object sender, EventArgs e)
{
Button btnSender = (Button)sender;
Point ptLowerLeft = new Point(-20, btnSender.Height);
ptLowerLeft = btnSender.PointToScreen(ptLowerLeft);
contextMenu1.Show(ptLowerLeft);
}
I want to know is how to have a context menu show only when the split part (i.e. the small section on the right-side of the split button with an arrow icon) of the split button is clicked and not on the button side.
You don't need to use a context menu object simply add the menu items to the SplitButton.MenuItems collection. You'll get a Click event when clicking on the button and an ItemClicked event when clicking on a menu item. The menu items will show only when clicking on the split part of the button.
When adding MenuItems to a Button (instead of a SplitButton) the menu will always show when clicking on the button.
The latest release added the Orientation property to SplitButton to show the split part below the button when set to Vertical.
HTH. Sorry for the late reply.

Tab index is not working on radio buttons

This is the part of my form that I am asking about
This is the tab index:
The problem that the tab goes from Farmer Audi Status to Yes, then to Ownder Bank Name instead of going to No
please notice that the yes and no already have 0.1.6.0 and 0.1.6.1 respectively.
could you help me please?
Notice
both radio buttons has TabStop property to True
From How to: Set the Tab Order on Windows Forms (MSDN):
A radio button group has a single tab stop at run time. The selected button (that is, the button with its Checked property set to true) has its TabStop property automatically set to true, while the other buttons have their TabStop property set to false.
In other words, what you're seeing is normal. Those "Yes/No" radio buttons are in the same group, and you can't tab between radio buttons in the same group. As you tab, you'll only focus on the currently selected one, then move to the next control on the form (in your case, a TextBox).
To work around this, you could place each radio button in its own container (such as a Panel), which means you'd have two "groups" each with one radio button. But then you lose the built-in functionality that automatically deselects one radio button when you select the other. Your user will be able to select both radio buttons, so you'd need to add some logic that disables the other. If you decide to try that, experiment with the radio buttons' CheckedChanged or Click / MouseClick events.
As Steve said, and as stated in the answer he linked to, the way it works out-of-the-box is expected behavior for Windows, so think twice before overriding it unless you have a good reason for doing so.
It worked for me!
first you have to create a method like this:
private void TabStopChanged(object sender, EventArgs e)
{
((RadioButton)sender).TabStop = true;
}
and then, put this in your Form_Load event:
private void Form_Load(object sender, EventArgs e)
{
foreach (var item in this.Controls)
{
if (item.GetType() == typeof(RadioButton))
((RadioButton)item).TabStopChanged += new System.EventHandler(TabStopChanged);
}
}
For radio buttons, you don't have to use Tab to navigate. Just use right and left keys to traverse radio buttons.
Check out this link to read more - https://www.csun.edu/universal-design-center/web-accessibility-criteria-tab-order

Why does my MenuStrip appear in the incorrect Location on first click?

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();

How to find Context menu was shown and not to handle click events

I got a quick question,
I am developing a custom chart control. The chart control let users to draw lines or drop rectangle (could be anything). This start draw/ drop options are set to controls mouse down, left button click and mouse down right click is set to show context menu.
My problem is, if user decide not to select anything and come out from context menu while it is shown they should left click on control as right click will show the menu again. But at the moment context menu is diapering and rest of the left click also executed. How can I verify context menu was shown on left click and user wants to come out from context menu and not to execute start draw/ drop logic.
Additional Info:
I am working on C# project, I tried to capture context menu closed event but it triggers and closes itself before it comes to controls mouse down event and I can not verify was context menu was shown before to avoid going through rest of the left mouse button down event logic.
Any help is greatly appreciated in advance.
Here's a simple solution.
Suppose you have a ListBox control on your form which has a ContextMenu associated with it. Now we want to add a list item to the control each time it is clicked:
private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
listBox1.Items.Add("new item added - " + DateTime.Now.ToLongTimeString());
}
Now define a bool variable at your form level called menuClosed like so:
private bool menuClosed = false;
Now capture the context menu's Closed event like so and flip the flag:
private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
menuClosed = true;
}
Now update the code that adds an item to the list box control like the following:
private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
if (!menuClosed)
listBox1.Items.Add("new item added - " + DateTime.Now.ToLongTimeString());
}
I'm just setting the bool variable to true when the context menu is closed, then i check for the bool flag to see if an item should be added to the list. you can use this kind of same mechanism to determine a specific command should be executed or not.

How to change drop down button in combobox control?

How to change dropdown button in a ComboBox control (C#, Windows Forms)? I have a custom button, and I want to use it in the ComboBox instead of the default dropdown button.
I think Hans Passant solution is the way...
From here:
http://social.msdn.microsoft.com/forums/en-US/winformsdesigner/thread/5d65f987-834c-465f-a944-622831d4cfb0
You can create a UserControl, drag a
ComboBox and a Button onto it, make
the Button right over the ComboBox's
arrow button to make the arrow button
invisible, handle the Button's Paint
event to draw an arrow on it, this can
be done by calling
ComboBoxRenderer.DrawDropDownButton()
method (Notice: this method has a
limit, it needs the visual style being
enabled on the OS) or by drawing an
icon on it, or just drawing a small
triangle on it.
Then handle the Click event of the button to show the ComboBox's
DropDown, this can be done by
something like this
private void button1_Click(object sender, EventArgs e)
{
this.comboBox1.DroppedDown = true;
}

Categories

Resources