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.
Related
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;
}
}
}
I have a class which I inherit from Button. In this class, I create labels which I can use as custom button captions. The problem I have on these custom buttons is when I click the label nothing happens, and if I click outside the label but still on the button it works as usual. To solve this I wrote:
lblTitle.Click += new EventHandler(LabelClick);
And in LabelClick I wrote:
private void LabelClick(object sender, EventArgs e)
{
base.OnClick(e);
}
This worked good, but then I noticed that I didnt get the clickanimation of the click, when I clicked on the label. So I made the same thing with onMouseDown and Up.
lblTitle.MouseDown += new MouseEventHandler(LabelMouseDown);
lblTitle.MouseUp += new MouseEventHandler(LabelMouseUp);
private void LabelMouseDown(object sender, MouseEventArgs e)
{
base.OnMouseDown(e);
}
private void LabelMouseUp(object sender, MouseEventArgs e)
{
base.OnMouseUp(e);
}
But then I sometimes get the effect of clicking two times on the button. So now I'm wondering, how do I give the user the same click animation when clicked on the button and the label ontop of the button - without making the button click twice?
Thanks in advance and if you gonna downgrade this question, please leave a comment on why!
I think that the problem is that LabelClick & LabelMouseDown & LabelMouseUp all these three events are fired when you click the label which fires them in the base button. so your click event is duplicated when you make a MouseDown event. (this should be a comment, but i dont have enough reputation to leave a comment, hope it will help :) )
I find a strange bug in windows 2012. I have a simple window (WinForm) with a text box and a button (textBox1 and button1).And I try to focus on textbox1, after form appear.
private void Find_Paint(object sender, PaintEventArgs e)
{
textBox1.Focus();
}
And if I set it Click and MouseClick events stop working. So I can't click on button.
In windows 2008 it's work. If comment focus line - works too.
Who can suggest a solution or perhaps an alternative? Need to get the cursor in the textbox after the form has appeared
You should use the Shown event instead:
private void Find_Shown(object sender, EventArgs e){
textBox1.Focus();
}
Note: you used Paint event which will be very nasty, everytime your form is repainted, your textBox1 will be focused, the Paint event is fired every time your form resizes, state changes, ... we can't determine exactly the time it fires but it fires fairly frequently when your form is running. That is the reason why you can't click on and select anything on your form. That's because clicking or selecting controls fires the Paint event and makes your textBox1 focused then.
I developping one win-form application which having one custom control with one label and text box, and placed the custom control in one panel with docksytle as fill,
there is mouse click event for panel and custom control both, but when i click only custom control mouse click event is firing not the panel click event,
so anyone please let me know how to call the panel mouse click event.
Are you sure that you really need to invoke click of parent control? In general it would be, in my opinion, a code smell if you will do something like that - especially when it requires some strange constructions.
If you need to react in a same way when clicking on panel and on any child control inside the panel, it should be enough just to call the same method from two event handlers (that is from event handler of parent panel and event handler of child control. If you need, for example, mouse pointer location inside parent panel, you can easily calculate the position of mouse pointer using, for example, PointToScreen() and PointToClient() methods.
This is not a general solution, but maybe it's what you're looking for:
private void CustomControl_MouseClick(object sender, MouseEventArgs e)
{
panel_MouseClick(sender, e);
}
private void panel_MouseClick(object sender, MouseEventArgs e)
{
}
Create Click Event for each control in panel and invoke the parent :
private void This_Click(object sender, EventArgs e)
{
this.InvokeOnClick(this, null);
}
I'm currently developing a custom control and realize that my code is being run twice. It is not really a huge issue (it is only a Focus method call). However, I would like to understand it.
From reading the MSDN description for click | onclick event, it states that:
Fires when the user clicks the left mouse button on the object.
So I added the OnClick event and the MouseClick events to handle both left and right clicking. But after debugging the code I found that the OnClick handles both left and right click events.
Why is OnClick handling both and do I need to keep both events in my code for some reason I'm overlooking?
protected override void OnClick(EventArgs e)
{
this.Focus();
base.OnClick(e);
}
private void CustomControl_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
rightClickMenu(e);
}
}
According to MSDN, the Click event is called not only when the mouse is clicked, but also when the Enter button is pressed. If you only need to handle mouse clicks, I'd move all of your code in the MouseClick event. You can't do it the other way around because the Click event doesn't tell you which mouse button (if any) was clicked.
First of all, your link is incorrect, it links to HTML and DHTML Reference, not WinForms :)
Correct link is Control.MouseClick event
You need to override only one method. If you want to handle only mouse clicks - override OnMouseClick() and don't handle MouseClick event, otherwise - override OnClick() and don't override OnMouseClick().
You shouldn't need to have both events... Just keep the OnClick.
Also, I haven't done Windows Forms in quite a while, but I think there's a better way to accept focus than manually setting it on the click event, but I can't tell you specifically what it is... I think there's a property for it or something.
In Winforms, the Click event is raised when either mouse key is clicked.
If my memory serves me right, click does both mouseclick and the 'Enter' key or even setting focus on the control using the 'Tab' key and then using 'Space' or 'Enter' to "click" it.
If such behaviour is acceptable/desired, you may do the following.
I had this workaround for a DoubleClick event...
void ControlClick(object sender, EventArgs e)
{
MouseEventArgs mEvt=e as MouseEventArgs; // or (MouseEventArgs)e;
// now mEvt has the same properties as 'e' in MouseClick event
}
Hope this helps.
-Nurchi
The OnClick and CustomControl_MouseClick is the same event
You can have how many methods you want attached to an event ( this.Click += ...)