2016-07-15 95 views
-1

如何讓我的Winforms應用程序單擊窗體上的按鈕,基於在窗體上的文本框中定義的間隔?間隔計時器點擊

我曾嘗試:

private void Form1_Load(object sender, EventArgs e) 
{ 
    Timer tm; 
    tm = new Timer(); 
    tm.Interval = 1000; 
    tm.Tick += new EventHandler(button1_Click); 
} 

一旦計時器啓動,他們單擊是對話框,然後我想計時器再次重新啓動。

我有以下幾點:

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 
    } 
+0

對話框?? Form1聽起來不像一個對話類的好名字。 –

回答

1

要停止計時,使用

tm.Stop(); 

然後你可以改變任何的間隔,或任何其他屬性,例如

tm.Interval = int.Parse(textBox1.Text); // Will fail on non-int input... 

然後使用,

tm.Start(); 
1

從文字框添加間隔和啓動計時器,都從一個按鈕,點擊:

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(); 
} 
1

使用此代碼。希望它會有所幫助。定義時間tm級別級別,以便您可以訪問Form1_load函數之外。

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 
    }