簡體   English   中英

MouseUp事件錯誤

[英]MouseUp Event Error

我是WinForms事件的新手,但遇到一個奇怪的錯誤,在啟動控件時寫了一下:

this.MouseUp += MouseUpMethod;

但是問題是,當我從控件中釋放鼠標按鈕時,該程序會識別為在控件上釋放鼠標。 我無法理解此錯誤。 有沒有人得到這個錯誤?

這是因為,默認情況下,您的控件會捕獲鼠標。 只需在MouseDown事件處理程序的 某處 Control.Capture設置為false ,例如:

void MouseDown(object sender, MouseEventArgs e) {
    this.Capture = false;
}

或者,只需在MouseUp中檢查鼠標仍在控件內:

void MouseUp(object sender, MouseEventArgs e) {
    if (ClientRectangle.Contains(PointToClient(Cursor.Position))) {
        // Your code here
    }
}

看到,您需要在InitializeComponent()之后將事件與事件處理程序關聯

    public Form1()
    {
        InitializeComponent();
        this.button2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button2_MouseUp);
    }

那么您的事件處理程序應該是

    private void button2_MouseUp(object sender, MouseEventArgs e)
    {
        //Do stuff here
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM