·天新网首页·加入收藏·设为首页
首页|笔记本|手机|数码相机|摄像机|MP3/MP4|主板|内存|显示器|办公|打印机|下载|开发|汽车|学院|业界
硬件|台式机|数码|数字家庭|投影仪|GPS/CPU|显卡|硬盘|服务器|网络|一体机|驱动|源码|游戏|考试|报价
您现在的位置:天新网 > 软件开发 > 开发文摘 > .Net文摘
如何用C#写一个简单的Login窗口
http://dev.21tx.com 2007年09月30日 愚翁

最近,看到网上经常会问如何进行窗口跳转,大多数的问题都是牵扯到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;

 

                // Set dialog result with OK

                this.DialogResult = DialogResult.OK;

            }

            else

            {

                // Wrong username or password

                nLoginCount++;

                if( nLoginCount == MAX_LOGIN_COUNT )

                    // Over 3 times

                    this.DialogResult = DialogResult.Cancel;

                else

                {

                    MessageBox.Show( "Invalid user name and password!" );

                    txtUserName.Focus();

                }

            }

        }

 

        private void btnCancel_Click(object sender, System.EventArgs e)

        {

            // Set dialog result with Cancel

            this.DialogResult = DialogResult.Cancel;

        }

 

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

private void frmLogin_Closing(object sender, System.ComponentModel.CancelEventArgs e)

{

    // Check whether form is closed with dialog result

    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 = "";

        }

    }

 

上一篇: “循环赛日程安排”问题的分而治之解决算法
下一篇: “插花问题”的动态规划法算法

Google
 
热点文章
关于我们 | 联系我们 | 广告服务 | 工作机会 | 版权声明 | 欢迎投稿 | 网站地图
Copyright © 2000-2008 , www.21tx.com , All Rights Reserved .
© 晨新科技 版权所有 Created by TXSite.net