2012-11-26 51 views

回答

1

在Form2上創建一個屬性並從Form1中進行設置。就像這樣:

Form1中

public partial class Form1 : Form 
{ 
    Form2 frm2; 
    public Form1() 
    { 
     InitializeComponent(); 
     frm2 = new Form2(); 
     frm2.Show(this); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     frm2.ModifyTextBoxValue = textBox1.Text; 
    } 
} 

窗體2

public partial class Form2 : Form 
{ 
    public Form2() 
    { 
     InitializeComponent(); 
    } 

    public string ModifyTextBoxValue 
    { 
     get { return textBox1.Text; } 
     set { textBox1.Text = value; } 
    } 
} 
+0

爲什麼會出現在textbox1.Text錯誤,當我加入公共字符串ModifyTextBoxValue? – user1647667

+0

@ user1647667你得到了什麼錯誤,上面的代碼工作正常 –

+0

textBox1.Text在當前上下文中不存在。我只是在包含文本框相同的屬性名稱的form2上添加文本框。和form1中相同的文本框屬性名稱@Mark Hall – user1647667

0

你可以使用.TAG屬性(看看我的問題here 簡單的方式做到這一點是這樣的: 添加其他的textBox在窗口2

在Form1做到這一點。這個代碼將存儲texBox.text在Form1

try 
{ 
    private void change_Click(object sender, EventArgs e) 
    { 
     form1 frm1 = new form(); 
     frm1.Tag = this.textBox1.text; 
     frm1.ShowDialog(); 
    } 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
} 

然後寫這樣當您加載窗體2。這一代碼將與texBox的值替換texBox2值1

string myText = (string)this.Tag; 
    this.textBox2.text = myText; 
相關問題