从父表单和子表单传递参数值 C#
我有两个表单,一个带有“添加”按钮,它加载带有两个文本框和一个提交按钮的第二个表单。
首先,我需要一种将文本框值传递给表单 1(父表单)并使表单 2 在提交时关闭的方法。
我该如何执行此操作?到目前为止,我已经编写了这段代码,但它不起作用
private void button1_Click(object sender, EventArgs e)
{
emailForm EmailF = new emailForm();
if ((EmailF.Username != null && EmailF.Password != null))
{
string user = EmailF.Username;
string pass = EmailF.Password;
}
并且在 emailForm.cs 中
private void button1_Click(object sender, EventArgs e)
{
username = username_textbox.Text;
password = username_textbox.Text;
Close();
}
public string Username
{
get { return username; }
set { this.username = value; }
}
public string Password
{
get { return password;}
set { this.password = value; }
}
I have two forms, one with a button 'Add' which loads the second form with two textboxes and a submit button.
First of all I need a way of passing the text box values to form 1 (parent) and making form 2 close on the submit..
How can I do this? Until now I have written this code but it is not working
private void button1_Click(object sender, EventArgs e)
{
emailForm EmailF = new emailForm();
if ((EmailF.Username != null && EmailF.Password != null))
{
string user = EmailF.Username;
string pass = EmailF.Password;
}
and in the emailForm.cs
private void button1_Click(object sender, EventArgs e)
{
username = username_textbox.Text;
password = username_textbox.Text;
Close();
}
public string Username
{
get { return username; }
set { this.username = value; }
}
public string Password
{
get { return password;}
set { this.password = value; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要查看
Form.ShowDialog()
。这将执行您想要的操作,当用户关闭对话框窗口时,您可以确保他们单击“确定”(或其他任何内容),然后从表单中获取值。You need to look at
Form.ShowDialog()
. This will do what you want, and when the user closes the dialog window you can make sure they clicked "OK" (or whatever else) and then grab the values from the form.