How To make Mouse Click for new position - c#

i already make a simple program to recording mouse position and playback. Now i wanna add event if the mouse left click and right mouse click. But i still not understand how to do it. I already try code from many site, but still not work. Any one wanna help me please ? I'm still learning about programing, i just wanna make simple program.
this is my code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AutoClicker
{
public partial class Form1 : Form
{
ListViewItem lv;
int a, b;
public Form1()
{
InitializeComponent();
this.Closing += new System.ComponentModel.CancelEventHandler(this.FormClosingEventCancle_Closing); //Menangkap event x di klik
}
private void FormClosingEventCancle_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
DialogResult dr = MessageBox.Show("Yakin ingin keluar?", "Konfirmasi", MessageBoxButtons.YesNo); if (dr == DialogResult.No)
e.Cancel = true;
else
e.Cancel = false;
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
btn_putar.Enabled = false;
btn_rekam.Enabled = false;
btn_berhenti.Enabled = true;
}
private void timer2_Tick(object sender, EventArgs e)
{
//set posisi baru mouse
if (a != b)
{
Cursor.Position = new Point(int.Parse(listView1.Items[a].SubItems[0].Text), int.Parse(listView1.Items[a].SubItems[1].Text));
a++;
}
//agar bisa rekam ulang dan data di set ulang
else
{
btn_rekam.Enabled = true;
btn_putar.Enabled = false;
btn_berhenti.Enabled = false;
listView1.Clear();
a = 0;
b = 0;
timer2.Stop();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
lv = new ListViewItem(Cursor.Position.X.ToString());
lv.SubItems.Add(Cursor.Position.Y.ToString());
listView1.Items.Add(lv);
b++;
}
private void btn_berhenti_Click(object sender, EventArgs e)
{
btn_rekam.Enabled = true;
btn_putar.Enabled = true;
timer1.Stop();
timer2.Stop();
}
private void btn_putar_Click(object sender, EventArgs e)
{
timer2.Start();
btn_putar.Enabled = false;
btn_rekam.Enabled = false;
btn_berhenti.Enabled = false;
}
private void Form1_Load(object sender, EventArgs e)
{
a = 0;
b = 0;
btn_berhenti.Enabled = false;
btn_putar.Enabled = false;
}
}
}

You can use some of Mouse events like MouseClick, MouseDown, MouseUp, etc.
For example:
protected override void OnMouseDown(MouseEventArgs e)
{
if(e.Button == System.Windows.Forms.MouseButtons.Left)
{
//Do some stuff
MessageBox.Show("Lefty!");
}
else if(e.Button == System.Windows.Forms.MouseButtons.Right)
{
//Do some stuff
MessageBox.Show("Righty!");
}
}
Assuming you copy/pase this code somewhere in Form1 class, it will override Form1's OnMouseDown event.
When you left/right click on form, you'll get related MessageBox.
If you want to use the event on any other Control, you need to override that control's related event.
*Edit after comment:
public void OnMouseDown(object sender, MouseEventArgs e)
{
if(e.Button == System.Windows.Forms.MouseButtons.Left)
{
//Do some stuff
MessageBox.Show("Lefty!");
}
else if(e.Button == System.Windows.Forms.MouseButtons.Right)
{
//Do some stuff
MessageBox.Show("Righty!");
}
}
And add OnMouseDown event to any Control on form load. For example:
private void Form1_Load(object sender, EventArgs e)
{
a = 0;
b = 0;
button1.MouseDown += OnMouseDown;
listView1.MouseDown += OnMouseDown;
}
That way, when you left/right click on button1 or listView1 you will get a MessageBox.

You can setup an event handler to fire whenever the mouse button is clicked (shown below;)
namespace MouseClickDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MouseClick += Form1_MouseClick;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
//Left mouse button hit
}
if(e.Button == MouseButtons.Right)
{
//Right mouse button hit
}
}
}

Related

Problems with Image Tag in Picturebox

