In my main application window, there are controls, each of which opens a popup that presents more controls to the user.
Other controls in the main application window have mousedoubleclick event handlers. My problem is that when the a user double clicks in the popup, the controls behind the popup are receiving the mousedoubleclick events.
I've tried added a mousedoubleclick event handler to the popup's parent, and handling the event, but it still gets through to the main application window.
private void ParentControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
I've also tried invoking Popup.CaptureMouse() in a MouseEnter event handler in the popup, but the method always fails (returns false).
void popup_MouseEnter(object sender, MouseEventArgs e)
{
e.Handled = true;
Popup popup = sender as Popup;
bool success = popup.CaptureMouse();
}
Are there any other ways to prevent the mouse events from firing in the main application window when the popup is open?
Easy! Instead of using the control's MouseDoubleClick event
private void myControl_MouseDoubleClick(System.Object sender, System.Windows.Input.MouseButtonEventArgs e)
{
MessageBox.Show("MouseDoubleClick on control");
}
use the PreviewMouseDoubleClick event.
private void myControl_PreviewMouseDoubleClick(System.Object sender, System.Windows.Input.MouseButtonEventArgs e)
{
MessageBox.Show("PreviewMouseDoubleClick on control");
}
Now double-clicking on your control will not also invoke the parent's DoubleClick event.
Related
In my WPF application I have a Grid with MouseDown, MouseUp and MouseMove events. I want the grid to disappear whenever I press the left mouse button, and reappear when I release it. The problem is that I don't get any mouse events while the grid is invisible (Visibility.Hidden).
This is the MouseDown handler:
private void TabHeaderOnMouseDown(object sender, MouseButtonEventArgs e)
{
tabHeader.CaptureMouse();
tabHeader.Visibility = Visibility.Hidden;
}
And the MouseUp handler:
private void TabHeaderOnMouseUp(object sender, MouseButtonEventArgs e)
{
tabHeader.ReleaseMouseCapture();
tabHeader.Visibility = Visibility.Visible;
}
Setting Opacity to 0 instead of changing the Visibility solved my problem.
I have a winforms app in vs2010 and a panel whose click event I would like to fire programatically. How do I do this? Button has a PerformClick but I cannot find the same in Panel.
Your panel's Click event is going to be attached to an event handler, right?
Then just call that event handler from the button's click event handler:
public void Panel1_Click(object sender, EventArgs e)
{
//Do whatever you need to do
}
public void Button1_Click(object sender, EventArgs e)
{
//Do anything you need to do first
Panel1_Click(Panel1, EventArgs.Empty);
}
The effect will be the same as clicking on the panel.
I have made a custom Number Keypad control that I want to place in my winform application. All of the buttons have an OnClick event to send a value to the focused textbox in my form where I have placed my custom control. Like this:
private void btnNum1_Click(object sender, EventArgs e)
{
if (focusedCtrl != null && focusedCtrl is TextBox)
{
focusedCtrl.Focus();
SendKeys.Send("1");
}
}
focusedCtrl is supposed to be set on the MouseDown event of the button like this:
private void btnNum1_MouseDown(object sender, EventArgs e)
{
focusedCtrl = this.ActiveControl;
}
where this.ActiveControl represents the active control on the form.
My problem is that the button always receives the focus before the event detects what the focused control was previously. How can I detect which control had the focus before the button got the focus? Is there another event I should be using? Thanks in advance!
EDIT: Also, I would rather not use the GotFocus event on each textbox in the form to set focusedCtrl since that can be tedious and because I would like to have all the coding of my custom control be in the control itself and not on the form where it is placed. (I will do this, though, if there is no other practical way to do what I am asking)
Your requirement is fairly unwise, you'll want some kind of guarantee that your button isn't going to poke text into inappropriate places. You really do need to have the form co-operate, only it knows what places are appropriate.
But it is not impossible, you can sniff at input events before they are dispatched to the control with the focus. In other words, record which control has the focus before the focusing event is fired. That's possible in Winforms with the IMessageFilter interface.
Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form, replacing your existing buttons.
using System;
using System.Windows.Forms;
class CalculatorButton : Button, IMessageFilter {
public string Digit { get; set; }
protected override void OnClick(EventArgs e) {
var box = lastFocused as TextBoxBase;
if (box != null) {
box.AppendText(this.Digit);
box.SelectionStart = box.Text.Length;
box.Focus();
}
base.OnClick(e);
}
protected override void OnHandleCreated(EventArgs e) {
if (!this.DesignMode) Application.AddMessageFilter(this);
base.OnHandleCreated(e);
}
protected override void OnHandleDestroyed(EventArgs e) {
Application.RemoveMessageFilter(this);
base.OnHandleDestroyed(e);
}
bool IMessageFilter.PreFilterMessage(ref Message m) {
var focused = this.FindForm().ActiveControl;
if (focused != null && focused.GetType() != this.GetType()) lastFocused = focused;
return false;
}
private Control lastFocused;
}
Control focusedCtrl;
//Enter event handler for all your TextBoxes
private void TextBoxesEnter(object sender, EventArgs e){
focusedCtrl = sender as TextBox;
}
//Click event handler for your btnNum1
private void btnNum1_Click(object sender, EventArgs e)
{
if (focusedCtrl != null){
focusedCtrl.Focus();
SendKeys.Send("1");
}
}
you have an event called lostFocus you can use
button1.LostFocus +=new EventHandler(dataGridView1_LostFocus);
and in the event:
Control lastFocused;
void dataGridView1_LostFocus(object sender, EventArgs e)
{
lastFocused = sender as Control;
}
in that way you can always know what is the Control that was focused previously
now, correct me if i'm wrong, but you do it for the SendKeys.Send("1"); to know which textBox need to receive the number. for that you can use GotFocus event and register only the textBoxs to it.
you can also do what windows is doing and use just one textbox like here:
if it's fits your needs
What about using this with the parameter forward = false?
Control.SelectNextControl Method
You'd probably call it on your "custom Number Keypad control".
I have created a custom XAML UserControl class that I pass to the ShowDialog method. I would like to be able to trigger clicking the OK button on the dialog through other events generated in my UserControl - for example double clicking a ListItem. I have the code to handle the double click just fine (tied into the MouseDown even and checked the click count) but I don't know how to trigger a new event to the parent dialog to close it.
// Bound to TextBlock, part of a ListBox on a UserControl
private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
// trigger dialog to close with OK button
}
}
Make your user control implement IDialogContent. You can then call directly to the CloseDialog event you implemented, and that will trigger the dialog closure.
You will need to decide how you want to handle that in your follow up code by setting some sort of state on your user control/view model or some other data as fits your particular extension.
// Bound to TextBlock, part of a ListBox on a UserControl
private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
CloseDialog(this, EventArgs.Empty);
}
}
In Silverlight 4, I wish to invoke one of the mouse button click events when the right mouse button is clicked. I have the RightMouseButtonDown click event wired up, but I don't know how to manually fire the MouseLeftButtonUp event.
I have tried raising the event in the following fashion.
private void MainLayoutRootMouseButton(object sender, MouseButtonEventArgs e)
{
MouseLeftButtonDown(sender, e);
}
However, a compiler error occurs:
"The event 'System.Windows.UIElement.MouseLeftButtonDown' can only appear on the left hand side of += or -=
What do I need to do to manually raise this event?
The other answers are correct in that they show you how to call those same code paths as you have for the other mouse events.
It should be clear that in Silverlight, you cannot raise (or automate) actual mouse button clicks, for security reasons.
Only user initiated actions such as the actual mouse moving can create real MouseEventArgs and fire the handlers directly, through the Silverlight input system.
You really shouldn't be raising the event itself. Instead, make the code inside the MouseLeftButtonUp its own function and then call that function from both the MouseLeftButtonUp and MouseRightButtonUp events.
e.g.
private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DoTheSameThing(sender, e);
}
private void MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
DoTheSameThing(sender, e);
}
private void DoTheSameThing(object sender, MouseButtonEventArgs e)
{
//Handle left or right mouse buttons up event
}
In your page's init function you need to have this line of code
someObjectOnPage.MouseLeftButtonDown += new MouseDownEventHandler(DoSomething);
someObjectOnPage.MouseRightButtonDown += new MouseDownEventHandler(DoSomething);
This is pure psuedo code but should lead you on the right track
also your method will need to look like this
void DoSomething(object sender, MouseButtonEventArgs e) { }