Call for a form method from another class



  • I want to update the text in a label from another class, but I don't know how to do it.

    I'm running a flow in the next class:

    private void Form1_Load(object sender, EventArgs e)
    {
        Thread thread = new Thread(delegate() { Auth.CheckAuth(); });
        thread.Start();
    }
    

    Class

    class Auth
    {
        public static void CheckAuth()
        {
            while (true)
            {
                //тут нужно обновлять информацию в форме
                Form1.label1.Invoke((MethodInvoker)(delegate() { Form1.label1.Text = "INFO"; }));
                Thread.Sleep(7000);
            }
        }
    }
    

    I tried delegates, but it's vain.

    I don't know what he wants.

    public void update_Label1(string text)
    {
        label1.Text = text;
    }
    

    And

    Form1.update_Label1("INFO");
    

    And I challenge the same place in the classroom. He's a statist, but if he does, he' it's about 50 more mistakes.

    Help me make sure that you can do what you're up to?



  • The mistake is, Form1 It's not an object, it's a class. That's why only static methods can be taken from it, because label is a normal field and you get a mistake.

    Instead, we need to use a copy of the class.

    For example:

    Thread thread = new Thread(delegate() { Auth.CheckAuth(this); });
    

    in method

    public static void CheckAuth(Form1 form)
    

    and inside

    form.label1.Invoke((MethodInvoker)(delegate() { form.label1.Text = "INFO"; }));
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2