Access a TextBox from another class in workspace

耗尽温柔 提交于 2019-12-25 07:17:44

问题


I want to use a textBox, which is on my main form Form1 from other class. In class Form1 I can use:

    this.Invoke(new EventHandler(displayText));

and then

    private void displayAccFields(object o, EventArgs e)
    {
        tbAccRoll.AppendText(packParameters.getPackage(3) + "");
    }

and it works fine.

How can I access this textbox for displaying something from a different class?


回答1:


For sending values between two forms, you may

  1. Send the values in the constructor of the second form. You may create a paramterized constructor and send the values when you initialize the form.
  2. You may take a reference in to your first form in the second form.

In second form,

public Form1 objForm1;

and in First Form,

Form2 objForm2=new Form2();
Form2.objForm1=this;

and then you can use Form2's objForm1 to refer to Form1's textbox.



来源:https://stackoverflow.com/questions/15766636/access-a-textbox-from-another-class-in-workspace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!