将 TextBox 的文本传递给 C# 中的另一种形式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11165537/
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
Passing TextBox's text to another form in C#?
提问by Hunter Mitchell
I have tried this to pass the information:
我试过这样传递信息:
Form1 frm1 = new Form1();
textBox1.Text = ((TextBox)frm1.Controls["textBox1"]).Text;
This is in the form load of the form getting the information. But there is no text. How do I fix this? Form2is grabbing Form1's text and displaying it.
这是在获取信息的表单的表单加载中。但是没有文字。我该如何解决?Form2正在抓取Form1的文本并显示它。
采纳答案by Herman
Expose the contents of the textbox using a property:
使用属性公开文本框的内容:
class Form1 {
public string MyValue {
get { return textBox1.Text; }
}
}
Then in Form2 do this:
然后在 Form2 中执行以下操作:
var frm1 = new Form1();
frm1.ShowDialog(this); // make sure this instance of Form1 is visible
textBox1.Text = frm1.MyValue;
If you want frm1to be constantly visible then make frm1a class variable of Form2, and call .Show()in the constructor of Form2for example.
如果您想frm1始终可见,则创建frm1一个类变量Form2,并调用例如.Show()的构造函数Form2。
回答by Scott Selby
If its a web form MSDN suggests this at the top of the page requesting text.
如果它是一个 Web 表单,MSDN 会在请求文本的页面顶部建议此内容。
<%@ PreviousPageType VirtualPath="~/SourcePage.aspx" %>
then this in page_load
然后在 page_load 中
if (PreviousPage != null)
{
TextBox SourceTextBox =
(TextBox) PreviousPage.FindControl("TextBox1");
if (SourceTextBox != null)
{
Label1.Text = SourceTextBox.Text;
}
}
回答by MMK
Form 1
表格 1
class Form1
{
public System.Windows.Forms.TextBox textBox1; // we need to make it public Textbox
private System.Windows.Forms.Button button1;
}
In Form2 on Button click or any other event use
在 Form2 上的 Button 单击或任何其他事件使用
Form 2
表格 2
private void button1_Click(object sender, EventArgs e)
{
Form1 _form1 = new Form1();
this._form2TextBox.Text = _form1.textBox1.Text;
}
for more details visit MSDN
有关更多详细信息,请访问MSDN
回答by Dave M
Make sure that Form1's textBox1.Text property is initialized to an actual stringthat you can see. I suspect that, since you're using a default instance of Form1, the TextBox1.Text property is String.Emptywhich is the default value (and why you're not getting any visible text which it seems is what you're expecting).
确保 Form1 的 textBox1.Text 属性已初始化为您可以看到的实际字符串。我怀疑,由于您使用的是 Form1 的默认实例,因此 TextBox1.Text 属性是String.Empty,这是默认值(以及为什么您没有获得任何可见文本,这似乎是您所期望的) .
回答by Muhammad Omer Javed
Form 1
表格 1
//Declare a Static variable
public static string txtInput=string.empty;`
//Set textBox Value to this variable on any event
txtInput=textBox1.Text;
Form 2
表格 2
// Now Call this variable from the Instance of form 1
string getForm1Value=string.empty;
getForm1Value=Form1.txtinput;
回答by user2499974
try the following code , its work for me...
尝试以下代码,它对我有用...
public static string sendtext = "";
private void button1_Click(object sender, EventArgs e)
{
sendtext = txt1.Text;
Form2 frm = new Form2();
frm.Show();
}
access this data in the form load event of form2
在 form2 的表单加载事件中访问此数据
private void Form2_Load(object sender, EventArgs e)
{
txt2.Text = Form1.sendtext;
}
Enjoy
享受
回答by Alexander Zaldostanov
**
**
100% Working
100% 工作
**
**
Take 2 forms, Form1 and Form2 , Form1 contain textbox1 and Frm1Btn1, and Form2 contain Frm2Btn1, now open form1, click on open the form2
取2个表单,Form1和Form2,Form1包含textbox1和Frm1Btn1,Form2包含Frm2Btn1,现在打开form1,点击打开form2
//FORM1 CODING:
//表格1编码:
private void Frm1Btn1_Click(object sender, EventArgs e)
{
Form2 obj=new Form2();
obj.showDialog();
}
//FORM2 CODING:
//表格2编码:
private void Frm2Btn1_Click(object sender, EventArgs e)
{
Form1 objj = new Form1();
objj =(Form1) Application.OpenForms["Form1"];
objj.textBox1.Text = "Salil";
objj=null;
}
回答by user3237458
An easy thing which you can do is to store the textbox value into a variable and then pass it to another form using constructor. E.G
您可以做的一件简单的事情是将文本框值存储到一个变量中,然后使用构造函数将其传递给另一个表单。例如
string textboxvalue=textbox1.Text();
form2 win = new form2(textboxvalue);
here form2 is the form where you want to send the value and win is the instance of that form.
这里 form2 是您要发送值的表单,而 win 是该表单的实例。
回答by Anmol
I find Easy and Logical Way to Passing Text value one textbox to other in Windows Application.
我发现在 Windows 应用程序中将文本值一个文本框传递给另一个文本框的简单而合乎逻辑的方法。
In Second Form Write Code:-
以第二种形式编写代码:-
Create a Parameter of *Form2* Constructor.
namespace PassingValue
{
public partial class Form2 : Form
{
public Form2(string message)
{
InitializeComponent();
Textbox1.Text= message;
}
}
}
In First Form Write Code:-
以第一种形式编写代码:-
Use the Parameter of Second Form in *First Form*:-
namespace PassValue
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
Form2 f2=new Form2(Textbox.Text);
f2.Show();
}
}
}
回答by Mathan Sivanantham
In windows application., Just go to another form designer and declare that tool as public. You can access that tool from any form.
在 Windows 应用程序中,只需转到另一个表单设计器并将该工具声明为公共。您可以从任何形式访问该工具。

