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)
{
}
}
Related
I have a C# project that has 2 forms. The first has 3 buttons. I need to be able from the second form to hide 2 (button1 and button2 )buttons with a checkbox, and I don't know how to call the buttons from the first form.
this is form1
namespace test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
}
}
and this is Form2
namespace test1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
checkBox1.Checked = Properties.Settings.Default.checkB;
if (checkBox1.CheckState == CheckState.Checked)
{
?????????
}
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.checkB = checkBox1.Checked;
Properties.Settings.Default.Save();
}
}
}
Another option is to pass the form as the "owner" in the Show() command:
private void button3_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show(this); // pass Form1 reference in to our instance of Form2
}
In Form2, cast the Owner property back to Form1 so you can access it (assuming you've changed the modifiers property of the buttons to public as already suggested):
private void Form2_Load(object sender, EventArgs e)
{
checkBox1.Checked = Properties.Settings.Default.checkB;
if (checkBox1.CheckState == CheckState.Checked)
{
Form1 f1 = (Form1)this.Owner;
f1.button1.Visible = false; // or whatever your buttons are called
}
}
This is almost exactly what I had posted previously...you need to change the Modifiers property of the buttons so they are public and can be seen from Form2.
this is the final version that works in my case thanks to those who answered my question and helped me to get this answer
Form1
namespace test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Form2 frm = new Form2();
frm.checkBox1.Checked = Properties.Settings.Default.checkB;
if (frm.checkBox1.CheckState == CheckState.Checked)
{
button1.Visible = false;
}
}
private void button3_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show(this);
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
}
}
Form2
namespace test1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
checkBox1.Checked = Properties.Settings.Default.checkB;
if (checkBox1.CheckState == CheckState.Checked)
{
Form1 f1 = (Form1)this.Owner;
f1.button1.Visible = false;
}
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.checkB = checkBox1.Checked;
Properties.Settings.Default.Save();
}
}
}
The buttons and checkBox are set to Modifiers - Public
you need to make the buttons on the first form public and then you can access them once you create a instance of the first form you will need to pass that form to the second form.
Might want to look into building an event that fires from one form and gets handled by the other form to disable the button.
I have a timer running on form 1 with a label called "timenumber" showing the time, i also have a second form that has a label "timer". How do i link these in a way that they both so the same value at the same time. Form 1 is used as a controller and the form 2 is the one that is displayed in another monitor.
Option 1
Pass a reference to the Count Down Label in Form1 to Form2 using its constructor, then use this reference to subscribe to the Label's TextChanged event:
In Form1:
private int CountDown = 100;
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this.[The Counter Label]);
form2.Show();
this.timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
this.[The Counter Label].Text = CountDown.ToString();
if (CountDown == 0)
this.timer1.Enabled = false;
CountDown -= 1;
}
In Form2:
public form2() : this(null) { }
public form2(Control timerCtl)
{
InitializeComponent();
if (timerCtl != null) {
timerCtl.TextChanged += (s, evt) => { this.[Some Label].Text = timerCtl.Text; };
}
}
Option 2
Use a Public Property of Form2 that can be set to a Control reference. Set this property in Form1 right after a new instance of Form2 has beed created.
This, however, implies that Form1 needs to know about this property in Form2:
In Form1:
private int CountDown = 100;
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
form2.CountDownControl = this.[The Counter Label];
this.timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
this.[The Counter Label].Text = CountDown.ToString();
if (CountDown == 0)
this.timer1.Enabled = false;
CountDown -= 1;
}
In Form2:
private Control timerCtl = null;
public Control CountDownControl {
set { this.timerCtl = value;
if (value != null) {
this.timerCtl.TextChanged += (s, evt) => { this.[Some Label].Text = timerCtl.Text; };
}
}
}
Many other options exist.
You could also use a Public Property in Form2 and set this property directly from the Timer.Tick event. The public property, as in the second example, could set the Text property of one of its controls to the value of the property.
I don't like this option very much, nonetheless it's available.
In Form1:
private int CountDown = 100;
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
this.timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
this.[The Counter Label].Text = CountDown.ToString();
form2?.CountDownValue = CountDown;
if (CountDown == 0)
this.timer1.Enabled = false;
CountDown -= 1;
}
In Form2:
public int CountDownValue {
set { this.[Some Label].Text = value.ToString(); }
}
}
You could also have a custom event in Form1 that Form2 could subscribe, or implement INotifyPropertyChange. But this is, give or take, the same thing as Option1.
If form1 opens form2, this is easy. In Form2, define a property to set the text on the label:
public string TimerText
{
set => _timerLabel.Text = value;
}
You might also need to call _timerLabel.Invalidate(); to force the label to refresh.
Then, when the countdown timer in form1 updates, simply set that property in form2:
private void Timer_Tick(object sender, EventArgs e)
{
// calculate remaining time
_form2.TimerText = remainingTime.ToString();
}
Here, _form2 is a reference to the Form2 object that form1 is showing.
You could have the property in Form2 be an int or double rather than a string; which you choose may depend on whether you do anything else with the value in Form2.
I have a small project about manage time use on computer.
Form1:
public partial class Form1 : Form
{
private Timer t = new Timer();
public static int counter = 60;
public Form1()
{
InitializeComponent();
t.Tick += new EventHandler(Timer_Tick);
t.Interval = 1000;
t.Enabled = true;
t.Start();
Form2 TheForm2 = new Form2();
TheForm2.ShowDialog();
}
void Timer_Tick(object sender, EventArgs e)
{
counter -= 1;
if (counter==20)
{
MessageBox.Show("Time remaining "+counter.ToString());
}
}
}
And Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int userVal = int.Parse(textBox2.Text);
Form1.counter += userVal;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = Form1.counter.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
Final program:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 TheForm = new Form1();
Application.Run();
}
}
I try to make this application run on Windows start up and the Form1 is hide. But I wanna make form2 invisible also. And it just show when user excute the application. How can I solve it?
I just put this exe to folder start up to make it run on start.(I'll try to make it with Registry)
If I understand correctly, you want to control the visibility of one form from another form. To do so, use the .Visible attribute of the form. For example:
public Form1()
{
InitializeComponent();
t.Tick += new EventHandler(Timer_Tick);
t.Interval = 1000;
t.Enabled = true;
t.Start();
Form2 TheForm2 = new Form2();
TheForm2.ShowDialog();
TheForm2.Visible = false;
}
There are other issues with the way you are doing this, but I presume you will sort those out over time, or post other questions :)
Edit:
OK, I have modified your code to demonstrate how to make this work. The code compiles and runs for me, and shows how to make form2 initially invisible, then make it visible after 10 seconds have elapsed.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TwoForms
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// If you do it this way you'll have to stop the application yourself
// Form1 TheForm = new Form1();
// Application.Run();
Application.Run(new Form1()); // When Form1 is closed, the application will exit.
}
}
}
Form1:
using System;
using System.Windows.Forms;
namespace TwoForms
{
public partial class Form1 : Form
{
private Timer t = new Timer();
public static int counter = 60;
public Form TheForm2;
public Form1()
{
InitializeComponent();
t.Tick += new EventHandler(Timer_Tick);
t.Interval = 1000;
t.Enabled = true;
t.Start();
this.Show(); // show Form1 just so we know it's really there
TheForm2 = new Form2();
// TheForm2.ShowDialog(); // Don't do this unless you really want a modal dialog
TheForm2.Show();
TheForm2.Visible = false; // A timer tick will later set visible true
}
void Timer_Tick(object sender, EventArgs e)
{
counter -= 1;
if (counter == 50)
TheForm2.Visible = true;
if (counter == 40)
MessageBox.Show("Time remaining " + counter.ToString());
}
}
}
Form2:
using System;
using System.Windows.Forms;
namespace TwoForms
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int userVal = int.Parse(textBox2.Text);
Form1.counter += userVal;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = Form1.counter.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs 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
}
}
}
I have a following form Form3 that is opened by another form Form1, and when closed Form1 opens back up.
The problem is when I close Form3 DoSomething keeps running after form is closed.
I understand that I can make DoSomething into a thread and set IsBackground = true but is there another way to stop all processes when form closes.
This code is just example, For illustration.
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void DoSomething()
{
int i = 0;
while(true)
{
if (!this.IsDisposed)
{
Application.DoEvents();
i++;
Thread.Sleep(10);
label1.Text = i.ToString();
dataGridView1.Rows.Add();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
DoSomething();
}
private void Form3_FormClosed(object sender, FormClosedEventArgs e)
{
this.Dispose();
Form1.Default.Show();
}
}
You never break out of the while(true). You should either break the loop when it's IsDisposed is true, change your while loop to while(!IsDisposed), or store use a class level variable that determines when to break the loop.
I would probably opt for the latter, as it gives you a little more control.
public partial class Form3 : Form
{
volatile bool clDoSomething;
public Form3()
{
InitializeComponent();
}
private void DoSomething()
{
int i = 0;
clDoSomething = true;
while(clDoSomething)
{
Application.DoEvents();
++i;
Thread.Sleep(10);
label1.Text = i.ToString();
dataGridView1.Rows.Add();
}
}
private void button1_Click(object sender, EventArgs e)
{
DoSomething();
}
private void Form3_FormClosed(object sender, FormClosedEventArgs e)
{
clDoSomething = false;
Form1.Default.Show();
}
}
Your fundamental approach is flawed.
First off, Application.DoEvents should be avoided unless you are sure that you really need it, and that you are using it correctly. You do not need it here, and you are not using it correctly.
What you really need here is a Timer.
private Timer timer = new Timer();
private int count = 0;
public Form3()
{
InitializeComponent();
timer.Tick += timer_Tick;
timer.Interval = 10;
//when the form is closed stop the timer.
FormClosed += (_, args) => timer.Stop();
}
private void button1_Click(object sender, EventArgs e)
{
count = 0;
timer.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
count++;
label1.Text = count.ToString();
dataGridView1.Rows.Add();
}
When the Form is create the Timer is configured. The tick event is set, along with the interval. The tick event will look similar to your DoSomething method; it will involve running some bit of code every 10 seconds, from the UI thread, while keeping the UI responsive. When the form is closed simply stop the timer and it will stop firing off these events.
Also note that in this example here pressing the button multiple times simply resets the timer and the count, it doesn't end up creating two loops that each fire every 10 milliseconds.
Override this.Dispose() or this.Close() as appropriate and kill off DoSomething() manually.
Thanks to cdhowie suggestions and input of all others. Mowing DoEvents to the end and adding IsDipsosed solved my problem.
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void DoSomething()
{
int i = 0;
while ((true) && !this.IsDisposed)
{
i++;
Thread.Sleep(10);
label1.Text = i.ToString();
dataGridView1.Rows.Add();
Application.DoEvents();
}
}
private void button1_Click(object sender, EventArgs e)
{
DoSomething();
}
private void Form3_FormClosed(object sender, FormClosedEventArgs e)
{
Form1.Default.Show();
}
}
try to add this intsruction in the FormClosing event :
System.Diagnostics.Process.GetCurrentProcess().Kill();
it's a little bit like this:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
System.Diagnostics.Process.GetCurrentProcess().Kill();
}