Right Clicking Action for Windows Form Buttons - c#

Some quick context: in this project we're using a Visual C# Windows Form project to recreate Minesweeper.
I am using an array of Cells (which inherit from Control.Button).
As extra credit, I want the user to be able to flag a cell like you can in the class version of the game. However, I can't get right-clicking to work.
When trying to find a solution, I read that you need to typecast the EventArg as a MouseEventArg, but that didn't solve my problem as right-clicking doesn't even trigger my click event.
Here's some paraphrased code:
namespace Project_5___Minesweeper_GUI
{
public partial class Form1 : Form
{
public class Cell : Button { /*Custom Cell-Stuff Goes Here*/ }
Cell[,] board = new Cell[AXIS_LENGTH, AXIS_LENGTH]; //Axis Length is just the dimensions of the board (I use 10x10).
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < AXIS_LENGTH; i++)
{
for (int j = 0; j < AXIS_LENGTH; j++)
{
board[i, j] = new Cell();
//Set position and size
board[i, j].MouseClick += button_arrayClick; //button_arrayClick() is never called by a right-click. Code for it is below. I suspect this line of code has to do with right-clicks not class button_arrayClick().
groupBox1.Controls.Add(board[i, j]); //I'm containing the array of Cells inside of a groupbox.
}
}
}
private void button_arrayClick(object sender, EventArgs e) //Is prepared for handling a right-click, but never receives them.
{
Cell temp = (Cell)sender;
MouseEventArgs me = (MouseEventArgs)e;
if (me.Button == MouseButtons.Left)
{
//Stuff that happens on left-click
} else {
//Stuff that happens on right-click
}
}
}
}
This is where I grabbed the type-casting the event arguments from.

MouseClick does not handle right clicks for buttons. You can use MouseDown.
board[i, j].MouseDown += button_arrayClick;

Use the _MouseDown event instead.
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
//stuff that happen on right-click
}
else if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
//stuff that happen on left click
}
}

listen for MouseDown event
private void button1_MouseDown(object sender, MouseEventArgs e)

Related

How to make sub for Picture box made during Run time in WinForms

So I have a button, and when the button is clicked, a picture box is created. I just wanted to know how I can make a message box appear when I click on that newly created picturebox.
private void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 1; i++)
{
PictureBox p = new PictureBox();
flowLayoutPanel1.Controls.Add(p);
}
}
you have to add a click event to your picturebox
p.MouseClick += p_MouseClick;
after adding a event this function will be called on that event -
void p_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show("clicked");
}

Close any TabControl TabPage when middle clicked on

I want to be able to middle click the tab on a TabPage and have it removed from the TabControl but even with HotTracking turned on, I don't know how to capture which tab I middle clicked on.
Is there a way to do that?
You could do something like this on the MouseClick event of your TabControl:
private void tabControl_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Middle)
{
for (int i = 0; i < tabControl.TabCount; i++)
{
if (tabControl.GetTabRect(i).Contains(e.Location))
{
tabPaControl.TabPages[i].Dispose();
}
}
}
}

How to create button animation similar in calculator with C#

Can someone help about create a winform animation like in Win7 Calculator when you hover mouse over button, currently i use bunch of image then looping it in backgroundworker, but i think its wrong, this is my code:
this occur when mouse enter,
private void bgTurnOn_DoWork(object sender, DoWorkEventArgs e)
{
Label labelSender = (Label)e.Argument;
int ii = labelSender.ImageIndex;
for (int i = ii + 4; i <= 11; i++)
{
if (labelSender.AllowDrop)
{
labelSender.ImageIndex = i;
Thread.Sleep(40);
}
}
}
and this when mouse leave
private void bgTurnOff_DoWork(object sender, DoWorkEventArgs e)
{
Label labelSender = (Label)e.Argument;
int ii = labelSender.ImageIndex;
for (int i = ii; i >= 0; i--)
{
if (!labelSender.AllowDrop)
{
labelSender.ImageIndex = i;
Thread.Sleep(80);
}
}
}
note: I just use AllowDrop so I do not bother to declare new variable, i have 42 button, so i think i need more efficient solution.
It seems that you want a glow effect, so you can use the next idea:
Make an OpacityPictureBox : PictureBox which supports opacity (in levels of 1-100 or double 0-1). See this for more information.
Add two public const int values of MaxOpacity and MinOpacity to the OpacityPictureBox class, for easy and safe range checks from the outside. The values might be 0, 100 or 0, 1, or something else, depending on your implementation of opacity.
Make an AnimatedPictureBox : UserControl which holds 1 PictureBox named pbNormal and 1 OpacityPictureBox named opbHover, both Dock = DockStyle.Fill, and one timer named timer. Make sure that pbNormal is below opbHover.
Have three public properties:
Normal of type Image which delegates into pbNormal.Image
Hover of type Image which delegates into opbHover.Image
AnimationInterval of type int which delgates into timer.Interval
In the constructor of the AnimatedPictureBox, after calling InitializeComponents, do opbHover.Opacity = 0;. You can also do this.Cursor = Cursors.Hand; if you want the cursor to change into a hand when hovering over it.
Have a private members: _animationDirection of type int, which will be -1 or 1.
Have a private method that starts an animation in a given direction:
Code:
private void Animate(int animationDirection)
{
this._animationDirection = animationDirection;
this.timer.Start();
}
Override OnMouseEnter and OnMouseLeave:
Code:
protected override void OnMouseEnter(EventArgs e)
{
this.Animate(1);
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
this.Animate(-1);
base.OnMouseEnter(e);
}
Listen to timer.Tick event and with this:
Code:
private void timer_Tick(object sender, EventArgs e)
{
var hoverOpacity = this.opbHover.Opacity + this._animationDirection;
if (hoverOpacity < OpacityPictureBox.MinOpacity ||
hoverOpacity > OpacityPictureBox.MaxOpacity)
{
this.timer.Stop();
return;
}
this.opbHover.Opacity = hoverOpacity;
}

