I want to show DateTime into a text box of a form "Form1".I have created a class "schedule" to create a timer with an interval 1 sec. But cannot access and update Form1's textbox field "xdatetxt". I cannot understand why is it not accessing the controls xdatetxt in Form1.
Schedule.cs
class Schedule{
System.Timers.Timer oTimer = null;
int interval = 1000;
public Form anytext_Form;
public Schedule(Form anytext_form)
{
this.anytext_Form = anytext_form;
}
public void Start()
{
oTimer = new System.Timers.Timer(interval);
oTimer.Enabled = true;
oTimer.AutoReset = true;
oTimer.Start();
oTimer.Elapsed += new System.Timers.ElapsedEventHandler(oTimer_Elapsed);
}
private void oTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{//i want to put here a line like "anytext_Form.xdatetxt.Text = System.DateTime.Now.ToString();"
}
}
in form1.cs:
public partial class Form1 : Form{
public Form1()
{
InitializeComponent();
InitializeScheduler();
}
void InitializeScheduler()
{
Schedule objschedule = new Schedule(this);
objschedule.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}