如何在每个"X"中循环按钮的功能分钟? [英] how to loop a button's functions every "X" minutes?
问题描述
我有一个按钮,只需单击一下即可执行许多不同的功能.和一个文本框.当单击按钮时,我希望这些功能每x分钟重复一次. x分钟代表文本框的值.以及texbox是否为空白.不重复.还有另一个按钮将停止计时器.我该怎么做呢?我不知道计时器,我想这是学习的好方法..
i have a button that does many different functions with one click. and a textbox. when the button is clicked i want those functions to be repeated every x minutes. and x minutes represents the value of the textbox. and if the texbox is blank. to not repeat. also another button that will stop the timer. how do i do this? i dont know timers and im guessing this is a good way to learn..
推荐答案
public partial class Form1 : Form
{
Timer theTimer = new Timer();
public int countdown { get; set; }
public Form1()
{
InitializeComponent();
theTimer.Tick += new EventHandler(theTimer_Tick);
}
void theTimer_Tick(object sender, EventArgs e)
{
//Work the Functions
}
private void btn_StopTimer_Click(object sender, EventArgs e)
{
if (theTimer.Enabled)
theTimer.Stop();
}
private void btn_StartTimer_Click(object sender, EventArgs e)
{
if (this.txt_TimerInterval.Text != "")
{
int theInterval = 0;
if (Int32.TryParse(this.txt_TimerInterval.Text, out theInterval))
{
if (theInterval != 0)
{
theTimer.Interval = theInterval * 1000;
theTimer.Start();
}
}
}
else
{
btn_StopTimer_Click(null, null);
}
}
}
我创建的只是一个虚拟表格.它包含2个按钮,btn_StartTimer启动计时器,btn_StopTimer停止计时器. txt_TimerInterval是您要在其中设置运行计时器的分钟数的文本框
Just a dummy form I created. It contains 2 buttons, btn_StartTimer starts the timer, btn_StopTimer stops the timer. txt_TimerInterval is the textbox where you want to set the minutes to run the timer
您好vlad781,
我根据您的要求创建了一个可执行示例.
(我不建议将文本框用于重复输入,请看一下NumericUpDown).随时问是否有不清楚的地方...
Hi vlad781,
I created an executable example based on your requirements for you.
(I wouldn''t recommend using a textbox for repetition input, have a look at NumericUpDown). Feel free to ask if anything is unclear...
using System;
using System.Windows.Forms;
namespace Repeat
{
static class Program
{
[STAThread]
static void Main()
{
// Create a test Form with Buttons and a Textbox to enter the repetition count
Form form = new Form();
Button buttonStart = new Button();
buttonStart.Text = "Start";
buttonStart.Dock = DockStyle.Top;
Button buttonStop = new Button();
buttonStop.Text = "Stop";
buttonStop.Dock = DockStyle.Top;
TextBox textbox = new TextBox();
textbox.Dock = DockStyle.Top;
form.Controls.Add(buttonStop);
form.Controls.Add(buttonStart);
form.Controls.Add(textbox);
// Create a timer
Timer timer = new Timer();
timer.Interval = 60000; // set the timer interval to one minute
int iRepetitionCount = 0; // Number of remainig repetitions
// Add event handlers for the button Click-Events
buttonStart.Click += delegate(object sender, EventArgs e)
{
// Restarting also stops the timer
timer.Stop();
if (String.IsNullOrWhiteSpace(textbox.Text)) // Textbox empty?
{
// Just call the function once
TheFunction();
}
else
{
// Try to get the entered repetition value
iRepetitionCount = 0; // reset
if (!Int32.TryParse(textbox.Text, out iRepetitionCount))
{
MessageBox.Show("Invalid repetition count");// The entered text could not be converted to an integer
return;
}
// Call the function now
TheFunction();
iRepetitionCount--; // Decrement the repetition counter
// ... and start the timer (on timer.Tick the Function will be called again)
timer.Start();
}
};
buttonStop.Click += delegate(object sender, EventArgs e)
{
// Stop the repetition timer
timer.Stop();
};
// Add event handlers for the timer Tick-Events
timer.Tick += delegate(object sender, EventArgs e)
{
if (iRepetitionCount <= 0) // no more repetitions?
{
timer.Stop(); // stop the timer now
}
else
{
iRepetitionCount--; // Decrement the repetition counter
TheFunction(); // Call the function
}
};
// Run the form
Application.Run(form);
}
static void TheFunction()
{
MessageBox.Show("I was called at " + DateTime.Now.ToString());
}
}
}
您最好使用线程.
http://msdn.microsoft.com/en-us/library/7a2f3ay4 (v = vs.80).aspx [
You better should use threads.
http://msdn.microsoft.com/en-us/library/7a2f3ay4(v=vs.80).aspx[^]
To elapse the time, you can useThread.Sleep
within thread loop.
这篇关于如何在每个"X"中循环按钮的功能分钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!