![](/img/trans.png)
[英]Passing text from one textBox to another in a same form using C# Windows Form
[英]Passing TextBox's text to another form in C#?
我已經嘗試過傳遞信息:
Form1 frm1 = new Form1();
textBox1.Text = ((TextBox)frm1.Controls["textBox1"]).Text;
這是在獲取信息的表單的表單負載中。 但是沒有文字。 我該如何解決? 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。 您可以從任何形式訪問該工具。
這在結果表單上:
碼:
Form1 frm1 = new Form1();
frm1.Show();
textBox1.Text = ((TextBox)frm1.Controls["textBox1"]).Text;
在第一種形式上:
按鈕代碼(在“表單加載”中,輸入此代碼):
this.Close();
在form1中:hotcoffee
傳遞文本框私有void button15_Click(object sender,EventArgs e)//在信用卡上獲取項目
{
this.Hide();
hotcoffepayment p = new otcoffepayment(richTextBox2.Text);//pass form1 textbox
p.Show();
}
形式2:hotcoffepayment
公共hotcoffepayment(字符串strValue)
{
InitializeComponent();
textBox1.Text = strValue;//here mention the form2 textbox name
}
您必須將文本框的modifer屬性設置為“ public”,以便從另一種形式獲取其值
對我來說,最簡單的方法是這樣的:
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上的Button單擊或任何其他事件使用
表格2
private void button1_Click(object sender, EventArgs e)
{
Form1 _form1 = new Form1();
this._form2TextBox.Text = _form1.textBox1.Text;
}
有關更多詳細信息,請訪問MSDN。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.