程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> c#-C#如何實現拖動按鈕時不觸發Click事件

c#-C#如何實現拖動按鈕時不觸發Click事件

編輯:編程解疑
C#如何實現拖動按鈕時不觸發Click事件

如題,做了一個在運行時可拖動的按鈕,可是每次一拖動就會觸發Click事件,怎樣才能實現只拖動按鈕而不觸發Click事件,Click事件是必要的,不可省

最佳回答:


 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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Point buttonlocation;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("button1");
        }

        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            buttonlocation = e.Location;            
        }

        private void button1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                this.button1.Click -= button1_Click;
                this.button1.Location = new Point(this.button1.Left + (e.X - buttonlocation.X), this.button1.Top + (e.Y - buttonlocation.Y));
            }
        }

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            buttonlocation = e.Location;
            this.button1.Click -= button1_Click;
            this.button1.Click += button1_Click;
        }
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved