Cannot keep OnClick event after visibilty change - c#

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;
}
}
}

Related

Prevent Windows firing events during long touch of a button

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.

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
}

ListView click event not triggered when clicking on empty areas

I have a ListView control on my form. I have set its display mode to LargeImageList. I need to handle the items inside this control. So I have written code for its click event. But I see now that this event is not triggered when I click in an empty area inside it.
How can I make my ListView aware of the clicks on its area regardless.
To capture mouse clicks on the "white space" around the ListView items, you will need to use the MouseDown/MouseUp events.
This will also capture clicks to the items as well.
I've used the Global Mouse Hook for similar issues. You can use it to detect Mouse Clicks anywhere on the screen, then just check the click was within the listview control bounds.
Grab the code from Global Mouse Key Hook
IKeyboardMouseEvents m_GolbalHook = Hook.GlobalEvents();
m_GolbalHook.MouseClick += m_GolbalHook_MouseClick;
private void m_GolbalHook_MouseClick(object sender, MouseEventArgs e)
{
if (listView.Bounds.Contains(e.Location)) && (e.Button == System.Windows.Forms.MouseButtons.Left))
{
//Do Stuff
}
}

Button inheritance onClick behaviour

I have a class which extends the Button-class. Why I do this, is because I want to use more than one label on the button.
The button works, and I've handled clicks on the label:
private void ClickOnLabel(object sender, EventArgs e)
{
base.OnClick(e);
}
Problem is, if I click and hold on a button the button reaches this "button pressed-state". And if I do the same on the label (which lies ontop of the button), it doesn't. The events that are using the method above is Click and DoubleClick. What am I missing?
Problem was I had to inheritate both MouseDown and MouseUp (along with Click) for get that behaviour.

Duplication Controls Winforms

I have such a situation here.
Let's say we have three button on our Form, and one Control, which is Panel, and which is hidden when the Form is loaded. Here is it
When I click on Button 1, that Panel have to be shown under Button1, when clicking on Button2, it have to be showned under Button2 and so on. Let's say I clicked on Button2.
Now I want that same Panel to show, when Clicking on Button1 and not at the same place. I need the same panel to be showed under buttons when clicking them.For example, the same panel is showing when clicking button 3
I made this only working for only one button. I can't have 2 controls with same attributes, but I need somehow to duplicate that control.I And I think it have to be done with UserControl.
private void btn_click(Control sender, EventArgs e)
{
var btn = sender as Button;
panel1.Left = btn.Left;
}
now assign this even handler to click event for all buttons.
The var btn.... line will represent the button that was clicked, or the control that triggered the event, so from there you can set the panel's location.

Categories