I have two Problems with the Mouse Events of ComboBoxes. What I want to achieve is a "Touch-and-Release"-Solution, that means after the User pressed/touched the Combobox for 500ms something should happen. I have Class where I fireup all my Events to my Controls. For the Comboboxs I do it like this:
((ComboBox)obj).PreviewMouseDown -= CTRLMouseButtonEventHandler_Down;
((ComboBox)obj).PreviewMouseDown += CTRLMouseButtonEventHandler_Down;
((ComboBox)obj).PreviewMouseUp -= CTRLMouseButtonEventHandler_Up;
((ComboBox)obj).PreviewMouseUp += CTRLMouseButtonEventHandler_Up;
My Up/Down-Events look like this:
private void CTRLMouseButtonEventHandler_Down(object sender, MouseEventArgs e)
{
_currentControl = (Control)sender;
_touchHoldTimer = new System.Windows.Threading.DispatcherTimer();
_touchHoldTimer.Interval = TimeSpan.FromMilliseconds(500);
_touchHoldTimer.Tick += TouchHoldTimer_Tick;
_touchHoldTimer.Start();
}
private void CTRLMouseButtonEventHandler_Up(object sender, MouseEventArgs e)
{
_touchHoldTimer.Stop();
}
The first Problem is, when my Focus is on another Control and click into the Combobox and hold, nothing happens. I first have to click into the Combobox and then click-and-hold and it works.
My second Problem is, that the PreviewMouseDown is also fired when the Scrollbar or ToggleButton of the Combobox is pressed. I tried something like this:
((ComboBox)obj).AddHandler(TextBox.PreviewMouseDownEvent, new RoutedEventHandler(CTRLMouseButtonEventHandler_Down2));
((ComboBox)obj).AddHandler(TextBox.PreviewMouseUpEvent, new RoutedEventHandler(CTRLMouseButtonEventHandler_Up2));
But it didn' work. Can somebody point me in the reight direction please?
Add another event. Set Event for hovering mouse over Combobox to give it selection. It would be same as clicking on it.
EDIT:
other possible events:
DropDownOpened
ContextMenuOpening
https://msdn.microsoft.com/en-us/library/system.windows.controls.combobox_events(v=vs.110).aspx
2.
KeyDown event is not firing when pressing enter in an UserControl
Just replace enter with space.
Related
I add a function that adds text to FlowDocument when the mouse clicks.
There is no Click event in FlowDocument, so I listen to FlowDocument.MouseLeftButtonDown and MouseLeftButtonUp and check whether the mouse moves between down and up. When I click the mouse left button, the text successfully adds. However, I can't select any text in the FlowDocument.
I tried PreviewMouseLeftButtonDown and PreviewMouseLeftButtonUp. The behavior is the same. Isn't there a PostMouseLeftButtonDown?
My Code:
Point mouseDownPoint;
private void doc_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
mouseDownPoint = Mouse.GetPosition(doc);
e.Handled = true;
}
private void doc_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var mouseUpPoint = Mouse.GetPosition(doc);
if ((mouseUpPoint - mouseDownPoint).Length < 8) /* add text */;
}
The control handles the event internally.
If you register the event handler programmatically like this, your doc_MouseLeftButtonUp event handler should get invoked (note that last handledEventsToo parameter):
doc.AddHandler(ContentElement.MouseLeftButtonUpEvent,
(MouseButtonEventHandler)doc_MouseLeftButtonUp, true);
Note that you may also have to take care of the MouseLeftButtonUp that is raised by the control itself.
I found the solution. Listen to FlowDocument.MouseLeftButtonDown and do not use e.Handled=true and listen to FlowDocumentScrollViewer.PreviewMouseLeftButtonUp will get text selection and add text behavior at the same time.
I have a WPF project. In this project at some point I'm dynamically making button in code behind like this:
private Button makeButton()
{
Button b = new Button();
b.Width = 24;
b.Height = 19;
b.Click += ButtonClick;
return b;
}
Where the ButtonClick is:
public void ButtonClick(object sender, RoutedEventArgs e)
{
// Do stuff...
}
Sometimes happen the event handler is called on pressing enter, even when button is not focused.
So my question is how can I disable to handle this event when it's caused by enter.
Tnaks you
I had the same problem. Using PreviewMouseLeftButtonUp event fixed my problem. It will not hire when you use enter button
It is probably getting focused before you press Enter.
Anyway, you could try to set the Focusable property to false:
b.Focusable = false;
Also make sure that you don't set the IsDefault property to true.
I'm not sure how this is happening with the button not focussed. Maybe this is logical rather than keyboard focus.
It'd have been nice to have a minimal reproduction.
Two things you can try.
1)
Eat the enter key just in case the keypress is somehow going to the button by using the previewkeydown event on the button. Marking the event as handled will stop the keydown event from firing so no enter will reach the button if it's somehow logically receiving the keypress.
b.PreviewKeyDown += (o, e) =>
{
if (e.Key == Key.Enter) e.Handled = true;
};
Set IsDefault="False" on the buttons in case they're somehow being set as default.
So I would like to know what is wrong with the following code, especially from a theoretical point of view.
I have a user control in which I've added a text box.
When I click in the text box I would like the Mouse clicked event raised in the user control.
To my mind, the solution should be:
Create an event handler for the mouse click event in the text box.
in this event handler, raise the mouse click event for the user control.
so this is what i have:
private void txtLog_MouseClick(object sender, MouseEventArgs e)
{
this.OnMouseClick(e);
}
i have tried it and it doesn't work, why is this?
P.S. I would really like to know why this is wrong! A correct solution is great, but I'm really trying to understand where I'm going wrong here. Thank :-)
Well, you could just click on your textbox in design mode and in the property window in events tab add the click event. or if you want to do it in runtime you can do it like this:
textbox.Click += Txt_Click;
private static void Txt_Click(object sender, EventArgs e)
{
// do your thing
}
or even shorter:
textbox.Click += (s,e) =>
{
//do your thing
};
you should do these three steps
declare an MouseClick delegation method for textbox
assign method to textbox
add this delegation to the this (form) OnMouseClick event [on user control constructor]
Step1:
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
}
Step2:
this.textBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseClick);
Step3:
public myUserControl()
{
InitializeComponent();
this.MouseClick += new MouseEventHandler(textBox1_MouseClick);
}
I have some context menu items that are not clickable. They just report the status of something. I don't like how the cursor still appears like they're clickable though.
Anyway to change this?
There isn't a Cursor Field like one would expect.
Handle the MouseMove event of the whole ToolStrip and check if the current mouse location is between the toolStripItem.Bounds. if so, change ToolStrip.Cursor
Amiram sent me in the right direction. You can't set the Cursor on the "ToolStripMenuItem" you have to set it on the parent ContextMenuStrip.
As for the mouse events, that has to go on the ToolStripMenuItems. As the MouseMove event is not fired when the Mouse is over ToolStripMenuItems.
// Init Code
contextMenuStrip1.Cursor = Cursors.Hand;
recentMessagesToolStripMenuItem.MouseLeave += new EventHandler(SetCursorToHandOn_MouseLeave);
recentMessagesToolStripMenuItem.MouseEnter += new EventHandler(SetCursorToArrowOn_MouseEnter);
private void SetCursorToArrowOn_MouseEnter(object sender, EventArgs e)
{
contextMenuStrip1.Cursor = Cursors.Arrow;
}
private void SetCursorToHandOn_MouseLeave(object sender, EventArgs e)
{
contextMenuStrip1.Cursor = Cursors.Hand;
}
I'm having problems focusing the textbox. I want to focus the textbox when I select a specific item from the listview. The Focus() will work when I use the up down arrows from the keyboards, but when I use the mouse its not working.
***EDIT***
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Focus();
}
private void Form1_Load(object sender, EventArgs e)
{
ListViewItem lvi = new ListViewItem("A");
lvi.SubItems.Add("AA");
listView1.Items.Add(lvi);
ListViewItem lvi1 = new ListViewItem("B");
lvi1.SubItems.Add("BB");
listView1.Items.Add(lvi1);
}
The Click and Mouseclick events happen after the listview SelectedIndexChanged event, so if you have textbox.focus in SelectedIndexChanged, focus comes back to the listview after the click or mousclick event. If you add a textbox.focus to the listview mouseclick event, focus will end up on the text box (even though it goes there twice).
In your SelectedIndexChanged event handler, first test if this.listView1.SelectedIndex > -1, and if it is, then do this.textBox1.Focus()