C#WinForms - 如何通过另一种形式从一个表单上的文本框中检索数据?

时间:2022-04-15 23:08:47

I have a method that executes inside one form, but I need to retrieve data from another form to pass into the method.

我有一个在一个表单内执行的方法,但我需要从另一个表单中检索数据以传递给该方法。

Whats the best way of doing this?

这是最好的方法吗?

5 个解决方案

#1


Assuming that formB is initialized in formA I would recommend adding a string to the constructor of formB sending the Texbox1.Text

假设formB在formA中初始化,我建议在formB的构造函数中添加一个字符串,发送Texbox1.Text

as in

class formB: Form{
   private string data;
   public formB(string data)
    {
        InitializeComponent();
        this.data = data;
    }
  //rest of your code for the class

}

#2


You can expose a property on one form and call it from the other. Of course you'll need some way of getting the instance of form1. You could keep it as a static property in the program class or some other parent class. Usually in this case I have a static application class that holds the instance.

您可以在一个表单上公开属性,然后从另一个表单中调用它。当然,你需要一些方法来获取form1的实例。您可以将它作为程序类或其他父类中的静态属性。通常在这种情况下,我有一个保存实例的静态应用程序类。

public static class Application
{
public static MyForm MyFormInstance { get; set; }
}

Then when you launch the first form, set the application MyFormInstance property to the instance of the first Form.

然后,当您启动第一个窗体时,将应用程序MyFormInstance属性设置为第一个窗体的实例。

MyForm instance = new MyForm();
Application.MyFormInstance = instance;

Add a property to the second form.

将属性添加到第二个表单。

public String MyText
{ get { return textbox1.Text; }
  set { textbox1.Text = value; }
}

And then you can access it from your second form with:

然后您可以从第二个表单访问它:

Application.MyFormInstance.MyText

#3


On the form that has the textbox you need data from, expose either a Property or a Method that returns the text. IE:

在具有文本框的表单上,您需要数据,公开属性或返回文本的方法。 IE:

internal string TextBoxTest
{
   get{ return this.textBox1.Text;}
}

#4


There is a similar post here

这里有类似的帖子

The videos below will clear up a lot of your concepts about passing data between 2 forms.

下面的视频将清除很多关于在两个表单之间传递数据的概念。

There are multiple ways to pass data between 2 forms check these links which has example videos to do this

有两种方法可以在两个表单之间传递数据,请检查这些链接,这些链接包含示例视频

HTH

#5


Don't do this.

不要这样做。

Longer version: Why is your view directly interacting with another view?

更长的版本:为什么您的视图直接与另一个视图交互?

Much longer version:

更长的版本:

Rather than making a public property that exposes the field, it would provide better encapsulation and insulation from change if the form with the field of interest interacted with some form of data object, which was then passed to the interested method.

如果具有感兴趣的字段的表单与某种形式的数据对象交互,那么它将提供更好的封装和绝缘,而不是制作暴露该字段的公共属性,然后将其传递给感兴趣的方法。

The location of the interested method should be carefully considered - if it controls aspects of the view (WinForm, in your case), then it may be appropriately a member of that class - if not, perhaps its real home is in the data object?

感兴趣的方法的位置应该仔细考虑 - 如果它控制视图的各个方面(在你的情况下是WinForm),那么它可能恰当地是该类的成员 - 如果不是,也许它真正的家在数据对象中?

#1


Assuming that formB is initialized in formA I would recommend adding a string to the constructor of formB sending the Texbox1.Text

假设formB在formA中初始化,我建议在formB的构造函数中添加一个字符串,发送Texbox1.Text

as in

class formB: Form{
   private string data;
   public formB(string data)
    {
        InitializeComponent();
        this.data = data;
    }
  //rest of your code for the class

}

#2


You can expose a property on one form and call it from the other. Of course you'll need some way of getting the instance of form1. You could keep it as a static property in the program class or some other parent class. Usually in this case I have a static application class that holds the instance.

您可以在一个表单上公开属性,然后从另一个表单中调用它。当然,你需要一些方法来获取form1的实例。您可以将它作为程序类或其他父类中的静态属性。通常在这种情况下,我有一个保存实例的静态应用程序类。

public static class Application
{
public static MyForm MyFormInstance { get; set; }
}

Then when you launch the first form, set the application MyFormInstance property to the instance of the first Form.

然后,当您启动第一个窗体时,将应用程序MyFormInstance属性设置为第一个窗体的实例。

MyForm instance = new MyForm();
Application.MyFormInstance = instance;

Add a property to the second form.

将属性添加到第二个表单。

public String MyText
{ get { return textbox1.Text; }
  set { textbox1.Text = value; }
}

And then you can access it from your second form with:

然后您可以从第二个表单访问它:

Application.MyFormInstance.MyText

#3


On the form that has the textbox you need data from, expose either a Property or a Method that returns the text. IE:

在具有文本框的表单上,您需要数据,公开属性或返回文本的方法。 IE:

internal string TextBoxTest
{
   get{ return this.textBox1.Text;}
}

#4


There is a similar post here

这里有类似的帖子

The videos below will clear up a lot of your concepts about passing data between 2 forms.

下面的视频将清除很多关于在两个表单之间传递数据的概念。

There are multiple ways to pass data between 2 forms check these links which has example videos to do this

有两种方法可以在两个表单之间传递数据,请检查这些链接,这些链接包含示例视频

HTH

#5


Don't do this.

不要这样做。

Longer version: Why is your view directly interacting with another view?

更长的版本:为什么您的视图直接与另一个视图交互?

Much longer version:

更长的版本:

Rather than making a public property that exposes the field, it would provide better encapsulation and insulation from change if the form with the field of interest interacted with some form of data object, which was then passed to the interested method.

如果具有感兴趣的字段的表单与某种形式的数据对象交互,那么它将提供更好的封装和绝缘,而不是制作暴露该字段的公共属性,然后将其传递给感兴趣的方法。

The location of the interested method should be carefully considered - if it controls aspects of the view (WinForm, in your case), then it may be appropriately a member of that class - if not, perhaps its real home is in the data object?

感兴趣的方法的位置应该仔细考虑 - 如果它控制视图的各个方面(在你的情况下是WinForm),那么它可能恰当地是该类的成员 - 如果不是,也许它真正的家在数据对象中?