Form1中代码
static string value1;
private static string getvalue;
private void button1_Click(object sender, EventArgs e)
{ value1= textBox1.Text;
Form2 f2 = new Form2();
f2.Show();
}
public static string GetV
{
set { getvalue = value1; }
get { return getvalue; }
}
Form2中的代码
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = Form1.GetV;
}
我想实现的是把通过单击form1中的button把form1中的textbox内容传给Form2中的text,运行后Form2的textbox中为空,能帮忙在我的程序上修改嘛?我也说不清楚我那里错了。谢谢
注:2个窗口不存在父子关系。
static string value1;
private static string getvalue;
private void button1_Click(object sender, EventArgs e)
{ value1= textBox1.Text;
Form2 f2 = new Form2();
f2.Show();
}
public static string GetV
{
set { getvalue = value1; }
get { return getvalue; }
}
Form2中的代码
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = Form1.GetV;
}
我想实现的是把通过单击form1中的button把form1中的textbox内容传给Form2中的text,运行后Form2的textbox中为空,能帮忙在我的程序上修改嘛?我也说不清楚我那里错了。谢谢
注:2个窗口不存在父子关系。
{
Form2 f2 = new Form2();
f2.Value = this.textBox1.Text.Trim();
f2.Show();
}Form2中代码private string strValue;
public string Value
{
get { return strValue; }
set { strValue = value; }
}//窗体的Load事件
private void Form2_Load(object sender, System.EventArgs e)
{
this.textBox1.Text = Value;
}
是属性set设置的固定格式
Form2中的代码
static string value1;
private static string getvalue;
private void button1_Click(object sender, EventArgs e)
{
GetV = textBox1.Text.trim();
Form2 f2 = new Form2();
f2.Show();
}
public static string GetV
{
set { getvalue = value1; }
get { return getvalue;}
} Form2中的代码
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = Form1.GetV;
}
在Form2中你取的是GetV 这个属性的值,所以在Form1中应当给属性GetV 赋值
建议先将光标放到字段上,再用快捷键Ctrl+R,Ctrl+E 再回车
或 直接右键重构 字段即可自动生成属性在取值前先判断下是否为null较安全哦
不可能的,我试过的。
Form2中的
private string strValue;
public string Value
{
get { return strValue; }
set { strValue = value; }
}
要写成全局变量
可以form2的textbox1設成public的 public System.Windows.Forms.TextBox textBox1;//在Form2.Designer.cs中。在form1中寫上 private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.textBox1.Text = textBox1.Text;
form2.Show();
}
LZ方法本身的问题是你根本就没有给 getvalue 赋过值啊.你赋值的是value1。
在
value1= textBox1.Text;后加上GetV=value1