惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Cisco Talos Blog
Cisco Talos Blog
C
Check Point Blog
博客园_首页
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Martin Fowler
Martin Fowler
Recorded Future
Recorded Future
S
Security @ Cisco Blogs
L
LINUX DO - 最新话题
博客园 - 司徒正美
P
Privacy International News Feed
G
Google Developers Blog
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
K
Kaspersky official blog
I
InfoQ
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Webroot Blog
Webroot Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
D
Docker
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Microsoft Azure Blog
Microsoft Azure Blog
Spread Privacy
Spread Privacy
量子位
H
Hacker News: Front Page
Simon Willison's Weblog
Simon Willison's Weblog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
SecWiki News
SecWiki News
S
Security Affairs
Latest news
Latest news
人人都是产品经理
人人都是产品经理
C
CERT Recently Published Vulnerability Notes
S
Security Archives - TechRepublic
V
Visual Studio Blog
T
Troy Hunt's Blog
S
Secure Thoughts
F
Fortinet All Blogs
V
V2EX
The Register - Security
The Register - Security
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - zhangh

[转]铁路订票网站个人的设计浅见 新写了一个Web即时通程序,基于HTTP长连接的服务器推技术。 .NET Framework 3.5 SP1安装时下载文件问题及精简方法2(转) [转] javascript基础知识大集锦(2) 迪士尼近期将拓展中国三线城市的市场 迪士尼礼品批发网 Web Service WSE 3.0 creation time in the timestamp cannot be in the future 局域网内机器访问服务器数据库连接,访问Web Service需要密码 - zhangh - 博客园 Web Service "Unsupported media type error" ubuntu下配置eclipse3.3 ubuntu下配置tomcat Ubuntu下配置samba实现文件夹共享 Open Source URL Rewriter for .NET / IIS / ASP.NET 要实现动态加载JS脚本有4种方法 - zhangh - 博客园 使用XML文件来动态配置ASP.NET MVC的Route规则 - zhangh - 博客园 window.opener - zhangh - 博客园 在WinForm中使用Web Services 来实现 软件 自动升级( Auto Update ) (C#) 多线程断点续传研究之二 多线程断点续传研究之一
winform登录窗口的正确操作办法
zhangh · 2009-01-07 · via 博客园 - zhangh

本文原出处:http://blog.csdn.net/knight94/archive/2006/04/06/652394.aspx 稍作修正

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

                    this.DialogResult=DialogResult.none;

                }

            }

        }

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

        }

    }