Show/Hide button on MouseOver - c#

I have a button on my WinForms app that I want to be invisible until the user moves his mouse over the button. Then they could click it. If the mouse leaves the button, it needs to be hidden again. The button.Visible parameter makes the button completely inaccessible and disables the mouse over. Any ideas or other button parameters I could use?
This currently does not work:
private void settingButton_MouseEnter(object sender, EventArgs e)
{
settingButton.Visible = true;
}
private void settingButton_MouseLeave(object sender, EventArgs e)
{
settingButton.Visible = false;
}

This issue was brought up and answered here:
C# WinForms MouseHover and MouseLeave problem
private void Form_MouseMove(object sender, MouseEventArgs e) {
if(settingButton.Bounds.Contains(e.Location) && !settingButton.Visible) {
settingButton.Show();
}
}

Related

How to not confuse the mouse click and mouse down event in windows forms? [duplicate]

This question already has answers here:
Why doesn't doubleclick event fire after mouseDown event on same element fires?
(2 answers)
Closed 5 years ago.
I have a MenuStrip from which I want to drag some things on the form body(then some things happen such as the backcolor of the form is changing, etc). I am handling the MouseDown event, but the thing is that when I click on the option in the ToolStripMenu the same things happen(the backcolor of the form is changing, etc).
What I want is to somehow separate the MouseClick from the MouseDown. More precisely, when I click on one option of the MenuStrip I don't want anything to happen.
When I click, the MouseDown event fires. I want to ignore it unless the mouse's cursor moves.
private void salmonToolStripMenuItem_MouseDown(object sender, MouseEventArgs e)
{
//gets the cursor position at the moment when mouse down is activated
p1M = Cursor.Position.X;
p2M = Cursor.Position.Y;
//miscare shows if the mouse moved
if (miscare == true)
{
//do things
}
}
private void Rezervare1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void salmonToolStripMenuItem_MouseUp(object sender, MouseEventArgs e)
{
//gets the cursor position to see if it moved
p1m = Cursor.Position.X;
p2m = Cursor.Position.Y;
if (p1M != p1m || p2M != p2m)
{
miscare = true;//it means the cursor moved
}
else miscare = false;
}
If you want your logic to happen on mouse move, then lets handle MouseMove!
bool isMouseDown = false;
private void salmonToolStripMenuItem_MouseDown(object sender, MouseEventArgs e)
{
isMouseDown = true;
}
private void salmonToolStripMenuItem_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}
private void salmonToolStripMenuItem_MouseMove(object sender, MouseEventArgs e)
{
if(isMouseDown)
{
//Do your thing
}
}
Also note, there are some strange edge cases you can hit with things like the form losing focus, or the user dragging outside of the bounds of the form. Something to be aware of and handle as needed.

C# WinForms: bring back focus to form after button click

my simple WinForms app consists of one Form which has 2 important methods:
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Q)
{
qKeyPressed = true;
}
keyTimer.Start();
}
private void MainWindow_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Q)
{
qKeyPressed = false;
}
keyTimer.Stop();
}
In form's constructor I use button1.TabStop = false; to set button1 focus to false, so my form can handle methods KeyUp and KeyDown. I want my form to be focused even after button1 click event. I tried:
button1.TabStop = false;
this.Focus();
in private void button1_Click(object sender, EventArgs e) but this doesn't seem to work and button after click looks like focused and methods KeyUp and KeyDown are disabled. How can my problem be solved? Thanks in advance
Run this in the button click function:
this.ActiveControl = null;
When you click the button, you set it as the active control. Removing this property will let you put focus on your form.

Hover over: Button Change using Flatyle properties C#

