Winform 中有关Form1、Form2两个窗体简单变量的使用

本文介绍如何在Winform应用程序中实现Form1登录界面与Form2主界面间的用户名和密码传递,通过静态变量和对话框结果进行控制流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

、Winform中有两个窗口Form1、Form2,运行过程中Form1操作完就关闭,只显示form2主窗口,以及Form2中使用Form1中变量的问题:

  1.首先在Form1中,从工具箱中拖两个“label控件”,命名为:txtuser(用户名)和txtpwd(密码),两个TextBox、命名为:txtUser.Text、txtPwd.Text,两个Button,命名为btnLog、btnCancel方法如下:

   public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();      
        }
        public static string User= this.txtUser.Text
        public static string Pwd= this.txtPwd.Text;
        private void btnLog_Click(object sender, EventArgs e)

       {

            User = this.txtUser.Text;
            Pwd = this.txtPwd.Text;

                  .......

                   this.DialogResult = DialogResult.OK;  

      }       

         private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

   }

2. Form2中也做一下定义:

   public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
          
        }
        public static string User = Form1.User;
        public static string Pwd = Form1.Pwd ;   

    }

3.应用程序的主入口点 Main()中,做如下更改:

 

static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //登录成功后,关闭登录窗口
            Form1 fm1=new Form1 ();
            if (fm1.ShowDialog() == DialogResult.OK)
            {
                Application.Run(new Form2());

            }   
        }
    }

以上方法即是对两个窗口的简单操作!

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值