使用label控件来实现单击双击操作

使用label控件来实现单击双击操作

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 ClickEvent
{
public partial class Form1 : Form
{

    private DoubleClickLabel label1;
    private FormBorderStyle initialStyle;

    public Form1()
    {
        initialStyle = this.FormBorderStyle;
        this.ClientSize = new System.Drawing.Size(292, 266);
        label1 = new DoubleClickLabel();
        label1.Location = new Point(40, 40);
        label1.Click += new EventHandler(label1_Click);
        label1.AutoSize = true;
        this.AllowDrop = true;
        label1.Text = "Click or Double Click";
        label1.DoubleClick += new EventHandler(label1_DoubleClick);
        this.Controls.Add(label1);
    }

    private void label1_DoubleClick(object sender, EventArgs e)
    {
        this.FormBorderStyle = initialStyle;
        label1.Text = "双击触发了。。。";
        MessageBox.Show("Rolled back single click change.");
    }

    private void label1_Click(object sender, EventArgs e)
    {
        this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
        label1.Text = "单击触发了。。。";
    }
}

public class DoubleClickLabel : Label
{
    public DoubleClickLabel()
        : base()
    {
        // Set the style so a double click event occurs.
        SetStyle(ControlStyles.StandardClick | 
            ControlStyles.StandardDoubleClick, true);
    }
}

}

你可能感兴趣的:(winform,后端开发,C#开发,winform开发,Label控件,单击双击)