I am trying to make a minesweeper type game in visual c# and I want to have different things happen when I right click and left click a button, how do I do this?
I have tried this code but it only registers left clicks:
private void button1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MessageBox.Show("Left");
}
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
MessageBox.Show("Right");
}
}
You will have to use the MouseUp or MouseDown event instead of the Click event to capture right click.
Just try with button1_MouseDown event instead of button1_MouseClick Event.It will solve your problem.
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//do something
}
if (e.Button == MouseButtons.Right)
{
//do something
}
}
Button is reacting only for MouseButtons.Left not for MouseButton.Right and not even for middle.
void Select(object sender, MouseEventArgs e)
{
/* var btn = sender as CardButton;*/
if (e.Button == MouseButtons.Left)
{
if (this.Selected == false)
{
this.Selected = true;
}
else
{
this.Selected = false;
}
}
if (e.Button == MouseButtons.Right)
{
if (this.Selected == false)
{
this.Selected = true;
}
else
{
this.Selected = false;
}
}
Draw();
}
Related
Hi I want to save 4 mouse positions when I click on button.
Smth like this:
ButtonClick -> 1.MouseClick / Save Mouse Position-> 2.MouseClick / Save Mouse Positon .....
private void button2_Click_1(object sender, EventArgs e)
{
if (!int.TryParse(textBox4.Text, out parsedValue))
{
MessageBox.Show("Wpsiz liczbe");
return;
}
else
{
iset = int.Parse(textBox3.Text);
ms = int.Parse(textBox4.Text);
MouseDownFunction();
}
}
private void MouseDownFunction(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
eqhelmetx = MousePosition.X;
eqhelmety = MousePosition.Y;
if (e.Button == MouseButtons.Left)
{
eqchestx = MousePosition.X;
eqchesty = MousePosition.Y;
if (e.Button == MouseButtons.Left)
{
eqleginsx = MousePosition.X;
eqleginsy = MousePosition.Y;
if (e.Button == MouseButtons.Left)
{
eqbootsx = MousePosition.X;
eqbootsy = MousePosition.Y;
}
}
}
}
}```
I tried but it doesn't work. Thanks for any help
List<Point> MousePositions = new List<Point>(); // list for saving mouse positions
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (MousePositions.Count == 4)
MousePositions.RemoveAt(0); // for saving last 4 positions
MousePositions.Add(e.Location); // when clicking form saving mouse location
}
private void button1_Click(object sender, EventArgs e)
{
foreach (var item in MousePositions)
MessageBox.Show(item.ToString()); // showing mouse positions
}
after many searches on the internet, I can’t find an optimized solution. Indeed, I would just like to block keys and mouse click at some point, however I would like to avoid putting all the keys and clicking in my condition. Is this possible?
There is the code :
private void cmdVision_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pnlNote.Enabled = true;
}
}
private void cmdVision_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pnlNote.Enabled = false;
}
//MAJ4
else if (e.Button == MouseButtons.Right || e.Button == MouseButtons.Middle)
{
pnlNote.Enabled = false;
}
//MAJ4
}
As you can see my condition consists of e.Button == MouseButtons.Right || e.Button == MouseButtons.Middle.
So I would like to avoid having to do that for every key on the keyboard.
Thank you in advance ;)
Try to use PreviewKeyDown event and its PreviewKeyDownEventArgs. There are a set of helpful properties, like IsInputKey and KeyCode (you can compare pressed key with the range of key codes).
In my mini app in C# I have a button that I want to enable and disable it by right click on it. i.e. when button is enabled by right click it turn to disable and when it is disable, right click on it change it's status to enable. Disabling of enabled button is easy and straightforward but enabling it by right click on it, is not possible; because the button is disabled and not event is sent to emaciate code.
How Can I do?
The mouse events of a disabled control are passed down to its Parent.
You can catch them there and test if the cursor is on the button.
Example:
if (button1.Bounds.Contains(e.Location)) button1.Enabled = true;
If you have several buttons you need to test them all..:
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
foreach (Control ctl in panel1.Controls)
{
if (ctl is Button && ctl.Bounds.Contains(e.Location))
ctl.Enabled = true;
}
}
If only the right mouse button is supposed to enable, add a test for it, maybe like so:
if (e.Button.HasFlag(MouseButtons.Right) &&
ctl is Button && ctl.Bounds.Contains(e.Location))
Right-clicking to enable and disable a button is not standard or recommended button behavior, so the API completely leaves out an easy implementation for it. Once the button is disabled, it no longer captures mouse events, but the form does.
public partial class Form1 : Form
{
private Boolean _button1IsRightClicked;
public Form1()
{
InitializeComponent();
_button1IsRightClicked = false;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hi");
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.X >= button1.Location.X && e.X <= (button1.Location.X + button1.Width) &&
e.Y >= button1.Location.Y && e.Y <= (button1.Location.Y + button1.Height))
{
_button1IsRightClicked = true;
}
else
{
_button1IsRightClicked = false;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.X >= button1.Location.X && e.X <= (button1.Location.X + button1.Width) &&
e.Y >= button1.Location.Y && e.Y <= (button1.Location.Y + button1.Height))
{
_button1IsRightClicked = false;
enableDisableButton1(button1.Enabled);
}
else
{
_button1IsRightClicked = false;
}
}
private void enableDisableButton1(Boolean isEnabled)
{
if (isEnabled)
{
button1.Enabled = false;
isEnabled = false;
}else
{
button1.Enabled = true;
isEnabled = true;
}
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
_button1IsRightClicked = true;
}
else
{
_button1IsRightClicked = false;
}
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && _button1IsRightClicked == true)
{
_button1IsRightClicked = false;
enableDisableButton1(button1.Enabled);
}
else
{
_button1IsRightClicked = false;
}
}
}
I have a ListView to display Images Icons, and i need 2 different Context menu on that ListView that have to show when i Right Click inside the ListView.
ContextMenu1 have to show only when i Right Click on a Item
ContextMenu2 have to show when i DO NOT Right click on a Item, but in the blank space of the ListView.
This is the current Code that i have, but it is only working when i right click on a item:
private void ListView_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (ListView.GetItemAt(e.X, e.Y) is ListViewItem)
{
ContextMenu1.Show(Cursor.Position);
}
else
{
ContextMenu2.Show(Cursor.Position);
}
}
}
What i did wrong?
GetItemAt will always return a ListviewItem. You should check for null like this example from MSDN:
private void ListView_MouseDown(object sender, MouseEventArgs e)
{
if (ListView.GetItemAt(e.X, e.Y) != null )
{
ContextMenu1.Show(Cursor.Position);
}
else
{
ContextMenu2.Show(Cursor.Position);
}
}
Try using the MouseDown or MouseUp event instead:
listView1.MouseDown += listView1_MouseDown;
void listView1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
if (listView1.GetItemAt(e.X, e.Y) is ListViewItem) {
ContextMenu1.Show(Cursor.Position);
} else {
ContextMenu2.Show(Cursor.Position);
}
}
}
The MouseClick event fires only when a ListItem is clicked.
How to make node being selected by right mouse button down ?
I made it by right mouse button clicked like this:
private void myTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
this.myTreeView.SelectedNode = e.Node;
}
}
I want node being selected not by click, but just button down.
I'm guessing you are looking for something like this:
void myTreeView_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
TreeNode tn = myTreeView.GetNodeAt(e.Location);
if (tn != null) {
myTreeView.SelectedNode = tn;
}
}
}
Subscribe to the MouseDown event of the TreeView and comment out the NodeMouseClick code.