下面是使用C#编写的一段代码,实现用户在输入账号密码过程中的验证提示功能:
using System;
using System.Windows.Forms;
namespace UserInputValidation
{
public class LoginForm : Form
{
private TextBox accountTextBox;
private TextBox passwordTextBox;
private Button loginButton;
public LoginForm()
{
InitializeComponents();
}
private void InitializeComponents()
{
// 创建窗体和控件
this.Text = "用户登录";
this.FormBorderStyle = FormBorderStyle.FixedDialog;
Label accountLabel = new Label();
accountLabel.Text = "账号:";
accountLabel.Location = new Point(20, 20);
Label passwordLabel = new Label();
passwordLabel.Text = "密码:";
passwordLabel.Location = new Point(20, 50);
accountTextBox = new TextBox();
accountTextBox.Location = new Point(80, 20);
passwordTextBox = new TextBox();
passwordTextBox.Location = new Point(80, 50);
loginButton = new Button();
loginButton.Text = "登录";
loginButton.Location = new Point(80, 80);
loginButton.Click += LoginButton_Click;
// 将控件添加到窗体上
this.Controls.Add(accountLabel);
this.Controls.Add(passwordLabel);
this.Controls.Add(accountTextBox);
this.Controls.Add(passwordTextBox);
this.Controls.Add(loginButton);
// 设置窗体大小及启动位置等属性
this.ClientSize= new Size(200, 150);
}
private void LoginButton_Click(object sender, EventArgs e)
{
string account = accountTextBox.Text;
string password = passwordTextBox.Text;
if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(password))
{
MessageBox.Show("账号和密码不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// 进行账号密码验证的逻辑判断
if (account == "admin" && password == "123456")
{
MessageBox.Show("登录成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
// 执行登录成功后的操作
}
else
{
MessageBox.Show("账号或密码不正确!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
// 清空输入框,让用户重新输入
accountTextBox.Text = "";
passwordTextBox.Text = "";
}
}
public static void Main()
{
Application.Run(new LoginForm());
}
}
}
这段代码创建了一个简单的窗体应用程序,包含账号、密码文本框以及一个登录按钮。在点击登录按钮时,会对用户输入的账号和密码进行验证,并根据验证结果弹出相应的提示框。
请注意将代码保存为一个.cs文件,并使用合适的开发工具(如Visual Studio)编译和运行该程序。
内容由零声教学AI助手提供,问题来源于学员提问