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

部署运行你感兴趣的模型镜像

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

        }

    }

 

您可能感兴趣的与本文相关的镜像

Facefusion

Facefusion

AI应用

FaceFusion是全新一代AI换脸工具,无需安装,一键运行,可以完成去遮挡,高清化,卡通脸一键替换,并且Nvidia/AMD等显卡全平台支持

C# 中编一个简单的登录器程序通常涉及以下几个步骤: 1. 创建一个窗体应用程序项目(Form Application): - 打开Visual Studio,选择“创建新项目”,然后选择Windows Forms App (.NET Framework)。 2. 设计用户界面 (UI): - 添加两个控件:一个TextBox用于输入用户名(Username TextBox),另一个用于密码(Password TextBox)。 - 创建一个Button(如Login Button),当点击时触发验证操作。 - 可能还需要添加一个Label用于显示状态信息,例如错误提示。 3. 编后台逻辑: - 在`LoginButton_Click`事件处理函数中,首先检查输入是否为空: ```csharp private void LoginButton_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(UsernameTextBox.Text) || string.IsNullOrEmpty(PasswordTextBox.Text)) { MessageBox.Show("请输入用户名和密码"); return; } } ``` 4. 进行认证逻辑(这里仅做示例,实际应用应连接数据库或服务进行验证): ```csharp bool AuthenticateUser(string username, string password) { // 模拟简单的用户名和密码匹配,实际应用需要替换为实际验证逻辑 return username == "admin" && password == "password"; } // 在事件处理器中继续 private void LoginButton_Click(object sender, EventArgs e) { // ... if (AuthenticateUser(UsernameTextBox.Text, PasswordTextBox.Text)) { MessageBox.Show("登录成功!"); // 登录成功后,可以关闭窗口或跳转到其他页面 this.Close(); } else { MessageBox.Show("用户名或密码错误!"); } } ``` 5. 错误处理和用户体验优化: - 可以添加更多的错误检查和提示信息,比如密码复杂度检查等。 这是一个非常基础的登录器示例,实际应用会更复杂,包括数据加密存储、安全性和性能考虑等。记得在部署前测试并保护好敏感信息。
评论 58
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值