我需要帮助将Form2.textbox1中的输入传递给Form1.sti Form1 =主要工作窗口 Form2 =弹出窗口,输入路径。
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
public string sti { get; set; }
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = sti;
this.Close();
}
}
public partial class Form1 : Form
{
int CountR = 1;
public Form1()
{
InitializeComponent();
}
public string sti { get; set; }
public void Form1_Load(object sender, EventArgs e)
{
Form2 popup = new Form2();
popup.ShowDialog();
popup.Dispose();
}
public void button1_Click(object sender, EventArgs e)
{
Label7.Text = sti;
}
但它总是会返回Null。
我已经用这种方式构建了它,因为我不希望用户只使用IT管理员的路径。
提前谢谢
答案 0 :(得分:0)
为什么不在Form2
上创建另一个用于从Form1
,添加:( 就像在form1 中一样)
Form2
因此,在您的public string sti { get; set; }
代码中,它将如下所示
Form1
并在public void Form1_Load(object sender, EventArgs e)
{
Form2 popup = new Form2();
popup.sti = sti;
}
Form2
答案 1 :(得分:0)
如果你想在表单之间传递数据,那么有一个很好的教程: http://www.codeproject.com/Articles/17371/Passing-Data-between-Windows-Forms
答案 2 :(得分:0)
您正在创建一个新的。 Form1
,其中没有对您创建的Form2
的引用
您更改了示例,您创建了2个不同的sti属性,在关闭Form2之前,需要将Form2
sti值分配给Form1
中的属性。
此代码应该适合您。
<强> Form1中强>
public partial class Form1 : Form
{
public string sti { get; set; }
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 popup = new Form2();
popup.ShowDialog();
sti = popup.sti;
popup.Close();
popup.Dispose();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = sti;
}
}
<强>窗体2 强>
public partial class Form2 : Form
{
public string sti { get; set; }
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
sti = textBox1.Text;
this.Hide();
}
}
您还可以创建一个可以从两个表单引用的静态类,这样您只处理一个sti属性。像这样:
<强> Form1中强>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
myProperties.sti = "Hello";
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 popup = new Form2();
popup.ShowDialog();
popup.Dispose();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = myProperties.sti;
}
}
public static class myProperties
{
public static string sti { get; set; }
}
<强>窗体2 强>
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
textBox1.Text = myProperties.sti;
}
private void button1_Click(object sender, EventArgs e)
{
myProperties.sti = textBox1.Text;
this.Close();
}
}
答案 3 :(得分:0)
尝试:
public partial class Form2 : Form
{
public String sti { get; set; }
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 frm = new Form3();
frm.ShowDialog();
sti = frm.sti;
textBox1.Text = sti;
}
}
其他形式:
public partial class Form3 : Form
{
public String sti { get; set; }
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
sti = textBox1.Text;
this.Close();
}
}