How do i get my winforms app to click the button on the form based on an interval which is defined in a text box on my form?
I have tried:
private void Form1_Load(object sender, EventArgs e)
{
Timer tm;
tm = new Timer();
tm.Interval = 1000;
tm.Tick += new EventHandler(button1_Click);
}
Once the timer starts and they click Yes to the dialog box, then i want the timer to restart again.
I have the following:
if (result1 == DialogResult.Yes)
{
string pastebuffer = DateTime.Now.ToString();
pastebuffer = "### Edited on " + pastebuffer + " by " + txtUsername.Text + "###";
Clipboard.SetText(pastebuffer);
}
else if (result1 == DialogResult.No)
{
//do something else
}
Add interval from textBox and start the timer, all from a click of a button:
private void button1_Click(object sender, EventArgs e)
{
timer1.Stop(); // if you need to stop, then stop it here
timer1.Interval = int.Parse(textBox1.Text);
timer1.Start();
}
To stop the timer, use
tm.Stop();
Then you can change whatever the interval is, or any other properties as such
tm.Interval = int.Parse(textBox1.Text); // Will fail on non-int input...
then use,
tm.Start();
Use this code. Hope it will help. Define Time tm on class level so that you can access outside the Form1_load function.
Timer tm;
private void Form1_Load(object sender, EventArgs e)
{
tm = new Timer();
tm.Interval = 1000;
tm.Tick += new EventHandler(button1_Click);
}
if (result1 == DialogResult.Yes)
{
'if you want to restart your timer than add here.
tm.stop()
tm.Interval = int.Parse(newinterval.text);
string pastebuffer = DateTime.Now.ToString();
pastebuffer = "### Edited on " + pastebuffer + " by " + txtUsername.Text + "###";
Clipboard.SetText(pastebuffer);
tm.start()
}
else if (result1 == DialogResult.No)
{
//do something else
}
Related
The Problem
I have used the project from https://github.com/zaagan/BioMetrix and want to use BackgroundWorker to display a progression during long tasks (Ex: Get Log Data takes about 30 seconds)
What I've done so far
1- Added backgroundWorker1 from Toolbox
2- After InitializeComponent(), I've Added :
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
3- Added those functions:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; i <= 10; i++)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
System.Threading.Thread.Sleep(500);
worker.ReportProgress(i * 10);
}
}
}
private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
this.Text = (e.ProgressPercentage.ToString() + "%");
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled == true)
{
this.Text = "Canceled!";
}
else if (e.Error != null)
{
this.Text = "Error: " + e.Error.Message;
}
else
{
this.Text = "Done!";
}
}
I was expecting to view the progression percentage on the main Caption title text while retrieving log data from the device.
You need to assign the event handler to the event
worker.DoWork += backgroundWorker_ProgressChanged;
If you have created a Windows Form you need to looking in events page of the Background Worker properties and find the DoWork event, then assign it a function to run.
I have a little application contains a start and reset button in this
timer alike program.
In the button start event handler, I have a FileSystemWatcher and Timer
to call a function at certain time.
I also have another timer to count down the time call timerCounter just to do for visual aid.
What I want is to click the start button again to run the program
after the stop/reset button is clicked, but fail to do so.
Below is my part of the code that I have doubt how to restart the program again on the start button click event.
private void btnStart_Click(object sender, EventArgs e)
{
// file system watcher, timer and one other function inside this event handler
_timeLeft = (int)numUpDown.Value;
timerCounter.Start();
}
private void btnStop_Click(object sender, EventArgs e)
{
_timer.Stop();
timerCounter.Stop();
_timer.Dispose();
numUpDown.Value = 0;
timeLabel.Text = 0 + #" seconds";
lblResult.Text = #"Program has stopped, press start button to
process again.";
_fsw.Dispose();
}
private void timerCounter_Tick(object sender, EventArgs e)
{
if (_timeLeft > 0)
{
_timeLeft = _timeLeft - 1;
timeLabel.Text = _timeLeft + " seconds";
lblResult.Text = $#"Process began and counting down {timeLabel.Text} in seconds";
}
else
{
timerCounter.Stop();
timerCounter.Enabled = false;
timeLabel.Text = $#"Time's up";
}
}
I want to save richTextBox.Text every minute, when user checked save checkbox.
here checkbox event.
num_saveTime is numericUpDown control for inpute minute.
save_timer is global variable.
private void chk_save_CheckedChanged(object sender, EventArgs e)
{
if (chk_save.Checked)
{
num_saveTime.Enabled = false;
save_timer = new System.Timers.Timer();
save_timer.Elapsed += new System.Timers.ElapsedEventHandler(save_timer_Elapsed);
save_timer.Interval = 250;
save_timer.Start();
}
else
{
num_saveTime.Enabled = true;
save_timer.Stop();
}
}
and Timer Event
delegate void TimerDelegate(object sender, System.Timers.ElapsedEventArgs e);
void save_timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (rtxb_PKT.InvokeRequired)
{
TimerDelegate tdel = new TimerDelegate(save_timer_Elapsed);
rtxb_PKT.Invoke(tdel, new object[] { sender, e });
}
else
{
string now_ss = DateTime.Now.ToString("ss");
if (now_ss.Equals("00"))
{
string now_mm = DateTime.Now.ToString("mm");
string save_mm = Convert.ToInt32(num_saveTime.Value).ToString("D2");
if (save_mm.Equals(now_mm))
{
string path = Application.StartupPath + #"\" + DateTime.Now.ToString("yy-MM-dd--HH-mm-sss") + ".rtf";
rtxb_PKT.SaveFile(path, RichTextBoxStreamType.RichText);
rtxb_PKT.Clear(); //----here
}
}
}
}
I want clear richTextBox after save.
But I insert .Clear() that position, it clear text before save, so file is empty...
How can I clear it after saving?
I have three forms, From 1 is a main form and Form2 is splash screen and Form3 is a mini form.
So what i am trying to do is when ever i send a mail this From3 will pop up which has a gif image
and a label. So i am successfully able to make the From3 for 5 sec, in which the label will
change for different words for each second.
In form 3
i am using timer1 to run the mini form only for 5 seconds. and timer two to run only for 1 sec.
I guess we can do this in a better way which is pretty simple and easy.. Any good ideas and
help are most welcome!!!
Note:- Also when i again press the button send mail.. the label is starting from - Done!!.. Any helps.. The first time it starts from Connecting to smtp server..but on second time onwards its staring from Done!! then label going to Connecting to smtp server.. and so on!!
Here is my code:
Form1
private void sendmail_Click(object sender, EventArgs e)
{
//mail basic function here!!!!
smtp.Send(msg);
_f3.ShowDialog();//- - ->> goes to mini form Form3
smtp.Dispose();
MessageBox.Show("Email Successfully Sent!!!", "Mail!!!.");
}
Form3
private void Form3_Load(object sender, EventArgs e)
{
timer1.Interval = 5000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
timer2.Interval = 1000;
timer2.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
timer1.Stop();
}
int StopTime = 0;
private void timer2_Tick(object sender, EventArgs e)
{
StopTime++;
if (StopTime == 1)
{
label1.Text = " Connecting to smtp server..";
}
if (StopTime == 2)
{
label1.Text = " Fetching recipients..";
}
if (StopTime == 3)
{
label1.Text = " Attaching G-code files..";
}
if (StopTime == 4)
{
label1.Text = " Done!!";
StopTime = 0;
timer2.Stop();
}
}
Does Form3 close after it displays Done? You could do something like this:
private void Form3_Load(object sender, EventArgs e)
{
SetLabel1Text(); //reset label text
timer1.Interval = 5000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
timer2.Interval = 1000;
timer2.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
timer1.Stop();
}
int StopTime = 0;
private void timer2_Tick(object sender, EventArgs e)
{
StopTime++;
SetLabel1Text();
if (StopTime == 4)
{
StopTime = 0;
timer2.Stop();
}
}
private void SetLabel1Text()
{
string[] label1Text = { " Connecting to smtp server..", " Connecting to smtp server..", " Fetching recipients..", " Attaching G-code files..", " Done!!" };
label1.Text = label1Text[StopTime]; //populate label from array of values
}
I intend to put a timer in my the following code so that that the button will be enabled again after 5 seconds. AS you can see, my send button will be disabled after the user send 5 message. I want to enabled it after 5 seconds have elapsed.
Any suggestion is welcomed.
public bool stopSpam(int counter)
{
int spam = counter;
if (spam < 6)
{
return false;
}
else
{
return true;
}
}
private void button1_Click(object sender, EventArgs e)
{
counter++;
bool check = stopSpam(counter);
if (check == false)
{
if (textBox2.Text != "")
{
if (textBox2.Text.ToLower().StartsWith("/"))
{
onCommand(textBox2.Text);
string datetimestring = string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}.txt", DateTime.Now);
String exePath = string.Format(Application.StartupPath + "\\logs\\" + "msglogs {0}", datetimestring);
StreamWriter writer = File.CreateText(exePath);
writer.Write(textBox1.Text);
writer.Close();
textBox2.Text = "";
}
else
{
m_ChildConnection.SendMessage("MSG :" + textBox2.Text);
string datetimestring = string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}.txt", DateTime.Now);
String exePath = string.Format(Application.StartupPath + "\\logs\\" + "msglogs {0}", datetimestring);
StreamWriter writer = File.CreateText(exePath);
writer.Write(textBox1.Text);
writer.Close();
textBox2.Text = "";
}
}
}
else
{
button1.Enabled = false;
}
Thanks in adavence!
Have a timer, set its interval to 5 seconds (5000). Keep it disabled by default.
When the button is pressed enable the timer
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
timer.Enabled = true;
}
When a tick occurs after 5 seconds, enable the button and disable the timer again.
private void timer_Tick(object sender, EventArgs e)
{
button1.Enabled = true;
timer.Enabled = false;
}
Hard to figure out what you're trying to achieve but you could take the following steps to disable the enable the button after 5 seconds.
Add:
private Timer t;
as a class variable.
then after your InitializeComponent add:
t = new Timer(5000){Enabled = false, Tick += (myTick)};
then add this method:
private void myTick(object source, ElapsedEventArgs e)
{
button1.Enabled = true;
}
Also, consider updating this method:
your stopSpam method to:
public bool stopSpam(int counter)
{
return counter >= 6;
}
In fact, there is actually no need for the method:
Simply change
if(check == false)
to
if(counter > 5)
You can simply use System.Timers.Timer class to put timer.
//Define the timer
private System.Timers.Timer buttonTimer;
// Initialize the timer with a five second interval.
buttonTimer= new System.Timers.Timer(5000);
// Hook up the Elapsed event for the timer.
buttonTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
//Start the timer
buttonTimer.Start();
// Enable the button in timer elapsed event handler
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
button1.Enabled = true;
}