首页 > 开发 > 综合 > 正文

如何用C#写一个简单的Login窗口

2024-07-21 02:27:06
字体:
来源:转载
供稿:网友

看到网上经常会问如何进行窗口跳转,大多数的问题都是牵扯到login窗口。其实,在visual studio 6以来,比较正确的做法,是判断login窗口的返回值,然后决定是否打开主窗体,那么在c#中也是一样的。 
 

具体做法如下:

首先,创建login窗口,然后添加相应的输入框和按钮,设置窗口的acceptbutton为窗体的确认按钮,而cancelbutton为窗体的取消按钮。例如:

            this.acceptbutton = this.btnok;

            this.cancelbutton = this.btncancel;

 

定义确定按钮以及取消按钮事件,如下:

        private void btnok_click(object sender, system.eventargs e)

        {

            // here is to use fixed username and password

            // you can check username and password from db

            if( txtusername.text == "admin" && txtpassword.text == "nopassword" )

            {

                // save login user info

                uilogin.username = txtusername.text;

                uilogin.password = txtpassword.text;

 

                this.dialogresult = dialogresult.ok;

            }

            else

            {

                nlogincount++;

                if( nlogincount == max_login_count )

                    this.dialogresult = dialogresult.cancel;

                else

                {

                    messagebox.show( "invalid user name and password!" );

                    txtusername.focus();

                }

            }

        }

 

        private void btncancel_click(object sender, system.eventargs e)

        {

            this.dialogresult = dialogresult.cancel;

        }

 

然后,在login窗体的closing事件中,要进行处理,如下:

private void frmlogin_closing(object sender, system.componentmodel.canceleventargs e)

{

    if( this.dialogresult != dialogresult.cancel &&

        this.dialogresult != dialogresult.ok )

        e.cancel = true;

}

 

除此外,login窗体一些辅助代码如下:

        private int nlogincount = 0;

        private const int max_login_count = 3;

 

        private userinfo uilogin;

        public frmlogin( ref userinfo ui )

        {

            //

            // required for windows form designer support

            //

            initializecomponent();

 

            // set login info to class member

            uilogin = ui;

        }

 

       调用的时候,要修改程序的main函数,如下:

        /// <summary>

        /// the main entry point for the application.

        /// </summary>

        [stathread]

        static void main()

        {

            userinfo ui = new userinfo();

            frmlogin mylogin = new frmlogin( ref ui );

            if( mylogin.showdialog() == dialogresult.ok )

            {

                //open your main form here

                messagebox.show( "logged in successfully!" );

            }

            else

            {

                messagebox.show( "failed to logged in!" );

            }

        }

 

       而附加的userinfo类如下:

    /// <summary>

    /// user info class

    /// </summary>

    public class userinfo

    {

        private string strusername;

        private string strpassword;

        public string username

        {

            get{ return strusername;}

            set{ strusername = value;   }

        }

        public string password

        {

            get{ return strpassword;}

            set{ strpassword = value;}

        }

        public userinfo()

        {

            strusername = "";

            strpassword = "";

        }

    }

 

,欢迎访问网页设计爱好者web开发。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表