Prevent Windows firing events during long touch of a button - c#

WinForms application.
User touches a button (touchscreen) that causes authentication and the screen (A) is changed to another screen (B). If user continues to hold the finger on the screen (just for a second) the screen B has a button that overlaps with the button on screen (A) and the button on screen B inadvertently gets a touch and invokes an action that is not supposed to happen...
How do I do to prevent this from happening?

That is weird behavior. I do not have a touch device to test this but in a regular scenario the click only triggers when the button is pressed and then released while the button is still in focus. If you press the button and do not release, the click event will not fire.
Anyhow perhaps what you can do is only show screen B when the button has been pressed and then it has lost focus. Like this:
private bool buttonIsClicked = false;
private void button1_Click(object sender, EventArgs e)
{
this.buttonIsClicked = true;
}
private void button1_Leave(object sender, EventArgs e)
{
if (this.buttonIsClicked)
{
this.buttonIsClicked = !this.buttonIsClicked;
// show screen B
}
}
Please use a variable like I have used buttonIsClicked so the code in Leave event handler is not executed every time the button loses focus even if it is not clicked.

Related

Cannot keep OnClick event after visibilty change

Just wondering if someone else can recreate this or is it just me :)
Drop a panel on the form
Put a button on the panel
Create click of button "I am Button"
Above works fine
Add onmouseleave to panel set button visible to false
Add onmouseenter to panel set button visible to true
Now if you move mouse in and out of panel the button will become visible false. Pefect.
Now move mouse back intp panel and button becomes visible. Perfect.
Clicking on button now does nothing (Lost on Click Event?)
Breakpoint in onclick never fires.
Is this a concept that cannot be done?
'Update' It wont let me post long comment.
First of all thanks for the quick responses.
I did not include code because this was in a very large project so i recreated it with a brand new app.
C# WinForm with one panel and one button on the panel.
I did not try to click on an invible button. Not only can i see the button but can see it respond to bing clicked.
#steve. Good point, The button is well inside of the panel but i see where you are going. I just performed a new test.
If i move the mouse in and out of the panel the button will show and hide perfectly.
After this, hitting enter on the button will excute onclick but the mouse will not. (I suppose this has something to do with only one button and it is default.)
What prompts the mouse to not fire the click event is beyond me.
Very Strange.
What i am trying to achieve:
An area on the screen that when the mouse enters this area a group of buttons appear so they can be used.
when the mouse leaves this area the buttons will dissapear. As my original post stated button works fine if visiblity does not change.
I suppose the your code has the following problem:
You receive the mouseenter event on the panel and you make the button
visible
You move the mouse over the button
This last action triggers the mouseleave event on the panel.
As a consequence your button becomes invisible.
But making the button invisible triggers a mouseenter over the panel and in a blink the button is again visible and causing a mouseleave on the panel.
An infinite loop between these two events starts.
So the click event is not lost, simply it will never trigger because the form engine is busy hiding and showing the button control.
I have reproduced the situation and fixed it adding an event handler for the MouseMove event. In this event I set a variable to block the infinite loop between the two events...
This is my example to adapt to your real code, try it in LinqPad
public class Form1 : Form
{
Button b1 = new Button();
Panel p1 = new Panel();
bool stillOnPanel = false;
public Form1()
{
b1.Text = "ClickMe";
b1.Click += onClick;
b1.Visible = false;
p1.MouseEnter += onEnter;
p1.MouseLeave += onLeave;
p1.MouseMove += onMove;
p1.BorderStyle = BorderStyle.FixedSingle;
p1.Size = new Size(150,100);
this.Controls.Add(p1);
p1.Controls.Add(b1);
}
void onClick(object sender, EventArgs e)
{
Console.WriteLine("Clicked");
}
void onMove(object sender, MouseEventArgs e)
{
//Check if we are still over the panel area
stillOnPanel = b1.ClientRectangle.Contains(p1.PointToClient(Cursor.Position));
}
void onEnter(object sender, EventArgs e)
{
if (!b1.Visible)
{
b1.Visible = true;
}
}
void onLeave(object sender, EventArgs e)
{
// Do not hide the button if we are over it, let it click....
if (b1.Visible && !stillOnPanel)
{
b1.Visible = false;
}
}
}

Using Mouse.Capture to detect a click outside of a given control

