黑马程序员技术交流社区

标题: C#键盘事件 [打印本页]

作者: 回忆初中时候    时间: 2014-3-22 21:32
标题: C#键盘事件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 键盘事件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_KeyPress(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                textBox1.Text = "你按了Enter键";
            }
        }
    }
}


运行程序后 用鼠标点击了窗体,按Enter键后,textBox1控件没有任何反应!!!
请问是问什么??
作者: lmm    时间: 2014-3-26 18:30
使用KeyDown事件吧.
KeyPress主要用来捕获数字(注意:包括Shift+数字的符号)、字母(注意:包括大小写)、小键盘等除了F1-12、SHIFT、Alt、Ctrl、Insert、Home、PgUp、Delete、End、PgDn、ScrollLock、Pause、NumLock、{菜单键}、{开始键}和方向键外的ANSI字符




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2