C#: How to simulate Mouse Hover event using Timer

I have a fom which has one user control docked to fill.
this user control displays different images.each image has Id and I have a list of imageId vs imageDetail object dictionary.
Mouse Move event of this user control is captured and i am displaying current X and Y position of mouse in tool tip.
I also want to display image detail in tool tip when user keeps the mouse over image for some time.
I tried to do this with Mouse Hover event but it only raised when mouse enters in user control bound. after this if i move mouse within user control mouse hover event does not fire...
How can i display current X, Y position along image detail in tool tip.
is there any way to simulate Mouse Hover event within Mouse Move using some timer.
Is there any sample code..
I solved this problem by
public partial class Form1 : Form
{
Timer timer;
bool moveStart;
int count = 0;
Point prev;
public Form1()
{
InitializeComponent();
timer = new Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
}
void timer_Tick(object sender, EventArgs e)
{
this.timer.Stop();
this.moveStart = false;
this.toolTip1.SetToolTip(this, string.Format("Mouse Hover"));
this.textBox1.Text = (++count).ToString();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (this.prev.X == e.X && this.prev.Y == e.Y)
return;
if (moveStart)
{
this.prev = new Point(e.X, e.Y);
this.timer.Stop();
this.toolTip1.SetToolTip(this, string.Format("Mouse Move\nX : {0}\nY : {1}", e.X, e.Y));
this.timer.Start();
}
else
{
moveStart = true;
}
}
}
The simplest way would be to call the MouseOver subroutine from the MouseMove subroutine as such:
void MouseMove(object sender, MouseEventArgs e)
{
//Call the MouseHover event
MouseHover(sender, e);
}
void MouseHover(object sender, EventArgs e)
{
//MouseHover event code
}
If you want more control over when and how to display your tooltip, however, you'll need to do something similar to the following:
Declare a listening variable at class level.
Hook to the MouseHover event so the listening variable is turned on when the mouse enters.
Hook to the MouseLeave event so the listening variable is turned off when the mouse leaves.
Put your tooltip code in the MouseMove handler so it displays your tooltip if the listening variable is on.
Here's some code to demonstrate what's I outlined above.
class Form1
{
bool showPopup = false;
void MouseHover(object sender, EventArgs e)
{
showPopup = true;
}
void MouseLeave(object sender, EventArgs e)
{
showPopup = false;
toolTip.Hide(this);
}
void MouseMove(object sender, MouseEventArgs e)
{
if (showPopup)
{
toolTip.Show("X: " + e.Location.X + "\r\nY: " + e.Location.Y,
this, e.Location)
}
}
}
Of course, you'll have to add a ToolTip with the name toolTip and associate the various methods (subroutines) with the appropriate events of your control (Form, PictureBox, etc).

Changing button background color on mouse-over event

I am developing a project in C# Windows applications (WinForms) in that I need to create a function to change the background color for all the buttons that are in the single form using button mouse-over event. How do I do that?
Changing all controls of type Button:
for (int i = 0; i < Controls.Count; i++)
if (Controls[i] is Button) Controls[i].BackColor = Color.Blue;
Example of hooks:
MouseEnter += new EventHandler(delegate(object sender, EventArgs e)
{
SetButtonColour(Color.Blue);
});
MouseLeave += new EventHandler(delegate(object sender, EventArgs e)
{
SetButtonColour(Color.Red);
});
public void SetButtonColour(Color colour)
{
for (int i = 0; i < Controls.Count; i++)
if (Controls[i] is Button) Controls[i].BackColor = Color.Blue;
}
Assuming you are just changing your own app, this isn't that difficult.
In the mouse over event, just loop over the Controls property of the form and for all items that are Button, change the back color. You will need to write a recursive function to find all of the buttons probably though, since a Panel (or GroupBox, etc) contains a Controls property for all of its controls.
Something like this:
public partial class Form1 : Form
{
Color defaultColor;
Color hoverColor = Color.Orange;
public Form1()
{
InitializeComponent();
defaultColor = button1.BackColor;
}
private void Form1_MouseHover(object sender, EventArgs e)
{
foreach (Control ctrl in this.Controls)
{
if (ctrl is Button)
{
ctrl.BackColor = hoverColor;
}
}
}
private void Form1_MouseLeave(object sender, EventArgs e)
{
foreach (Control ctrl in this.Controls)
{
if (ctrl is Button)
{
ctrl.BackColor = defaultColor;
}
}
}
}

Categories