Add a right click event to a tab in a TabControl container - c#

In a form I have a tab container in which I dynamically add tabs through the use of a button. As there is no easy way that I know of (then again I'm a WinForms newbie) to close the selected tab, I'd like to set up an event handler for handling a right click through which the tab will close. In simple words I want to right-click on the selected tab in order to close it.
This is the event handler which I have written (yet doesn't work):
private void tab_Click(object sender, EventArgs e)
{
MouseEventArgs me = (MouseEventArgs)e;
if (sender == tabControl1.SelectedTab && me.Button == MouseButtons.Right)
{
tabControl1.TabPages.Remove(tabControl1.SelectedTab);
}
}
I guess it's too naive of an approach? The handler does not even register the right click when I click on the tab. Any suggestions how to make this work?

private void tabControl1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Application.Exit();
}
else
{
}
}

You've got very iffy code here. Don't blindly cast an EventArgs object, simply use the MouseClick event instead. Don't blindly hope that the SelectedTab is the one that was clicked, that happen later. And never, never use the Remove() method like that, it is super-duper important to dispose the TabPage and its controls. If you don't then those controls will permanently leak, the kind of bug that eventually crashes your program with an undebuggable exception like "Error creating window".
Make it look like this instead:
private void tabControl1_MouseClick(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
for (int ix = 0; ix < tabControl1.TabCount; ++ix) {
if (tabControl1.GetTabRect(ix).Contains(e.Location)) {
tabControl1.TabPages[ix].Dispose();
break;
}
}
}
}

Related

C# Form - MouseClick will click the actual control after losing form focus

If you ever remove focus from any professional application like Chrome/FireFox/Visual Studio, and then reclick a button/menu item, it will actually click it as if you never lost focus.
How can I apply the same concept in C# WinForm? I tried many things like
private void form1_MouseClick(object sender, MouseEventArgs e)
{
BringToFront();
Activate();
}
Activate/focus/select/etc... nothing worked to react the same way, it always takes 3-4 clicks to actually click on a menu!
I thought about making a click event for every single control, but that seemed rather redundant.
Check this for example (Yellow Clicks)
You are right about Menues taking an extra click to get focus.
Which is extra annoying since the menue get highlighted anyway but doesn't react to the 1st click..
You can avoid that by coding the MouseEnter event:
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
// either
menuStrip1.Focus();
// or
this.Focus();
}
The downside of this is, that it is stealing focus from other applications, which is not something a well-behaved application should do..
So I think it is better to wait for a definitive user action; code the MouseDown event in a similar way..:
private void menuStrip1_MouseDown(object sender, MouseEventArgs e)
{
menuStrip1.Focus();
}
Or use the event that was made for the occasion:
private void menuStrip1_MenuActivate(object sender, EventArgs e)
{
menuStrip1.Focus();
}
I can't confirm a similar problem with Buttons or any other controls, though.
I have find trick to solve your problem. it work for me 100%
See this code:
dynamic elem1;
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
elem1 = sender;
}
private void menuStrip1_MouseLeave(object sender, EventArgs e)
{
elem1 = null;
}
private void Form1_Activated(object sender, EventArgs e)
{
if(elem1 != null){
elem1.PerformClick();
if (elem1.GetType().ToString() == "System.Windows.Forms.ToolStripMenuItem") elem1.ShowDropDown();
}
elem1 = null;
}
Here what happend.
When mouse enter button/menu item elem1 = this button/menu, and when mouse leave it set back to null.
so when form Activated we can call elem1.PerformClick() to click the button/menu item.

Disable right click in Win form app

Hii I am making a windows Form application
I added a user control in that application
Is there a way to disable the right click event.
because if I use
Button.click += SomeMethod
It is going to someMethod in case of both (left and right) clicks.
I want to do this action in case of left click only.
Can't you handle which button has been clicked inside of the event?
private void uc_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
//more logic here
}
}
You can detect which button of mouse was clicked inside event handler.
Check MouseEventArgs e argument of handler
You can check the button and call the native onClick for cases of left-button click.
[pseudo code]
SomeMethod(e){
if (e.Button == left){
control.onClick(e);
}
}
you could do number of ways;
1) if you want to use your button.click event then do the code below:
Button.Click += SomeMethod;
private void SomeMethod(object sender, EventArgs e)
{
System.Windows.Forms.Button bt = sender as System.Windows.Forms.Button;
if (bt != null)
{
if (bt.Equals(System.Windows.Forms.MouseButtons.Left))
{
// do something;
}
}
}
2) Or use the mouse clicked event in windows form "button.MouseClick".
Button.MouseClick += SomeMethod;
private void SomeMethod(object sender, MouseEventArgs e)
{
if (!e.Button.Equals(System.Windows.Forms.MouseButtons.Right))
{
// do work
}
}
I was hoping that we could use the e.Handled = true, but in this case neither of the event argument has the Handled property, therefore you have to manually check them.

Listbox events firing strangely

