有两个窗体 form1 form2 两个窗体各有一个textbox控件,想在form1中输入文本,传递给form2中的textbox控件中,我用过构造函数,也有把textbox的属性设为public 可都不能在form2中显示,各位有什么高见,给指导一下
在网上看了好多可是对我的程序都没有用,怎么回事啊 。泪奔~~~·
急啊 ~~~~~~~~~~~~
在网上看了好多可是对我的程序都没有用,怎么回事啊 。泪奔~~~·
急啊 ~~~~~~~~~~~~
在form2
private textBox txt;
public Form1(textBox txt)
{
this.txt=txt;
}
form2_load
{
//进行赋值
}
form1的话看你想在什么事件下弹出form2了
比如button_click
{
form2 f=new form2(this.textBox1);
f.show();}
比方你要这么做,
中不能带参数
{
......
}
如果想传文本框的内容,声明string类型就可以,想整个对象的话就声明textBox
通过绑定事件进行传值
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show(this);
}
} public partial class Form2 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = (Form1)this.Owner;
((TextBox)frm1.Controls["textBox1"]).Text = this.textBox2.Text;
this.Close();
}
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
string test = "传到Form2的值";
Form2 f2 = new Form2(test);
f2.DialogResult = DialogResult.No;
f2.ShowDialog();
if (f2.DialogResult==DialogResult.OK)
{
f2.Beack(ref test);
textBox1.Text = "FORM2传回的值>>" + test;
f2.Close();
}
}
}Form2 代码public partial class Form2 : Form
{
/// <summary>
/// 给构造函数加参数
/// </summary>
/// <param name="text"></param>
public Form2(string text)
{
InitializeComponent();
textBox1.Text = text;
} /// <summary>
///回传值函数,public
/// </summary>
/// <param name="text"></param>
public void Beack(ref string text)
{
text = this.textBox1.Text;
} /// <summary>
/// 关闭From2,
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
} }
form2 f2=new form2();
f2.textBox1.Text = this.textBox1.Text;
f2.show();
public class Session
{
private string text; public static void set(string t)
{
text = t;
} public static string get()
{
return text;
}
}
Form2 frm2=new Form2(this.TextBox1.text)
frm.show();
Form2 中原来的构造函数改为:
Public Form2(string txt)
{
InitializeComponent();
this.TextBox1.text=txt;
}
private string a;
public void GetData(string A)
{
this.a=A;
this.show;
}在form1中
Form2 f2=new Form2();
f2.GetData(this.TextBox.text);
lz看看这个!
2. Use form presenter interfaceYou can google these two approaches and find what you should do.