C# - ComboBox - How to capture a MouseClick on a item in the list? - c#

I read many answers, but in my case, I don't know why I can't capture the mouse click on the list items, it works only when I click on the object (ComboBox).
internal System.Windows.Forms.ComboBox MyComboBox;
this.MyComboBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyComboBox_MouseDown);
...
private void MyComboBox_MouseDown(object sender, MouseEventArgs e)
{
Console.WriteLine("MouseDown...");
}
When I click on the ComboBox itself or in the arrow I get the click event, but when I click on any items in the list of this ComboBox the click event never fires.
From other answers, they seem to use SelectedIndexChanged() event for Mouse Clicks... but this event also fires when it changes programmatically.
How I can detect a MouseDown on a ComboBox item?

Related

Why does the ButtonClick event on DropDown control not fired in VSTO Ribbon?

For example, I add a DropDown Control and its ButtonClick event handler method:
private void dropDown1_ButtonClick(object sender, RibbonControlEventArgs e)
{
MessageBox.Show("I am clicked.");
}
but, when I click DropDown control, it doesn.t show the message, which mean the event not fired. How to explain this?
The event is fired when a button is clicked on the list. So, you need to fill the list of Buttons like shown on the screenshot:

What Event for ComboBox to change text color while typing

I'm programming in WinForms.
I have a ComboBox set with an initial gray ForeColor. My goal is to change the text color of this ComboBox when the user start to type something.
I tried to use _TextChanged and TextUpdate Events but don't work.
private void ComboBox1_TextChanged(Object sender, EventArgs e)
{
ComboBox1.ForeColor = SystemColors.ControlText;
}
I already used the Event _SelectedIndexChanged to change the text color when the user select an item from the drop-down list, and it works well, but the text remains gray if the user types something (there is an AutoCompleteCustomSource collection associated to the ComboBox so the user can write instead to use the drop-down list).
Any suggestion?
EDIT
I have solved this way:
Registering in Form1.Designer.cs:
this.ComboBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.ComboBox1_KeyUp);`
Using this code:
private void ComboBox1_KeyUp(Object sender, KeyEventArgs e)
{
ComboBox1.ForeColor = SystemColors.ControlText;
}
Use KeyUp, KeyDown, or KeyPress events fired by the textbox. You probably don't want KeyPress for this purpose. TextChanged fires when the text has already been changed, which is why the new character being typed doesn't have a different color when you subscribe to it.
Have you looked at KeyUp event? MSDN Keyup
You should be able to subscribe to this event and do whatever you want inside it.

What event should I use to something when I select any text in a RichTextBox in WinForms?

I want to perform some action when some text is selected from a RichTextBox. So in what event of the RichTextBox should I write my logic?
I'm designing a notepad. I have one Menustrip and in the Menustrip I have Copy, Paste, Undo and Redo option. I want these options to be visible to the users only when a user selects any text in the RichTextBox.
I've tried it in many events but haven't succeeded so far.
I've tried in RichTextBox the Mousecapturechanged event and Toolstripmenu menu active event.
I am using C#.
Kindly help.
The best solution would be to subscribe to the MouseUp event of the RichTextBox and within the event handler, check if the length of the selected text is greater than 0 like this:
private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
{
if (richTextBox1.SelectedText.Length > 0)
{
// Show the Copy, Paste, Cut Buttons...
}
}
This is because the SelectionChanged event fires whenever the selection is changed and so will not even let you select text properly.
You can use SelectionChanged event and check if the cursor has changed or a part of text has been selected.
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
if (richTextBox1.SelectionLength < 2) return;
//Show the menu
}
You can use the SelectionChanged event which:
Occurs when the selection of text within the control has changed.
MSDN Link - RichTextBox.
This event would be ideal for what you aim to achieve.
Now you also need to use this properly. This event will fire even if you type something. So you have to verify that something is selected like this:
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
if (richTextBox1.SelectedText.Length > 1)
{
// Show the Copy, Paste, Cut Buttons...
}
}

WinForms - Disable default mouse hover over item behaviour?

I have a ComboBox with items in it. I also have a SelectedIndexChange event.
When I open the ComboBox and hover over an item, the SelectedIndex property seems to change to that item. I'd only like it to change when I click on the item. Is it possible to disable that behavior?
I have a timer that refreshes an Image based on SelectedIndex of ComboBox, but still, even if I highlight an item but don't select, why does the Image changes while it should not change and it only should change when I select an item.
The Problem
When the the mouse moves on items of ComboBox, the SelectedIndex changes but SelectedIndexChanged event doesn't fire, so in your timer Tick event, you will see the change while SelectedIndexChanged doesn't fire.
Scenario of reproducing the problem
To simply reproduce the problem, put a Timer on a form and enable it, then handle its Tick event. Also add a ComboBox and add some items to it and handle its SelectedIndexChanged event. When you open the dropdown and move mouse over items, you will see the Text of form changes to the index of item that is under the cursor, while SelectedIndexChanged doesn't fire and no MessageBox will show.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(this.comboBox1.SelectedIndex.ToString());
}
private void timer1_Tick(object sender, EventArgs e)
{
this.Text = this.comboBox1.SelectedIndex.ToString();
}
The Solution for your case
You can simply check if the dropdown is not open using DroppedDown property of ComboBox, then do the job:
private void timer1_Tick(object sender, EventArgs e)
{
if(!this.comboBox1.DroppedDown)
this.Text = this.comboBox1.SelectedIndex.ToString();
}

Context Menu - How can I know which control activated it

I have made a context menu which is activated via MouseDownevent. This event checks if the user clicked the right button and if so opens the menu. I am using the same event to open the same context menu for a listbox and a listview. Is there a way to check which one of them activated the MouseDown event?
Edit: I'll be a bit more specific. I can tell which controller activated the event from the event itself.. I want to know which controller activated the event from the context menu item which has been clicked on.
If you have something like that:
private void MouseDown(object sender, MouseButtonEventArgs e)
{
}
you can check sender:
if(sender is ListView)
{
//event fired by ListView
}
if(sender is ListBox)
{
//event fired by ListBox
}
etc.
I solved it by using the Tag property of the context menu. I put there the sender object which triggered the event, and then I could just do:
ListView lv = resultsContextMenu.Tag as ListView;
if (lv == null) //listbox was the one to call the mouse down event
{ //do stuff }
this code was called inside the menu items themselves that were chosen by the user

Categories