I have a TextBox in a WPF project where I am trying to detect a mouse click anywhere on the Application other than in the TextBox.
Here is the code that I have so far.
System.Windows.Input.MouseButtonEventHandler clickOutsideHandler;
public MyClass() {
clickOutsideHandler = new System.Windows.Input.MouseButtonEventHandler(HandleClickOutsideOfControl);
}
private void StartCapture() {
System.Windows.Input.Mouse.Capture(TextBox1, System.Windows.Input.CaptureMode.SubTree);
AddHandler(System.Windows.Input.Mouse.PreviewMouseDownOutsideCapturedElementEvent, clickOutsideHandler, true);
}
private void HandleClickOutsideOfControl(object sender, System.Windows.Input.MouseButtonEventArgs e) {
ReleaseMouseCapture();
RemoveHandler(System.Windows.Input.Mouse.PreviewMouseDownOutsideCapturedElementEvent, clickOutsideHandler);
}
The problem I'm having is that the event handler never gets called. I've tried capturing the return value of the Capture() function, but it shows as true. Can anyone show me what I'm doing wrong?
You could instead use LostFocus / LostKeyboardFocus but there has to be another element on the window that can get focus.
Second approach that does more what you what exactly ( yet doesn't make total sense) would be to attach to the global mouse down. Intercept every mouse click to WPF application
then on that one do a hittest and determine what is underneath.https://msdn.microsoft.com/en-us/library/ms608753(v=vs.110).aspx

Click event not working on dynamically added button or Panel

I am using following code in C# to add a Button
Button TextLabel = new Button(); //local variable
TextLabel.Location = new Point(0, 0);
TextLabel.Visible = true;
TextLabel.Enabled = true;
TextLabel.AutoSize = true;
TextLabel.Click += click;
this.Controls.Add(TextLabel);
And its click handler is
protected void click(object o, EventArgs e)
{
MessageBox.Show("hello");
}
Though the Button is visible and responding to mouse hover, but nothing is happening on its click. What could be wrong or missing?
If I write this same code in an independent project, it works!!!!! Strange. but why????
Form Properties: (if required)
1. Show in taskbar: false
2. Borderless
3. 50% Opaque
Today I realised that just registering click event for a control will not make any event to work unless its parent (in my case its form) on which that control is still active.
Parent control will receive event notification earlier than its child controls. This is a simple and obvious observation, but if not paid attention will make undesirable effects.
That's the mistake I did, I made another form active on my form activated event, hence any control in it didn't received events like mouse clicks.
Talking of 'hover effects are working', then yes, even if a form is inactive, hover works.
So I just removed the line of code that made another form active and everything is working fine now.
private void Form1_Activated(object sender, EventArgs e)
{
//if (form2!=null) form2.BringToFront(); //commented this
}

C# Windows Form Application Long Button Click

Well, so first of all I am pretty new to programming in Windows Form Application, so if I lack knowledge I hope you'll understand.
The program I am currently doing is a virtual piano in Windows Form Application. There are buttons on the screen, each button represent a piano "button", if one is clicked - it plays a sound. Now, what I wanted to know is if there is any way that I can program my piano into detecting a continuous click and play sound until the button is "unclicked". For example, if one keeps on clicking on the "G" chord button it will keep playing the sound until he stops clicking.
If I didn't provide any necessary information I would love to know. Thanks in advance to all the answers.
The thing you are looking for is "hold button event".
You could handle the MouseDown and MouseUp events, something like this:
private bool buttonDown;
private void btn1_MouseDown(object sender, MouseEventArgs e)
{
buttonDown = true;
int num = 0;
do
{
num++;
label1.Text = num.ToString();
Application.DoEvents();
} while (buttonDown);
}
private void btn1_MouseUp(object sender, MouseEventArgs e)
{
buttonDown = false;
}
This will starts executing the code when you click the button and keeps executing it until you release the button
You need to use mousedown event. When mouse will enter the button the you need to raise the button click event unit the mouseup event fire.

VirtualKey C# Windows Store App

I'm writing a Windows Store App for Windows 8. I have two buttons on the screen that I'd like to toggle with the space bar: when the start button in pressed I disable it and enable the stop button, and visa versa.
I can start and stop using the spacebar, but I have a problem after I click any button with the mouse: then the focus is on a different button, which I then accidentally press with the space bar.
I want the focus to be on the stop button after I clicked the start button, so I can press the stop button with the space bar. How should I do that?
public void Grid_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Space)
{
if(!isPlaying)
{
StartButton_Click(sender, null);
}
else
{
StopButton_Click(sender, null);
}
}
}
Regards
In StartButton_Click call
StopButton.Focus(Focus.Programmatic);

Categories