将TextBox的文本传递给C#中的另一个表单?

我试过这个传递信息:

Form1 frm1 = new Form1(); textBox1.Text = ((TextBox)frm1.Controls["textBox1"]).Text; 

这是获取信息的表单的forms加载。 但是没有文字。 我该如何解决? Form2正在抓取Form1的文本并显示它。

使用属性公开文本框的内容:

 class Form1 { public string MyValue { get { return textBox1.Text; } } } 

然后在Form2中执行以下操作:

 var frm1 = new Form1(); frm1.ShowDialog(this); // make sure this instance of Form1 is visible textBox1.Text = frm1.MyValue; 

如果你想让frm1持续可见,那么使frm1成为Form2的类变量,并在Form2的构造函数中调用.Show()

我发现在Windows应用程序中将简单和逻辑方式将文本值传递给其他文本框。

在第二表格编写代码: –

  Create a Parameter of *Form2* Constructor. namespace PassingValue { public partial class Form2 : Form { public Form2(string message) { InitializeComponent(); Textbox1.Text= message; } } } 

在第一表格编写代码: –

  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(); } } } 

尝试以下代码,它的工作对我来说……

  public static string sendtext = ""; private void button1_Click(object sender, EventArgs e) { sendtext = txt1.Text; Form2 frm = new Form2(); frm.Show(); } 

在form2的表单加载事件中访问此数据

  private void Form2_Load(object sender, EventArgs e) { txt2.Text = Form1.sendtext; } 

请享用

**

100%工作

**

取2个表单,Form1和Form2,Form1包含textbox1和Frm1Btn1,Form2包含Frm2Btn1,现在打开form1,点击打开form2

// FORM1编码:

 private void Frm1Btn1_Click(object sender, EventArgs e) { Form2 obj=new Form2(); obj.showDialog(); } 

// FORM2编码:

 private void Frm2Btn1_Click(object sender, EventArgs e) { Form1 objj = new Form1(); objj =(Form1) Application.OpenForms["Form1"]; objj.textBox1.Text = "Salil"; objj=null; } 

如果它是一个Web表单MSDN建议在页面顶部请求文本。

  <%@ PreviousPageType VirtualPath="~/SourcePage.aspx" %> 

然后在page_load中

  if (PreviousPage != null) { TextBox SourceTextBox = (TextBox) PreviousPage.FindControl("TextBox1"); if (SourceTextBox != null) { Label1.Text = SourceTextBox.Text; } } 

确保将Form1的textBox1.Text属性初始化为您可以看到的实际字符串 。 我怀疑,因为你使用的是Form1的默认实例,所以TextBox1.Text属性是String.Empty ,这是默认值(为什么你没有得到任何看似你正在期待的可见文本) 。

表格1

 //Declare a Static variable public static string txtInput=string.empty;` //Set textBox Value to this variable on any event txtInput=textBox1.Text; 

表格2

 // Now Call this variable from the Instance of form 1 string getForm1Value=string.empty; getForm1Value=Form1.txtinput; 

您可以轻松地将文本框值存储到变量中,然后使用构造函数将其传递给另一个表单。 例如

 string textboxvalue=textbox1.Text(); form2 win = new form2(textboxvalue); 

这里form2是您要发送值的表单,win是该表单的实例。

在Windows应用程序中,只需转到另一个表单设计器并将该工具声明为public。 您可以从任何表单访问该工具。

这是在结果表格上:

  1. 放一个textBox。
  2. 您必须单击该表单,直到代码编辑器弹出。
  3. 在表单加载上,您插入代码。

码:

 Form1 frm1 = new Form1(); frm1.Show(); textBox1.Text = ((TextBox)frm1.Controls["textBox1"]).Text; 

在第一个表格上:

  1. 放一个文本框和一个按钮。

按钮代码(在表单加载时,输入此代码):

 this.Close(); 

在form1:hotcoffee

传递文本框private void button15_Click(object sender,EventArgs e)//获取creditcard上的项目

  { this.Hide(); hotcoffepayment p = new otcoffepayment(richTextBox2.Text);//pass form1 textbox p.Show(); } 

在form2:hotcoffepayment

public hotcoffepayment(string strValue)

  { InitializeComponent(); textBox1.Text = strValue;//here mention the form2 textbox name } 

您必须将textbox的modifer属性设置为“public”,以便从另一个表单中获取其值

对我来说,最简单的方法是这样的:

  1. 将Textbox修饰符属性更改为Public;
  2. 你在哪里打开你的第二张表格,打开就像这样: frm.Show(this);
  3. 在您的Form2中,无论您需要文本,只需执行以下操作:
 var frm = this.Owner; var ctl = frm.Controls.Find("textboxOnform1", true).FirstOrDefault() as Control; TextBox Txt = (TextBox)ctl; //And then you can use It textboxt1.Text= Txt.Text; 

你可以做相反的事情 – 以同样的方式发回文本。

表格1

  class Form1 { public System.Windows.Forms.TextBox textBox1; // we need to make it public Textbox private System.Windows.Forms.Button button1; } 

在Form2上按钮单击或任何其他事件使用

表格2

 private void button1_Click(object sender, EventArgs e) { Form1 _form1 = new Form1(); this._form2TextBox.Text = _form1.textBox1.Text; } 

有关详细信息,请访问MSDN