当前位置:网站首页>C mouse event and keyboard event of C (XXVIII)

C mouse event and keyboard event of C (XXVIII)

2022-07-06 03:47:00 camellias_

Look at the mouse event today 、 Keyboard events .

1: There are two kinds of mouse events :

EventArgs:MouseEnter/MouseLeave etc.

MouseEventArgs:MouseDoubleClick、MouseDown、MouseUp、MouseHover、MouseMove.

private void button1_MouseEnter(object sender, EventArgs e)
        {
            label1.Text = " Button entry event ";
        }
 
        private void button1_MouseLeave(object sender, EventArgs e)
        {
            label1.Text = " Button leave event ";
        }
 
        private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            label2.Text = " Double click the form event ";
        }
 
        private void button2_MouseDown(object sender, MouseEventArgs e)
        {
            label2.Text = " Press the button 2";
        }
 
        private void button2_MouseUp(object sender, MouseEventArgs e)
        {
            label2.Text = " Release the button 2";
        }
 
        private void button2_MouseHover(object sender, EventArgs e)
        {
            label3.Text = " Mouse on button 2 Stayed on for some time ";
        }
 
        private void button2_MouseMove(object sender, MouseEventArgs e)
        {
            label4.Text = " Mouse on button 2 Go through ";
        }

2: Keyboard events

KeyEventArgs:

KeyPressEventArgs:

KeyPress: It can only be used when the focus is on the control .( Press and release to happen )

KeyDown: Occurs when the key is pressed

KeyUp: It happens when you lift the key

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            label1.Text += e.KeyChar;
        }

Keyboard events are similar to mouse events , Here are just some concepts , Relevant cases will be added later .

Test using code :

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 mianbanGc
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_MouseEnter(object sender, EventArgs e)
        {
            label1.Text = " Button entry event ";
        }
 
        private void button1_MouseLeave(object sender, EventArgs e)
        {
            label1.Text = " Button leave event ";
        }
 
        private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            label2.Text = " Double click the form event ";
        }
 
        private void button2_MouseDown(object sender, MouseEventArgs e)
        {
            label2.Text = " Press the button 2";
        }
 
        private void button2_MouseUp(object sender, MouseEventArgs e)
        {
            label2.Text = " Release the button 2";
        }
 
        private void button2_MouseHover(object sender, EventArgs e)
        {
            label3.Text = " Mouse on button 2 Stayed on for some time ";
        }
 
        private void button2_MouseMove(object sender, MouseEventArgs e)
        {
            label4.Text = " Mouse on button 2 Go through ";
        }
 
        public string str = "";
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            str += e.KeyChar;
            label5.Text += e.KeyChar;
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
 
        private void label5_Click(object sender, EventArgs e)
        {
 
        }
    }
}

There are good suggestions , Please enter your comments below .

Welcome to personal blog
https://guanchao.site

Welcome to the applet :

 Insert picture description here

原网站

版权声明
本文为[camellias_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060338512138.html