So, i try to learn how to Drag and Drop an Image from one PictureBox to another and that works well. But how can i drag and drop the Image TAG from pictureox1 to picturebox2?
i currently have 3 source images and 3 drop boxes.
the dropbox6 is locked with a countdown after a buttonclick (see button2)
(later i will lock all dropboxes)
after i hit this button a countdown starts and if the countdown is 0 (ZERO) only then i can drag one of the 3 images to this box.
however, i have given each of these 3 images in the souceboxes a TAG name.
how can i drop this tagname also to the dropbox?
here is what i have so far:
(the Label labeled "Counter" is actually Label4 in the code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Game
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
// pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
((PictureBox)sender).DoDragDrop(((PictureBox)sender).Image, DragDropEffects.Copy);
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox4.AllowDrop = true;
pictureBox5.AllowDrop = true;
pictureBox6.AllowDrop = true;
pictureBox6.Enabled = false;
}
private void pictureBox4_DragEnter(object sender, DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
}
}
private void pictureBox4_DragLeave(object sender, EventArgs e)
{
}
private void pictureBox4_DragDrop(object sender, DragEventArgs e)
{
Image getPicture = (Bitmap) e.Data.GetData(DataFormats.Bitmap);
pictureBox4.Image = getPicture;
}
private void pictureBox5_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
}
}
private void pictureBox5_DragLeave(object sender, EventArgs e)
{
}
private void pictureBox5_DragDrop(object sender, DragEventArgs e)
{
Image getPicture = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pictureBox5.Image = getPicture;
}
private void pictureBox6_DragDrop(object sender, DragEventArgs e)
{
Image getPicture = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pictureBox6.Image = getPicture;
timer1.Enabled = false;
}
private void pictureBox6_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
timer1.Enabled = false;
}
}
private void pictureBox6_DragLeave(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void pictureBox4_Click(object sender, EventArgs e)
{
MessageBox.Show("test");
}
private void timer1_Tick(object sender, EventArgs e)
{
counter--;
if (counter == 0)
timer1.Stop();
label4.Text = counter.ToString();
if(counter == 0)
{
pictureBox6.Enabled = true;
label4.Enabled = false;
timer1.Stop();
}
}
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
private void pictureBox6_Click(object sender, EventArgs e)
{
MessageBox.Show(pictureBox6.Tag.ToString());
}
private int counter = 3;
private void button2_Click(object sender, EventArgs e)
{
int counter = 3;
timer1 = new Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000; // 1 second
timer1.Start();
label4.Text = counter.ToString();
if(label4.Text == "0")
{
timer1.Stop();
}
pictureBox6.Image = Properties.Resources.shoreSite_d_1_l_x_x;
button2.Visible=false;
}
}
internal class bild1
{
private Bitmap shoreSite_d_1_l_x_x;
public bild1(Bitmap shoreSite_d_1_l_x_x)
{
this.shoreSite_d_1_l_x_x = shoreSite_d_1_l_x_x;
}
}
}
In MouseDown you are saying what you want to transmit in the DoDragDrop call. In your current case you are transmitting the image ((PictureBox)sender).Image but you can chose to transmit the tag as well...
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
((PictureBox)sender).DoDragDrop(((PictureBox)sender).Image, DragDropEffects.Copy);
((PictureBox)sender).DoDragDrop(((PictureBox)sender).Tag, DragDropEffects.Copy);
}
...Then make sure to parse for each possible input type in DragDrop
private void pictureBox6_DragDrop(object sender, DragEventArgs e)
{
var image = e.Data.GetData(DataFormats.Bitmap) as Bitmap;
var tag = e.Data.GetData(DataFormats.Text) as string;
if (image != null)
{
pictureBox4.Image = image;
}
if (tag != null)
{
pictureBox4.Tag = tag;
}
timer1.Enabled = false;
}
Beware, that you will need to update all the code that checks that the data is Bitmap before it arrives at DragDrop ie in DragEnter and write code that handles both Bitmap and Text, otherwise the Drag functionality will break.
AWESOME!!
It works now. I will continue this Project.

Setting a "mouseDown" bool using labels

I was trying to set up a system to check if the mouse is clicked in a winforms application and have ended up doing this
private void lblChips_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void lblChips_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
eight times over (for each label).
Does anyone know if there's a more efficient way of doing this? I tried using
private void frmGame4_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void frmGame4_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
but that doesnt work. Any help would be greatly appreciated.
this
public Form1()
{
InitializeComponent();
CollectFormControls(this);
ControlList.Add(this);
MergeEvents();
}
List<Control> ControlList = new List<Control>();
private void CollectFormControls(Control c)
{
foreach (Control control in c.Controls)
{
ControlList.Add(control);
CheckSubControls(control);
}
}
private void CheckSubControls(Control control)
{
if (control.Controls.Count > -1)
{
CollectFormControls(control);
}
}
private void MergeEvents()
{
for (int i = 0; i < ControlList.Count; i++)
{
ControlList[i].MouseDown += All_MouseDown;
ControlList[i].MouseUp += All_MouseUp;
}
}
public void All_MouseUp(object sender, MouseEventArgs e)
{
this.Text = "up";
}
public void All_MouseDown(object sender, MouseEventArgs e)
{
this.Text = "down";
}
You could make two methods, one for mouse up and one for mouse down, like this:
private void labels_mouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void labels_mouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
Then, link all the labels to these methods. You can also do this from Code:
label.mouseDown += labels_mouseDown;
label.mouseUp += labels_mouseUp;
This has to be done somewhere under the InitializeComponent(); method. You still have to do this for every label but in the end it's less code.

KeyDown functions not activating

