我有两个表单,一个带有“添加”按钮,可以加载第二个表单,其中包含两个文本框和一个提交按钮。
首先,我需要一种方法将文本框值传递给表单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; }
}
答案 0 :(得分:2)
您需要查看Form.ShowDialog()
。这将完成您想要的操作,当用户关闭对话框窗口时,您可以确保它们单击“确定”(或其他任何内容),然后从表单中获取值。
答案 1 :(得分:0)
public void ShowMyDialogBox()
{
Form2 testDialog = new Form2();
// Show testDialog as a modal dialog and determine if DialogResult = OK.
if (testDialog.ShowDialog(this) == DialogResult.OK)
{
// Read the contents of testDialog's TextBox.
this.txtResult.Text = testDialog.TextBox1.Text;
}
else
{
this.txtResult.Text = "Cancelled";
}
testDialog.Dispose();
}