30.鼠标处理事件和键盘处理事件
鼠标相关的事件大致有6中,MouseHover,MouseLeave,MouseEnter,MouseMove,MouseDown,MouseUp。
键盘相关的事件有3种,KeyDown,KeyUp,KeyPress。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication15
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
this.textBox1.Text = Convert.ToString( e.X );
this.textBox2.Text = Convert.ToString( e.Y );
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if ( e.Button == MouseButtons.Left )
MessageBox.Show( "左键" );
if ( e.Button == MouseButtons.Middle )
MessageBox.Show( "中键" );
if ( e.Button == MouseButtons.Right )
MessageBox.Show( "右键" );
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// MessageBox.Show( "你所按的键为:" + e.KeyCode.ToString() );
}
}
}
看下键盘的事件。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication16
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != 8 && e.KeyChar != 13 && !char.IsDigit(e.KeyChar))
{
MessageBox.Show( "只能输入数字!!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information );
e.Handled = true;
}
}
}
}
本文出自 “郭俊的博客” 博客,转载请与作者联系!
原文地址:http://10093949.blog.51cto.com/10083949/1632131