I've been making a game called dog simulator. Im at the point on where the movement needs to be developed, however, for some reason, the function didn't activate. Visual Studio (the IDE I use) said there were no errors. Some help will be appreciated
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace dog_simulator
{
public partial class Form1 : Form
{
bool right;
bool left;
public int left_var = 5;
public int right_var = 5;
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
public void button2_Click(object sender, EventArgs e)
{
panel1.Visible = false;
}
public void button1_Click(object sender, EventArgs e)
{
panel1.Visible = false;
}
public void Form1_KeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine("Left has been pressed");
if (e.KeyCode == Keys.Left)
{
left = true;
Console.WriteLine("Left has been pressed");
}
if(e.KeyCode == Keys.Right)
{
right = true;
Console.WriteLine("Right has been pressed");
}
}
public void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
left = false;
Console.WriteLine("Left has been let go of");
}
if (e.KeyCode == Keys.Right)
{
right = false;
Console.WriteLine("Right has been let go of");
}
}
public void timer1_Tick(object sender, EventArgs e)
{
if (right == true)
{
player.Left += left_var;
}
if (left == true)
{
player.Left -= left_var;
}
Invalidate();
}
private void dog_Click(object sender, EventArgs e)
{
}
private void player_Click(object sender, EventArgs e)
{
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
}
}
}
For a Windows Form to be able to see the key pressed you need to set the
this.KeyPreview = true;
in designer or in form code. Otherwise the key presses will be received by the control that has the current focus.
MSDN: Form.KeyPreview

Timer event c# Crash

I have a timer event as given below and I have got some suggestions from this post. Could you suggest me what is wrong with this? I am getting the following crash:
Unable to cast object of type System.Windows.Forms.Timer to type System.Windows.Forms.Button.
Any suggestions on where I am going wrong??
public MainForm()
{
InitializeComponent();
ButtonTimer.Tick += new EventHandler(ButtonTimer_Tick);
ButtonTimer.Interval = 100;
}
private void ButtonTimer_Tick(object sender, EventArgs e)
{
Button CurrentButton = (Button)sender;
string PressedButton = CurrentButton.Name;
switch (PressedButton)
{
case "BoomUp":break;
}
}
private void BoomUp_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//ButtonTimer.Enabled = true;
ButtonTimer.Start();
}
}
private void BoomUp_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ButtonTimer.Stop();
}
}
You have a problem in ButtomTime_Tick method :
Button CurrentButton = (Button)sender;
sender is not a Button, it's a Timer.
So what you gonna do now ?
You could add a private field in your class
private Button currentButton_;
and
private void BoomUp_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
currentButton_ = e.Button;//but I don't know if e.Button has a Name "BoomUp"
//you can set the name manually if needed :
currentButton_.Name = "BoomUp";
//ButtonTimer.Enabled = true;
ButtonTimer.Start();
}
}
and
private void ButtonTimer_Tick(object sender, EventArgs e)
{
switch (currentButton_.Name)
{
case "BoomUp":break;
}
}
In ButtonTimer_Tick the sender is the timer, not a button. Hence you cast a timer into a button (1st line in that method).

visual c# limit how much a button can be clicked

OK i wish to make it so there is a limit to how much a button can be clicked because with multiple boxes open causes my application to crash ~:(
here is my code for the form that i wish to stop it from opening to many don't get me wrong i do want multiple boxes just not many:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}
private void pictureBox1_Click(object sender, EventArgs e)
{
var myForm = new Form2();
myForm.Show();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.A)
{
Form3 f3 = new Form3();
f3.ShowDialog();
}
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}
Use a private counter. Every time a user clicks the button, increment the counter, and check the counter before showing the new form.
private int clickCounter = 0;
private void pictureBox1_Click(object sender, EventArgs e)
{
this.clickCounter++;
if (this.clickCounter < 10) // arbitrary number
{
var myForm = new Form2();
myForm.Show();
}
}
This will work for ensuring the user doesn't abuse the button on a single instance of the form. To make sure the same counter applies to all instances of the form, make it static:
private static int clickCounter = 0;
private void pictureBox1_Click(object sender, EventArgs e)
{
clickCounter++;
if (clickCounter < 10) // arbitrary number
{
var myForm = new Form2();
myForm.Show();
}
}
If you'd like to decrement the counter when a form closes as Junior Programmer suggests, you can bind to the Closing event. This will effectively limit the number of new forms that can be opened (rather just limiting the number of times the button can be clicked). This will work for both the local and static counter versions:
myForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
clickCounter--;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
if (clickCounter < 10) // arbitrary number
{
clickCounter++;
var myForm = new Form2();
myForm.Closing += myForm_Closing;
myForm.Show();
}
}
This is another solution which doesn't differ much from the p.s.w.g's answer;
public class Form1 : Form {
private static int count = 0;
public Form1(){
InitializeComponent();
button1.Click += click;
}
private void click(object sender, EventArgs e){
count++;
if(count > 20) {
button1.Click -= click;
count = 0;//If you want to reset
}
}
}
Simply create a global static int count variable and set it to 0. Every time the button is clicked, increase the value of count (count++).
So when the button is clicked put all your event handling in the bellow if statement instead of my comment
if(count<5)
{
count++;
//run your code for the button event
}
else
{
MessageBox.Show("You have pressed the button too many times");
}
So using your above code, your final should look like this
public partial class Form2 : Form
{
public static int count=0;
public Form2()
{
InitializeComponent();
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}
private void pictureBox1_Click(object sender, EventArgs e)
{
if(count<5)
{
count++;
var myForm = new Form2();
myForm.Show();
}
else
{
MessageBox.Show("You have pressed the button too many times");
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.A)
{
Form3 f3 = new Form3();
f3.ShowDialog();
}
}
private void Form2_Load(object sender, EventArgs e)
{
}
}

Categories