Accessing Text Box Values from Form to another class

冷暖自知 提交于 2019-12-02 10:21:49

Just a small example (This is winforms)

This is the mainwindow, where your textbox is:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
1
public string TextBox1Text
{ 
  get { return textBox1.Text; }
  set { textBox1.Text = value;
}
}

and this is a class where you want to interact with the textboxes:

public class Test
{
public Test(Form1 form)
{
//Set the text of the textbox in the form1
form.TextBox1Text = "Hello World";
}
}

To get and set the value of a textbox within another class/form you can do it with something like:

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