如何在 C# 中将 Form1 值传递给 Form3 [英] How to pass Form1 value to Form3 in C#

查看:66
本文介绍了如何在 C# 中将 Form1 值传递给 Form3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将form1的值直接传递给Form3.

How to pass value of form1 directly to Form3.

Form1中,我有按钮和一个名为txtUsernameTextbox.

In Form1 i have button and a Textbox named txtUsername .

   public string username = string.Empty;
 private void LoginBT_Click(object sender, EventArgs e)
      {
         username = txtUsername.Text;
         Form3 form  = new Form3(username);

         Form2 frm = new Form2();
         frm.Show();
         this.Hide
}

并且在 From2 我有这个

 private void Button_Click(object sender, EventArgs e)
      {
            Form3 form = new Form3();
            form.Show();
            this.Hide();
       }

并在 Form3

public string Name;
        public Form3(string CName)
                {

                    InitializeComponent();
                    Name= Cname;
                }

private void frmTicketandCottages_Load(object sender, EventArgs e)
        {
      MessageBox.Show(Name); 
         }

显示文本.

但是当我显示文本时,我只会得到一个空文本

But when i display the text i only get an Empty Text

还有其他方法可以做到这一点吗?

Any other method to do this ?

推荐答案

为什么要直接把值传给form3还要去form 2?无论如何,你可以这样做...

Why do you still need to go to form 2 if you want to directly pass the value to form3? Anyway you can do it this way...

表单 1:

     private void LoginBT_Click(object sender, EventArgs e)
     {
     username = txtUsername.Text;


     Form2 frm = new Form2(username);
     frm.Show();
     this.Hide
     }

表单 2:

    string Name;
    public Form2(string CName)
            {

                InitializeComponent();
                Name= Cname;
            }

   private void Button_Click(object sender, EventArgs e)
  {
        Form3 form = new Form3(Name);
        form.Show();
        this.Close();
   }

表单3:

    string Name;
    public Form3(string CName)
            {

                InitializeComponent();
                Name= Cname;
            }

    private void frmTicketandCottages_Load(object sender, EventArgs e)
            {
                MessageBox.Show(Name); 
            }

这篇关于如何在 C# 中将 Form1 值传递给 Form3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