Im working in WinForms I have 4 buttons on my form. I want to be able to hover my mouse over it and change the FlatStyle from Flat to System.
My code transforms all the buttons to System Style when you hover your mouse over it, that's not exactly what i had in mind.
All the buttons should remain flat until you hover over them. If you hover off the button it should turn back into flat button
private void All_Button_Hover_MouseHover(object sender, EventArgs e)
{
btn_Back.FlatStyle = FlatStyle.System;
Btn_Forward.FlatStyle = FlatStyle.System;
btn_Print.FlatStyle = FlatStyle.System;
btn_Open.FlatStyle = FlatStyle.System;
}
Here's a suggestion of how you could handle this.
You're already setting all the buttons in a single event method, which is fine. Since the button that triggered the event is stored in sender, you could just use that:
private void All_Button_Hover_MouseHover(object sender, EventArgs e)
{
((Button)sender).FlatStyle = FlatStyle.System;
}
To change the buttons back to the original FlatStyle.Flat style, you'll probably want to subscribe all of their MouseLeave events to a method as well:
private void All_Button_Hover_MouseLeave(object sender, EventArgs e)
{
((Button)sender).FlatStyle = FlatStyle.Flat;
}

How to detect a MenuStrip's open and close states ?

I need to detect whenever the MenuStrip (NOT a contextmenu) is opened and closed. The Enter and Leave events do not seem to work.
Why I need it:
I use a MenuStrip with options like "File", "Edit", "About", etc. But when the menu strip is active, and the user navigates through it using either the mouse or keyboard the other controls on the Windows Form also respond to it.
For example: I click the "Edit > Paste Special.. > Unformatted text" in my application using the mouse. But below the expanded menu item is a XNA 2d-rendering control that also detects the mouse input and does something that I don't want it to do. Now if I could just detect whenever the menu is opened I could disable/enable the appropriate controls.
Without knowing a little more about what you are doing, you can try the code below:
private void Form1_Load(object sender, EventArgs e)
{
menuStrip1.GotFocus += new EventHandler(MenuStrip1_GotFocus);
menuStrip1.LostFocus += new EventHandler(MenuStrip1_LostFocus);
}
private void MenuStrip1_GotFocus(object sender, EventArgs e)
{
textBox1.Text = "Has Focus";
}
private void MenuStrip1_LostFocus(object sender, EventArgs e)
{
textBox1.Text = "Lost Focus";
}
private void menuStrip1_MenuActivate(object sender, EventArgs e)
{
textBox1.Text = "Has Focus";
}
private void menuStrip1_MenuDeactivate(object sender, EventArgs e)
{
textBox1.Text = "Lost Focus";
}
This seems to be working for what you have described above. If you have the menu strip with tab stop on (true) then the got focus events will handle this case. If you only need to make changes if the mouse is used then Menu Active events should work.

Expand collpase splitcontainer in win form c#

i am working with split container. my split container has two panel and horizontal orientation. in first panel there are some textboxes and one button. when i click on button then a code run to collapse Panel1 of split container. code is like
private void button1_Click(object sender, EventArgs e)
{
splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;
}
my problem is when collapse occur then my button and all the textboxes getting invisible. so i next time not being able to make those control visible. so i want trick like button will not be invisible as a result i can click on that button again to make panel1 visible. if possible guide me how to fix or place my button on splitter rather on panel. so guide me how can i do it.
private void button1_Click(object sender, EventArgs e)
{
splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;
button1.Parent = splitContainer1.Panel1Collapsed ? splitContainer1.Panel2 : splitContainer1.Panel1;
}
Related to my previous comment on your entire posting. this is a small solution with a ToolBarButton. It will only be enabled if the SplitContainer.Panel1 is collapsed.
Code:
private void Form1_Load(object sender, EventArgs e)
{
splitContainer1.Panel1Collapsed = true;
toolStripButton1.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
splitContainer1.Panel1.Hide();
toolStripButton1.Enabled = true;
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (splitContainer1.Panel1Collapsed)
{
toolStripButton1.Enabled = false;
splitContainer1.Panel1.Show();
}
}

Categories

Resources