A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© §風過無痕§ 金牌黑马   /  2013-10-14 21:01  /  2032 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 §風過無痕§ 于 2013-10-14 21:07 编辑

平常我们在练习编程模拟登陆界面的时候!当我们在“用户名”文本框中输入值,并按下enter键时,将鼠标焦点移动到“密码”文本框,同理  当在“密码”文本框中输入值,并按下Enter键时  将鼠标焦点移动到 “登陆”按钮

同理推出 按其他键时也能移动鼠标焦点!

纯粹以娱乐方式玩玩  请大神勿喷噢!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;

  9. namespace 获取焦点
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }

  17.         private void btnLogin_Click(object sender, EventArgs e)
  18.         {
  19.             if (txtUserName.Text == "")
  20.             {
  21.                 MessageBox.Show("用户名不能为空!");
  22.                 return;
  23.             }
  24.             if (txtPSW.Text == "")
  25.             {
  26.                 MessageBox.Show("请输入登陆密码!");
  27.                 return;
  28.             }
  29.             //.............
  30.         }

  31.              private void btnCancel_Click(object sender, EventArgs e)
  32.         {
  33.             this.Close();//关闭当前窗体
  34.         }

  35.         //获取移动鼠标焦点
  36.         private void txtUserName_KeyPress(object sender, KeyPressEventArgs e)
  37.         {
  38.             if (e.KeyChar == '\r')//判断是否按下Enter键
  39.             {
  40.                 txtPSW.Focus();//将鼠标焦点移动到 “密码”文本框
  41.             }
  42.         }

  43.         private void txtPSW_KeyPress(object sender, KeyPressEventArgs e)
  44.         {
  45.             if (e.KeyChar == '\r')//判断是否按下Enter键
  46.             {
  47.                 btnLogin.Focus();//将鼠标焦点移动到“登陆”按钮
  48.             }
  49.         }
  50.         //KeyPressEventArgs指定在用户按键时撰写的字符

  51.     }
  52. }
复制代码



评分

参与人数 1技术分 +1 收起 理由
追溯客 + 1

查看全部评分

5 个回复

倒序浏览
待我慢慢学习
回复 使用道具 举报
学习了,谢谢楼主分享。
回复 使用道具 举报
liubeimeng 发表于 2013-10-15 11:13
学习了,谢谢楼主分享。

:handshake加油噢!一起努力
回复 使用道具 举报
非常值得学习。。
回复 使用道具 举报

感谢支持!共勉!!!:loveliness:
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马