国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

如何用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開發。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 庆安县| 大港区| 高台县| 佛山市| 增城市| 梨树县| 客服| 建瓯市| 文山县| 清涧县| 航空| 曲阳县| 大洼县| 怀柔区| 贞丰县| 镇雄县| 分宜县| 禄劝| 儋州市| 怀来县| 广水市| 哈尔滨市| 苍山县| 江达县| 朝阳市| 阳信县| 天长市| 三台县| 彭阳县| 兴隆县| 南华县| 城口县| 贵州省| 郑州市| 延安市| 贺州市| 新密市| 丹江口市| 莱州市| 珠海市| 舒城县|