I'm confused. I am basically trying to tell when the user has clicked something in the listbox, held the button, and left the listbox. Here is a somewhat dumbed down version of what I am doing:
private bool itemHeld;
private void listOriginal_MouseDown(object sender, MouseEventArgs e)
{
itemHeld = true;
}
private void listOriginal_MouseUp(object sender, MouseEventArgs e)
{
itemHeld = false;
}
private void listOriginal_MouseLeave(object sender, EventArgs e)
{
if (itemHeld)
MessageBox.Show("OHH YEAH");
}
To me that seems like it should turn itemHeld true when you press the mousebutton, turn it false only if you lift it, and display ohh yeah if the value is true. If I break on the mouse down event to check the value, it is true and if I continue from there it displays the message. If I do not break, it does nothing. Is there something else at work here?
Edit:
Brief description: It would be difficult to explain what I am really trying to accomplish but imagine something almost like dragging a file off of a window. I need to simply be able to recognize when the user clicks inside of the listbox and then drags out of the listbox if that makes sense
You can not debug windows events by break point because when the Visual Studio get active to debug, the mouse leave event will be fired for the hovered control.
You can use Debug.WriteLine which writes information about the debug to the trace listeners.
private void button1_MouseLeave(object sender, EventArgs e)
{
Debug.WriteLine("Mouse leave");
}
private void button1_MouseEnter(object sender, EventArgs e)
{
Debug.WriteLine("Mouse enter");
}
private void button1_MouseHover(object sender, EventArgs e)
{
Debug.WriteLine("Mouse hover");
}
what about this?
private void listBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.X > listBox1.Width - 1 || e.Y > listBox1.Height - 1 || e.X < 0 || e.Y < 0)
{
Console.WriteLine("drag out");
}
else
Console.WriteLine("mouse move {0}/{1}", e.X, e.Y);
}
it uses the fact that the Control is not left before the mousebutton is released ... but be aware that the drag out part will occur more than once so you probably will want to have a flag set the first time ... and have that flag cleared on mouse up or leave
For every mouse click, your MouseDown event will fire AND your MouseUp event will fire, so the sequence of operations is equivalent to
itemHeld = true;
itemHeld = false;
if(itemHeld)
MessageBox.Show("yay");
If you press the mouse button on the listbox and move the cursor out without releasing the button, switching focus to another window (e.g. Visual Studio) is what triggers the MouseLeave event to fire. This is why you're seeing the message box pop up when you're debugging.
I'm not sure what you're trying to accomplish, so I can't recommend another solution.

System Tray notifyIcon won't accept left-click event

I'm creating a System-Tray only application. It's somewhat complicated to have the icon without a main form, but through previous topics on StackOverflow I've worked it out. The right-click works fine, I've linked in a context menu, etc.
I'm having problems with the left-click. As far as I can tell, the "notifyIcon1_Click" event isn't firing at all.
private void notifyIcon1_Click(object sender, EventArgs e)
{
Debug.WriteLine("Does it work here?");
if (e.Equals(MouseButtons.Left))
{
Debug.WriteLine("It worked!");
}
}
Neither of those debug lines are outputting, breakpoints in that event don't stop the program, etc.
Am I doing this incorrectly? What should my next step be? I'm coding this in C#, using Windows 7 if that matters at all for taskbar behavior.
If you want to determine if it's a left or right click, wire up the MouseClick, rather than click.
That way you get a signature like this:
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
//Do the awesome left clickness
else if (e.Button == MouseButtons.Right)
//Do the wickedy right clickness
else
//Some other button from the enum :)
}
If you want the click event of the Message/Balloon itself use
_notifyIcon.BalloonTipClicked += notifyIconBalloon_Click;
private void notifyIconBalloon_Click(object sender, EventArgs e)
{
// your code
}
private void NotifyIcon_Click(object sender, EventArgs e)
{
MouseEventArgs mouseEventArgs = (MouseEventArgs)e;
if (mouseEventArgs.Button == MouseButtons.Right && IsHandleCreated)
{
popupMenu1.ShowPopup(MousePosition);
return;
}
if (mouseEventArgs.Button == MouseButtons.Left && IsHandleCreated)
{
if (isWindowMinimized)
showWindow();
else
hideWindow();
}
}
The other answer is not clear that you need MouseClick event instead of Click.
notifyIcon.MouseClick += MyClickHandler;
Then your handler function will work fine.
void MyClickHandler(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Console.WriteLine("Left click!");
}
}

How can I determine which mouse button raised the click event in WPF?

I have a button that I trigger OnClick whenever there is a click on that button. I would like to know which Mouse button clicked on that button?
When I use the Mouse.LeftButton or Mouse.RightButton, both tell me "realsed" which is their states after the click.
I just want to know which one clicked on my button. If I change EventArgs to MouseEventArgs, I receive errors.
XAML: <Button Name="myButton" Click="OnClick">
private void OnClick(object sender, EventArgs e)
{
//do certain thing.
}
You can cast like below:
MouseEventArgs myArgs = (MouseEventArgs) e;
And then get the information with:
if (myArgs.Button == System.Windows.Forms.MouseButtons.Left)
{
// do sth
}
The solution works in VS2013 and you do not have to use MouseClick event anymore ;)
If you're just using the Button's Click event, then the only mouse button that will fire it is the primary mouse button.
If you still need to know specifically whether it was the left or right button, then you can use the SystemInformation to obtain it.
void OnClick(object sender, RoutedEventArgs e)
{
if (SystemParameters.SwapButtons) // Or use SystemInformation.MouseButtonsSwapped
{
// It's the right button.
}
else
{
// It's the standard left button.
}
}
Edit: The WPF equivalent to SystemInformation is SystemParameters, which can be used instead. Though you can include System.Windows.Forms as a reference to obtain the SystemInformation without adversely effecting the application in any way.
You're right, Jose, it's with MouseClick event. But you must add a little delegate:
this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyMouseDouwn);
And use this method in your form:
private void MyMouseDouwn(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
this.Text = "Right";
if (e.Button == MouseButtons.Left)
this.Text = "Left";
}

Categories