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).
Related
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.
This is my code:
IN Parent form:
public void tokenform_Load(object sender, EventArgs e)
{
tbvarchecker.Text = HelpForm.code;
}
private void TextBox1_KeyDown(object sender, KeyEventArgs e)
{
HelpForm help = new HelpForm();
if (e.KeyCode == Keys.F2)
{
help.Show();
}
}
public void setvalues(string cd)
{
tbvarchecker.Text = cd;
label10.Text = cd;
}
Child Form code:
private void dgv(object sender, DataGridViewCellMouseEventArgs e)
{
DataGridViewRow row = dgvshowallfields.Rows[e.RowIndex];
code = row.Cells[0].Value.ToString();
code1 = row.Cells[0].Value.ToString();
this.Close();
}
private void HelpForm_FormClosed(object sender, FormClosedEventArgs e)
{
tokenform tkf = new tokenform();
tkf.setvalues(code);
tkf.setvalues(code1);
tkf.tokenform_Load(sender,e);
}
//private void HelpForm_FormClosing(object sender, FormClosingEventArgs e)
//{
// tkf.setvalues(code);
//}
This code is declared as public static string code, while the code1 is just string.
I have also checked through breakpoints and the value reaches the function in Parent form function set values but still, I couldn't show it in the textbox.
Guys can anyone help me out.....??
First, pass the parent form to your Show() call.
Change:
help.Show();
To:
help.Show(this);
Then, in the child form, you can cast the .Owner property to your parent form type and call its methods:
private void HelpForm_FormClosed(object sender, FormClosedEventArgs e)
{
tokenform tkf = this.Owner as tokenform;
if (tkf != null)
{
tkf.setvalues(code);
tkf.setvalues(code1);
tkf.tokenform_Load(sender,e);
}
}
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
}
}
}
In my code I want to perform some actions when some controls are focused. So instead of having one handler for each control i was wondering if there could be any way of adding all controls to the handler and inside the handler function perform the desired action.
I have this:
private void tb_page_GotFocus(Object sender, EventArgs e)
{
tb_page.Visible = false;
}
private void tb_maxPrice_GotFocus(Object sender, EventArgs e)
{
tb_maxPrice.Text = "";
}
private void tb_maxPrice_GotFocus(Object sender, EventArgs e)
{
tb_maxPrice.Text = "";
}
I want this:
private void AnyControl_GotFocus(Object sender, EventArgs e)
{
if(tb_page.isFocused == true)
{
...
}
else if (tb_maxPrice.isFocused == true)
{
...
}
else
{
...
}
}
Is this possible? How could I do it? Thanks a lot.
Iterate your controls in your form or panel and subscribe to their GotFocus Event
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control c in this)
{
c.GotFocus += new EventHandler(AnyControl_GotFocus);
}
}
void AnyControl_GotFocus(object sender, EventArgs e)
{
//You'll need to identify the sender, for that you could:
if( sender == tb_page) {...}
//OR:
//Make sender implement an interface
//Inherit from control
//Set the tag property of the control with a string so you can identify what it is and what to do with it
//And other tricks
//(Read #Steve and #Taw comment below)
}
I have a user control with some buttons (tmNewItem, tmEdit, tmInsert)
I write a clickButton event for them.
for example:
public void btnEdit_Click(object sender, EventArgs e)
{
btnNew.Enabled = false;
btnEdit.Enabled = false;
}
I used this user control in another project and write another method for the buttons and assign it to the usr control:
public void DTedit(object sender, EventArgs e)
{
}
private void UserControl_Load(object sender, EventArgs e)
{
DT_Navigator.btnCancel.Click += new EventHandler(DTedit);
}
and now, when I run the project and press btnEdit button, the first time, btnEdit_Click will execute and after that DTedit. can i change it? I mean the first time DTedit (that I define it in my project) run, and after it btnEdit_Click (that I define it in the user control) run?
how can I do that?
Try this
public void DTedit(object sender, EventArgs e)
{
//Place your code here
DT_Navigator.btnCancel.Click -= new EventHandler(DTedit); //This will remove handler from the button click and it will not be executed next time.
}
private void UserControl_Load(object sender, EventArgs e)
{
DT_Navigator.btnCancel.Click += new EventHandler(DTedit);
}
Suggested Code
//User control
public event CancelEventHandler BeginEdit;
public event EventHandler EndEdit;
private btnYourButton_Click(object sender, EventArgs e)
{
CancelEventArgs e = new CancelEventArgs();
e.Cancel = false;
if (BeginEdit != null)
BeginEdit(this, e);
if (e.Cancel == false)
{
if (EndEdit != null)
EndEdit(this, new EventArgs);
//You can place your code here to disable controls
}
}