WPF MouseLeftButtonDown makes FlowDocument unable to select text - c#

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.

Related

Problems with Combobox MouseDown Events

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.

Fire an event after Richtextbox Textchanged event

I am writing a lexer and i also want my text editor to change the color of the keywords. While typing! like VS and ... ! but the problem is TextChanged event of a RichTextBox fires BEFOR adding the text to it.
Is there any pre defined event that fire write AFTER adding the text to the richTextBox ? if not how can i create one that fire write after entering the text!
PS : i am writing a WPF project!
private void rtxMain_TextChanged (object sender, TextChangedEventsArgs e) {
visual.getLastWord();
}
TextChnaged event fires after changing the text of RichTextBox. Here is a working example of it:
private void richtextBox1_TextChanged(object sender, TextChangedEventArgs e)
{
string text = new TextRange(richtextBox1.Document.ContentStart, richtextBox1.Document.ContentEnd).Text;
MessageBox.Show(text);
}
Whatever I type into the richtextBox1, it shows me the exact same value in the MessageBox. It means it fires the event after changing the Text.
you can use the keyUp event it will fire when a key on the keyboard is let go off.
RichTextBox r = new RichTextBox();
r.KeyUp += (s, e) =>
{
//your logic
};
You might need to add extra logic to avoid keypresses like shift which isnt hard to do and maybe not even neccesary.

Route multiple buttons to the same routine

