Good evening. I've looked at everything I can on Google and cannot find what I'm looking for (or I am not searching correctly). Thanks ahead of time for the help.
In XAML, on a ContentControl, I have MouseDoubleClick="ControlClick".
This ContentControl does not have a single mouse click event. So I added one in code. When that single-mouse-click event in code is called, I would like to call the MouseDoubleClick event defined on the ControlConrol in XAML.
In the single-click mouse event, I have the ControlControl object (I'll call it "c"). There is a ContentControl.MouseDoubleClick handler but I do not know how to invoke it.
How may I? Thank you in advance.
First of all, ContentControl does have single mouse button click events: MouseButtonDown and MouseButtonUp, depending when you'd like to handle the action.
Then, to invoke a MouseDoubleClick event in your single click event handler, you'd call the following:
(sender as ContentControl).RaiseEvent(new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, e.ChangedButton) { RoutedEvent = MouseDoubleClickEvent });
where sender is the object and e is the MouseButtonEventArgs e of the single click event.
You must remember that double clicking will now invoke two single click events (which invoke the double clicking in turn) and a double click event, thus the double click event handler will effectively be called trice.
You can protect against this behaviour by checking the TimeStamp property of the MouseButtonEventArgs and ignoring the incoming event in the handler if the timespan between the clicks is too short.
Related
So, I would double click here on my designer
and it should create me the code, but well it doesn't. And there is no value changed event in the events either.
So if anyone knows how to fix this, it would be nice. (I doubt it) so how would I get around this? How would I go on about creating the code myself that should be created when I double click on it?
Click the form or control that you want to create an event handler for.
In the Properties window(F4), click the Events button
In the list of available events, click the event that you want to create an event handler for.
In the box to the right of the event name, type the name of the handler and press ENTER.
Add the appropriate code to the event handler.
To create an event handler in the code editor
Switch to the code editor by using one of the following techniques:
Create a new method like:
private void buttonName_Click(object sender, EventArgs e) { ... }
In the file YourFormName.Designer.cs find your button and add
this.buttonName.Click += new System.EventHandler(this.buttonName_Click);
I am trying to use the MouseClick event from the properties of a listView to handle left and right mouse clicks.
Unfortunately the event never seems to fire. (Double clicked on the event to create a property, entered a bit of simple code and placed a breakpoint on the first line). The same is true of several other events listed in the properties (ItemSelectionChanged seems to work but the other events I have tried don't fire.
Here is the code added:
In form.designer.cs:
this.listView1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.listView1_MouseClick);
In form.cs:
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
Some code
}
That method never gets called when I click on the listview. The listview is inside a tab on top of the stack.
I guess I am probably forgetting something very basic but what?
ListView is a bit unusual, its MouseClick event doesn't fire unless you click an item in the view. Workaround is to use the MouseDown or MouseUp event instead. You typically are much more interested in the ItemSelectionChanged event btw. You probably need its HitTest() method to see exactly what was clicked if you use MouseDown/Up.
I have written a custom control in C# (inherited from Forms.Control) and it seems to working fine, but if you press the button fast enough a problem occurs: only every other click will call the click event handler. This doesn't happen if you don't press it fast (less than once a second). The mouseUp and mouseDown handlers always get called no matter how fast you click the button.
Of course doesn't happen with the canned winform button.
I cannot use the canned button because I'm writing an application for the .net compact framework, so I need a custom control in order to make the UI more presentable. Also, I tested out my code on the full version of the .net framework, and I still have the same problem.
Any help would be greatly appreciated. Thank you!
If you are clicking rapidly enough, you are getting into DoubleClick territory.
According to above MSDN Page the order of events are:
The following series of events is raised by the control when such a user action takes place:
MouseDown event.
Click event.
MouseClick event.
MouseUp event.
MouseDown event.
DoubleClick event.
MouseDoubleClick event.
MouseUp event
If you will notice there is only one Click event per DoubleClick
For a way to disable it try looking at this MSDN Page discussing ControlStyles.
From above link:
StandardClick -- If true, the control implements the standard Click behavior.
StandardDoubleClick -- If true, the control implements the standard DoubleClick behavior. This style is ignored if the StandardClick bit is not set to true.
So try this in your controls constructor or load event:
this.SetStyle(ControlStyles.StandardClick, true );
this.SetStyle(ControlStyles.StandardDoubleClick, false);
Since SetStyle does not appear to be in the Compact Framework you could add a DoublClick Event and have it trigger the Click event Programmically like this.
YourClickEvent(sender, new MouseEventArgs(System.Windows.Forms.MouseButtons.Left,1,0,0,0));
When you click your control fast enough, it calls double click rather than click.
So, you should do something like this in your click function:
{
control.Enabled = false;
......
control.Enabled = true;
}
I was wonder how can I fire an event when the user double click on my webbrowser component. Since it has no such event how it could be possible...
Thanks in advance
Sounds like a WPF matter :-)
There you would go with an Behaviour attached to the browser. See this link for more information about this approach if you can alter your application (dependends on what you have done yet).
If you can't apply this solution, just bind a event handler to the click event and count click per time with respect to the mouse movement since the last click and if both conditions are true (two clicks in 0.2 secs, mouse hasn't moved more than 2px, for example) execute your double click code. The events you should use are previewMOUSEdown or MOUSEdown, not KEYdown.
// Call this where you want to create the event (let's say on the form load for example).
webBrowser1.DoubleClick += new EventHandler(webBrowser1_DoubleClick);
// This happens when the event is fired (so when you double click on the webbrowser control).
void webBrowser1_DoubleClick(object sender, EventArgs e)
{
// Code
}
Try this.
I don't know why you can't set this event via the designer :(, but this should work.
I have a textbox in a groupbox, both with double click events. When I double click in the textbox both events are triggered.
How do I stop clicks in the textbox from passing through to the groupbox? I've tried putting "e.Handled = true;" at the end of the textbox_DoubleClick event but this makes no difference.
Because WPF uses a "tunneling / bubbling" model of event propagation, most events begin bubbling UP from the bottom of the visual tree. If you want to catch an event on the way down, there are Preview versions of the events that tunnel downwards. For example:
PreviewMouseDoubleClick
Set e.Handled = true in there.
In your GroupBox's DoubleClick event you could check the value of e.OriginalSource and if that value is not the GroupBox, ignore the event
private void TabItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (e.OriginalSource is GroupBox)
{
// Your code here
}
}
I believe ClickEvents are actually Direct Events, and not Tunneled/Bubbled events, so setting e.Handled in one won't cancel the other.
Per MSDN Site for MouseDoubleClick
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.
you should handle e.Handled in the PreviewDoubleClick because tunneled events happens before bubbled up ones.
also why would you need to handle that event in both textbox and groupbox ? as it is getting fired in both because 2 separate events are getting fired.