Sunday, August 3, 2014

C# adding text every five minutes

First, you need to create a Windows Forms application in Visual Studio. Then you add your TextBox and ListBox to the form and open the code file for it (right-click on Form1.cs or whatever your Form is called and select "View code").


To add the text in the TextBox to the ListBox every 5 minutes, you could use a System.Windows.Forms.Timer and the following code:



public partial class Form1 : Form
{
System.Windows.Forms.Timer _timer = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();

_timer.Interval = 300000; // 30000 ms = 5 mins
_timer.Tick += _timer_Tick;
_timer.Start();
}

void _timer_Tick(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
}
}



A System.Windows.Forms.Timer is optimized for use in Windows Forms applications and performs the code in the Tick event handler on the UI thread.


Please remember to mark helpful posts as answer.


No comments:

Post a Comment