2013-10-04 69 views
3

我的問題是因爲我認爲簡單,但它變得更復雜一點,我試圖實現的是這樣的:我有幾個按鈕在我的WinForm中做幾乎相同的事情,所以我創建了一個事件來處理所有這些事件,現在我希望根據他們點擊哪個鼠標按鈕給他們一些其他的功能,例如,如果按鈕被點擊左鍵執行,但是如果它的右鍵單擊以其他方式執行,這可以實現嗎?,任何幫助將不勝感激。檢測按鈕是左鍵還是右鍵

我正在尋找一些語句像這樣也許......

private void buttons_Click(object sender, EventArgs e) 
     { 
if(e.buttons==mousebuttons.right) 
//do something 
else 
//do other thing 
} 

正如你所知道的是,這是無法實現的,因爲e上有沒有mouse button events的東西,造成i'ts是稱爲button click event

+1

A 「點擊」 的定義是一個單一的,左擊鼠標。您的用戶也會期待這一點。如果右鍵單擊程序中的某個按鈕,可能會引起混淆。也許你可以找到更符合標準的方法來開始你的行動。畢竟一個按鈕上只有一個文本,要傳達兩種意義的兩種含義,只有一個文本才能讓用戶點擊按鈕。 – nvoigt

+0

任何時候,我已經做了這樣的事情,我已經使用了MouseUp事件,因爲click事件同時包含MouseDown和MouseUp事件,並且在鼠標被釋放之前不會被觸發,所以它在我心中是最合乎邏輯的地方做那樣的事情。 –

+0

您可以_type cast_'EventArgs e'到'MouseEventArgs'。但是,它只會捕獲'mousebuttons.left',但不會'mousebuttons.right'。 – Edper

回答

2

它似乎並不與click事件工作。但是MouseDown事件會給你一個MouseEventArgs,它有一個button屬性。所以也許你可以修改你的代碼來使用MouseDown而不是點擊?

var btn = new Button(); 
btn.Text = "BUTTON"; 
btn.MouseDown += ((o, e) => { 
    MessageBox.Show(e.Button.ToString()); 
}); 
+2

由於點擊事件由MouseDown和MouseUp組成,我會更傾向於使用MouseUp事件。恕我直言 –

1

爲左右按鈕創建一個類布爾變量默認爲false。當鼠標停止事件觸發時,將變量設置爲true並檢查兩者是否都爲真。當鼠標起火時將變量設置爲false。

public bool m_right = false; 
public bool m_left = false; 

private void MainForm_MouseDown(object sender, MouseEventArgs e) 
{ 
    m_objGraphics.Clear(SystemColors.Control); 

    if (e.Button == MouseButtons.Left) 
     m_left = true; 
    if (e.Button == MouseButtons.Right) 
     m_right = true; 

    if (m_left == false || m_right == false) return; 
    //do something here 
} 

private void MainForm_MouseUp(object sender, MouseEventArgs e) 
{ 
    if (e.Button == MouseButtons.Left) 
     m_left = false; 
    if (e.Button == MouseButtons.Right) 
     m_right = false; 
} 

Source

8

button1_MouseDown事件,而不是buttons_Click Event.It只要嘗試將解決您的問題。

private void button1_MouseDown(object sender, MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Left) 
     { 
      //do something 
     } 
     if (e.Button == MouseButtons.Right) 
     { 
      //do something 
     } 
    } 
+0

thx爲你的答案朋友,最終它幫助我解決了我的問題,並且實際上是一個非常簡單的解決方案,唯一我必須意識到的是,你在窗體中討論按鈕事件而不是鼠標按鈕事件XD ...男孩這是某種混亂。無論如何,這個答案真的幫助我,它在那裏,但有時需要一個問題以外的人看到它 – user2793090

0

以下代碼對我很好用。 (請注意,我正在使用與問題中的函數類似的EventArgs類型)。

私人無效buttons_Click(對象發件人,EventArgs的){

Type t = e.GetType(); 
if (t.Equals(typeof(MouseEventArgs))) 
{ 
    MouseEventArgs mouse = (MouseEventArgs)e; 

    if (mouse.Button == MouseButtons.Left) 
    { 
     Console.WriteLine("****Left!!"); 
    } 

    if (mouse.Button == MouseButtons.Right) 
    { 
     Console.WriteLine("****Right!!"); 
    } 

} 

}

0

目的: 要處理兩個鼠標左鍵和單擊鼠標右鍵。

問題: 單擊鼠標右鍵時不會引發Click事件。只有當鼠標左鍵被按下然後釋放時纔會產生。

解決方案:

  1. 訂閱無論是點擊,和的MouseRightButtonUp事件。

注A.的的MouseRightButtonUp事件當鼠標右鍵按鈕被釋放到按鈕上提出的,不管它在按鈕上時,它最初被按下。

注B.這與這種情況無關,但您應該注意到Button對象的Click事件(按下並釋放鼠標左鍵)同時處理MouseLeftButtonDown和MouseLeftButtonUp事件,因此這兩個事件都不能在處理時處理您使用鼠標左鍵單擊Button對象。如果您確實需要分別處理這些操作,則應訂閱PreviewMouseLeftButtonDown和/或PreviewMouseLeftButtonUp。

  • 然後可以或者:

    一個。有2個獨立的事件處理程序,每個事件處理程序一個,或

    b。編寫一個可處理Click和MouseRightButtonUp事件的事件處理程序。 注意:Click事件需要一個接受類型爲RoutedEventArgs的參數的處理程序,而MouseRightButtonUp事件需要MouseButtonEventArgs類型之一。 RoutedEventArgs和MouseButtonEventArgs都從EventArgs繼承。因此,使處理程序接受EventArgs。然後處理程序應該確定它是否已經傳遞了一個RoutedEventArgs(在這種情況下,它執行左鍵單擊所需的操作)或MouseButtonEventArgs(在這種情況下,它執行右鍵單擊所需的操作)。

  • 代碼例如:

    myButton.MouseRightButtonUp += MenuItemButton_Click; 
    myButton.Click += MenuItemButton_Click; 
    
    internal void MenuItemButton_Click(object sender, EventArgs e) 
    { 
        var clickedButton = sender as Button; 
        string EventArgsType = e.GetType().ToString(); 
        //Remove leading qualification from Type ("System.Windows." or "System.Windows.Input.") 
        EventArgsType = type.Substring(EventArgsType.LastIndexOf('.') + 1); 
        switch (EventArgsType) 
        { 
         //Handle R-Click (RightMouseButtonUp) 
         case "MouseButtonEventArgs": 
          var MbeArgs = e as MouseButtonEventArgs; 
          //Do stuff 
          break; 
         //Handle L-Click (Click) 
         case "RoutedEventArgs": 
          var ReArgs = e as RoutedEventArgs; 
          //Do stuff 
          break; 
         default: 
          break; 
        } 
    }