登录页面出来的时候,可否将focus直接置到“用户名”输入框里面?

博客提出登录页面显示时,希望将焦点直接置于“用户名”输入框,以提升便捷性。当前情况是需按三次Tab或用鼠标精确操作才能将焦点移至该输入框。

登录页面出来的时候,可否将focus直接置到“用户名”输入框里面?这样比较方便。目前的情形是:登录页面出来之后,需要按三次Tab才能将focus切到“用户名”输入框;或者需要鼠标做精确定位置放focus。

using System; using System.Windows.Forms; using System.Data.OleDb; using System.IO; namespace 千依零食 { public partial class LoginForm : Form { public LoginForm() { InitializeComponent(); InitializeCustomComponents(); // 窗体加载时自动读取记住的密码 LoadRememberedPwd(); } // 2. 读取本地记住的密码(FileStream文件IO,教材知识点) private void LoadRememberedPwd() { // 判断文件是否存在 if (File.Exists(pwdFilePath)) { // 使用using语句自动释放资源(教材推荐写法) using (FileStream fs = new FileStream(pwdFilePath, FileMode.Open, FileAccess.Read)) { using (StreamReader sr = new StreamReader(fs)) { // 读取文件内容(格式:账号|密码) string content = sr.ReadLine(); if (!string.IsNullOrEmpty(content)) { string[] arr = content.Split('|'); if (arr.Length == 2) { textBox1.Text = arr[0]; // 填充账号 textBox2.Text = arr[1]; // 填充密码 checkBox1.Checked = true; // 勾选记住密码 } } } } } } // 3. 保存密码到本地文件(FileStream文件IO) private void SaveRememberedPwd(string userId, string pwd) { using (FileStream fs = new FileStream(pwdFilePath, FileMode.Create, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs)) { // 按指定格式写入:账号|密码 sw.WriteLine(string.Format("{0}|{1}", userId, pwd)); } } } // 4. 验证用户登录(OleDb数据库操作,教材核心知识点) private bool VerifyUser(string userId, string pwd, out string userType) { userType = ""; // 参数化查询:防止SQL注入 string sql = "SELECT UserType FROM tUser WHERE UserID=@UserID AND Password=@Password"; using (OleDbConnection conn = new OleDbConnection(connStr)) { try { conn.Open(); // 打开数据库连接 OleDbCommand cmd = new OleDbCommand(sql, conn); // 添加参数(教材参数化查询写法) cmd.Parameters.AddWithValue("@UserID", userId); cmd.Parameters.AddWithValue("@Password", pwd); // 执行查询(ExecuteScalar:返回单行单列结果) object result = cmd.ExecuteScalar(); if (result != null) { userType = result.ToString(); return true; // 验证成功 } return false; // 验证失败 } catch (Exception ex) { MessageBox.Show("数据库查询失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } } private string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=SnackShop.mdb;"; // 记住密码的文本文件路径 private string pwdFilePath = "user_pwd.txt"; // 初始化控件(设计器未生成的部分) private void InitializeCustomComponents() { // 初始化ComboBox comboBox1.Items.Clear(); comboBox1.Items.Add("收银员"); comboBox1.Items.Add("店长"); comboBox1.SelectedIndex = 0; } // 当窗体关闭时,确保整个程序退出 protected override void OnFormClosing(FormClosingEventArgs e) { base.OnFormClosing(e); Application.Exit(); } private void LoginForm_Load(object sender, EventArgs e) { } private void button1_Click_1(object sender, EventArgs e) { // 输入校验 if (string.IsNullOrEmpty(textBox1.Text)) { MessageBox.Show("请输入账号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); textBox1.Focus(); // 聚焦到账号输入框 return; } if (string.IsNullOrEmpty(textBox2.Text)) { MessageBox.Show("请输入密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); textBox2.Focus(); return; } // 验证用户 string userType; bool isSuccess = VerifyUser(textBox1.Text, textBox2.Text, out userType); if (isSuccess) { // 记住密码:勾选时保存,未勾选时删除文件 if (checkBox1.Checked) { SaveRememberedPwd(textBox1.Text, textBox2.Text); } else if (File.Exists(pwdFilePath)) { File.Delete(pwdFilePath); // 删除记住的密码文件 } // 登录成功:传递用户类型,打开主菜单窗体(Form2) MessageBox.Show(string.Format("登录成功!欢迎{0}用户", userType), "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Hide(); // 传递用户类型到主窗体 MainForm main = new MainForm(userType); main.Show(); this.Close(); } else { MessageBox.Show("账号或密码错误!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error); textBox2.Clear(); // 清空密码框 textBox2.Focus(); } } private void button2_Click_1(object sender, EventArgs e) { // 退出应用程序 DialogResult result = MessageBox.Show("确定要退出吗?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { Application.Exit(); } } } } 这是LoginForm.cs的程序,为了配套主菜单程序,需要修改LoginForm.cs的哪里吗
最新发布
12-28
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值