从父表单和子表单传递参数值 C#

发布于 2024-12-10 10:46:20 字数 992 浏览 1 评论 0原文

我有两个表单,一个带有“添加”按钮,它加载带有两个文本框和一个提交按钮的第二个表单。

首先,我需要一种将文本框值传递给表单 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月朦胧 2024-12-17 10:46:20

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.

初与友歌 2024-12-17 10:46:20
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();
}
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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文