0

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)
    {

    }
}

3 Answers3

0

So you need to get the instance of the form that you want to modify the text. You can do it by passing a reference to the objschedule or by using Application.openforms.

The first way is perfect if you already have the form referenced but if you don, just:

private void oTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    dynamic f = System.Windows.Forms.Application.OpenForms["anytext_Form"];
    f.xdatetxt.Text=System.DateTime.Now.ToString();
}
Aimnox
  • 895
  • 7
  • 20
0

Check this SO thread - How to access Winform textbox control from another class?

  1. Basically expose a public property that updates Textbox

  2. Make Textbox public

Also note, you need to update Form Controls in UI thread

Community
  • 1
  • 1
Makubex
  • 1,054
  • 1
  • 9
  • 16
0

There are a few problems here, but both are rather straight-forward to address.

Problem 1: Referencing the form by its base class

Your Schedule class' constructor takes an instance of Form. That is the base class of your Form1 class, and it has no xdatetxt field. Change your Schedule constructor to accept and store an instance of Form1 instead:

public Form1 anytext_Form;

public Schedule(Form1 anytext_form)
{
    this.anytext_Form = anytext_form;
}

Problem 2: Updating a control from a non-UI thread

Once you get the compiler error fixed, you will encounter a runtime error. The reason is that the Timer class executes its callback on a background thread. Your callback method attempts to access a UI control from that background thread, and that is not allowed.

I could give a solution inline here, but instead I will point you at another StackOverflow post with far more details about the problem and how to fix it: Cross-thread operation not valid.

Community
  • 1
  • 1
Matt Dillard
  • 14,677
  • 7
  • 51
  • 61
  • Boss, I am referencing Form1 class when creating object for schedule class.. Schedule objschedule = new Schedule(this); I cant write public Schedule(Form1 anytext_form) as Form2 may also be referenced instead of Form1 – Chistia Chowdhury Jul 14 '16 at 06:14
  • xdatetxt field is available in form1.designer.cs private System.Windows.Forms.Timer timer1; public System.Windows.Forms.TextBox xdatetxt; – Chistia Chowdhury Jul 14 '16 at 07:02