C# WinForms - 如何通过另一个表单从一个表单上的文本框检索数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/830824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# WinForms - How Do i Retrieve Data From A Textbox On One Form Via Another Form?
提问by Goober
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?
这样做的最佳方法是什么?
采纳答案by vaquito
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
}
回答by Joshua Belden
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
回答by BFree
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;}
}
回答by Tetsujin no Oni
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),那么它可能是该类的适当成员 - 如果不是,也许它的真正家在数据对象中?
回答by Suneet
There is a similar post here
还有一个类似的帖子在这里
The videos below will clear up a lot of your concepts about passing data between 2 forms.
下面的视频将清除您关于在 2 个表单之间传递数据的许多概念。
There are multiple ways to pass data between 2 forms check these links which has example videos to do this
有多种方法可以在 2 个表单之间传递数据,请检查这些链接,其中包含可以执行此操作的示例视频
FormToForm Using Properties - http://windowsclient.net/learn/video.aspx?v=108089
FormToForm Using Parameters - http://windowsclient.net/learn/video.aspx?v=105861
FormToForm 使用属性 - http://windowsclient.net/learn/video.aspx?v=108089
FormToForm 使用参数 - http://windowsclient.net/learn/video.aspx?v=105861
HTH
HTH