I have 10 buttons, 0-9 (button0, button1, button2...). When I click any of these buttons, I would like to perform the same routine on them. I would like to know how to, upon clicking of any of these buttons, direct them to the routine below.
private void button0_Click(object sender, EventArgs e)
{
int newValue;
newValue = Convert.ToInt32(Button.text);
}
I have already gone into the properties of each button, then events, and changed the click event to button0_Click (I would have thought this would add "handles button1.click, button2.click, etc." after "private void button0_Click(object sender, EventArgs e)" but if it does that in the background, that's ok as long as it works.)
I also need to know how to identify the button that has been pressed, which is where I'm at with "Convert.ToInt32(Button.text)" (e.g. button2.text = "2").
You can select the same event handler for all the buttons in the designer (in the event tab of the properties window, select the event and there'll be a drop down with all your defined event handlers).
To get which button has been clicked on, cast the sender argument to a Button and you'll have it.
Button button = (Button)sender;
int value = int.Parse( button.Text );
Edit: Also, the "Handles control.event" syntax only exists in Visual Basic.
Edit: Check out the generated code (Form1.Designer.cs, for example) to see how the events are hooked up.
The C# language doesn't use handles to bind events (as VB does). The code for the actual binding is in the generated code for the form, i.e. in the background as you put it.
The sender property is a reference to the control where the event happened. You just need to cast it to the actual type of the control:
private void button0_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
int newValue = Convert.ToInt32(button.text);
}
As an alternative to using the text of the button (for example if you want to translate the application to different languages, or simply don't want to rely on the text), you can put whatever you like in the Tag property of each button, and retrieve it in the event handler.
You could wire them all up to the same event handler an extract the button from sender e.g.
private void button0_Click(object sender, EventArgs e)
{
var button = sender as Button
if (button != null)
{
int newValue = Convert.ToInt32(Button.text);
}
}

C# Window form text box contents have been highlighted event

is there an event for when the contents of a text box have been highlighted.
If not is there a way to determine this.
Also is there a way to determine of a text box has been tabbed to.
Thanks,
There is no event for highlighting.. but you can simulate it via the MouseUp and KeyUp events:
private void textBox_MouseUp(object sender, MouseEventArgs e) {
if (textBox.SelectedText != "")
MessageBox.Show("Selected!");
}
private void textBox_KeyUp(object sender, KeyEventArgs e) {
if (textBox.SelectedText != "")
MessageBox.Show("Selected!");
}
There is also an Enter event for when the TextBox gains focus.
You will have to mix a few events for this.
Capture Left Mouse button OR Shift Key+Arrow key
MouseDrag event also must be captured after that
Mouse Button Up too (for the next time).

MouseDoubleClick events don't bubble

My scenario, simplified: I have a ListView containing rows of Employees, and in each Employee row, there are buttons "Increase" and "Decrease" adjusting his salary.
Pretend that in my program, double-clicking an Employee row means "fire this person".
The problem is that while I'm clicking "Increase" rapidly, this triggers a double click event on the ListViewItem. Naturally, I don't want to fire people when I'm just increasing their salary.
According to how all other events work, I expect to be able to solve this by setting Handled=true on the event. This, however, doesn't work. It appears to me that WPF generates two separate, completely unlinked, double click events.
The following is a minimal example to reproduce my issue. The visible components:
<ListView>
<ListViewItem MouseDoubleClick="ListViewItem_MouseDoubleClick">
<Button MouseDoubleClick="Button_MouseDoubleClick"/>
</ListViewItem>
</ListView>
And the handler code:
private void Button_MouseDoubleClick(object s, MouseButtonEventArgs e) {
if (!e.Handled) MessageBox.Show("Button got unhandled doubleclick.");
e.Handled = true;
}
private void ListViewItem_MouseDoubleClick(object s, MouseButtonEventArgs e) {
if (!e.Handled) MessageBox.Show("ListViewItem got unhandled doubleclick.");
e.Handled = true;
}
After firing up this program and double-clicking the listed button, both messageboxes show up in sequence. (Also, the button is stuck in the down position after this.)
As a "fix" I can, on the ListViewItem handler, inspect the visual tree attached to the event and check that "there is a button there somewhere" and thus discard the event, but this is a last resort. I want to at least understand the issue before coding such a kludge.
Does anyone know why WPF does this, and an elegant idiomatic way to avoid the problem?
I think you'll find that the MouseDoubleClick event is an abstraction on top of the MouseDown event. That is, if two MouseDown events occur in quick enough succession, the MouseDoubleClick event will also be raised. Both the Button and ListViewItem appear to have this logic, so that explains why you're seeing two distinct MouseDoubleClick events.
As per MSDN:
Although this routed event seems to
follow a bubbling route through an
element tree, it actually is a direct
routed event that is raised along the
element tree by each UIElement. If you
set the Handled property to true in a
MouseDoubleClick event handler,
subsequent MouseDoubleClick events
along the route will occur with
Handled set to false.
You could try handling MouseDown on the Button and setting that to handled so that it doesn't propagate to the ListViewItem.
Wish I could verify this myself but I'm .NET-less at the moment.
The MSDN documentation for the MouseDoubleClick does give a suggestion on how to keep the MouseDoubleClick event from bubbling up:
Control authors who want to handle
mouse double clicks should use the
MouseLeftButtonDown event when
ClickCount is equal to two. This will
cause the state of Handled to
propagate appropriately in the case
where another element in the element
tree handles the event.
So you could hanlde the MouseLeftButtonDown event and set hanged to true if ClickCount is two. But this fails on Buttons because they already handle the MouseLeftButtonDown and don't raise that event.
But there is still the PreviewMouseLeftButtonDown event. Use that on your buttons to set handled to true when ClickCount equals two as below:
private void Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
if (e.ClickCount == 2)
e.Handled = true;
}
Since there have been no definite answers to this question, this is the workaround I ended up using:
protected override void ListViewItem_MouseDoubleClick(MouseButtonEventArgs e) {
var originalSource = e.OriginalSource as System.Windows.Media.Visual;
if (originalSource.IsDescendantOf(this)) {
// Test for IsDescendantOf because other event handlers can have changed
// the visual tree such that the actually clicked original source
// component is no longer in the tree.
// You may want to handle the "not" case differently, but for my
// application's UI, this makes sense.
for (System.Windows.DependencyObject depObj = originalSource;
depObj != this;
depObj = System.Windows.Media.VisualTreeHelper.GetParent(depObj))
{
if (depObj is System.Windows.Controls.Primitives.ButtonBase) return;
}
}
MessageBox.Show("ListViewItem doubleclicked.");
}
Class names are here unnecessarily typed with full namespaces for documentation purposes.
Well it may not be elegant or idiomatic, but you might like it better than your current workaround:
int handledTimestamp = 0;
private void ListViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (e.Timestamp != handledTimestamp)
{
System.Diagnostics.Debug.WriteLine("ListView at " + e.Timestamp);
handledTimestamp = e.Timestamp;
}
e.Handled = true;
}
private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (e.Timestamp != handledTimestamp)
{
System.Diagnostics.Debug.WriteLine("Button at " + e.Timestamp);
handledTimestamp = e.Timestamp;
}
e.Handled = true;
}
The weird thing is this doesn't work if you don't set e.Handled = true. If you don't set e.Handled and put a breakpoint or a Sleep into the button's handler, you will see the delay in the ListView's handler. (Even without an explicit delay there will still be some small delay, enough to break it.) But once you set e.Handled it doesn't matter how long of a delay there is, they will have the same timestamp. I'm not sure why this is, and I'm not sure if this is documented behavior that you can rely on.
Control.MouseDoubleClick is not a bubble event but a direct event.
Since checking this question with Snoop, which is a tool for browsing visual trees and routed events, I see that Control.MouseDoubleClick events of 'ListView' and 'ListBoxItem' are fired at one time. You could check with this Snoop tool.
First, to find an answer, it is needed to check that both event arguments of the MouseDoublClick are same objects. You would expect they are same objects. If it is true, it is very strange as your question, but they are not same instances. We can check it with following codes.
RoutedEventArgs _eventArg;
private void Button_MouseDoubleClick(object s, RoutedEventArgs e)
{
if (!e.Handled) Debug.WriteLine("Button got unhandled doubleclick.");
//e.Handled = true;
_eventArg = e;
}
private void ListViewItem_MouseDoubleClick(object s, RoutedEventArgs e)
{
if (!e.Handled) Debug.WriteLine("ListViewItem got unhandled doubleclick.");
e.Handled = true;
if (_eventArg != null)
{
var result = _eventArg.Equals(e);
Debug.WriteLine(result);
}
}
It means that the event argument of the MouseDoublClick is created newly at somewhere, but I don't understand deeply why it is.
To be clearer, let's check for the event argument of the BottonBase.Click. It will be return the true about checking same instances.
<ListView>
<ListViewItem ButtonBase.Click="ListViewItem_MouseDoubleClick">
<Button Click="Button_MouseDoubleClick" Content="click"/>
</ListViewItem>
</ListView>
If you only focus on the execution as you mentioned there'll be lots of solutions. As above, I think that using the flag(_eventArg) is also good choice.
I've just had this same problem. There is a simple but non-obvious solution.
Here is how double click is raised by Control ....
private static void HandleDoubleClick(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
Control control = (Control)sender;
MouseButtonEventArgs mouseButtonEventArgs = new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, e.ChangedButton, e.StylusDevice);
if (e.RoutedEvent == UIElement.PreviewMouseLeftButtonDownEvent || e.RoutedEvent == UIElement.PreviewMouseRightButtonDownEvent)
{
mouseButtonEventArgs.RoutedEvent = Control.PreviewMouseDoubleClickEvent;
mouseButtonEventArgs.Source = e.OriginalSource;
mouseButtonEventArgs.OverrideSource(e.Source);
control.OnPreviewMouseDoubleClick(mouseButtonEventArgs);
}
else
{
mouseButtonEventArgs.RoutedEvent = Control.MouseDoubleClickEvent;
mouseButtonEventArgs.Source = e.OriginalSource;
mouseButtonEventArgs.OverrideSource(e.Source);
control.OnMouseDoubleClick(mouseButtonEventArgs);
}
if (mouseButtonEventArgs.Handled)
{
e.Handled = true;
}
}
}
So if you handle PreviewMouseDoubleClick setting e.Handled = true on the child control MouseDoubleClick won't fire on the parent control.
You cannot easily change the way double clicking events get fired because they are dependent on user settings and that delay is customized in control panel.
You should checkout RepeatButton that allows you to press's button and while it is pressed it generates multiple click events in regular sequence.
In case if you want to customize event bubbling then you should search for Preview events that allows you to block propogation of events. What are WPF Preview Events?

Categories

Resources