2017-07-26 242 views
2

我有一個大按鈕和一個大按鈕上方的小按鈕。我想要的是:當用戶點擊小按鈕時,激活他的點擊事件而不是大按鈕的點擊事件。謝謝!如何點擊另一個按鈕上方的按鈕?

編輯:我有一個窗體2 System.Windows.Forms.Button。的Visual Studio 2017

一個圖像ilustrate:

enter image description here

EDIT²:本smllButton開始無形! bigButton的 MouseEnter事件:bigButton的smallButton.Visible = true 鼠標離開事件:smllButton.visible = false

+1

我認爲我們需要比您提供的更多信息。 – hatchet

+0

你有沒有試過把它帶到設計器或BringToFront()的代碼中?看到https://stackoverflow.com/questions/1351054/winform-ui-components-layer-order – hatchet

+0

我嘗試的第一件事 –

回答

1

一種方法是使用MouseDown事件上的大按鈕發送點擊的小按鈕:

private void bigButton_MouseDown(object sender, MouseEventArgs e) { 
    if (littleButton.Bounds.Contains(littleButton.Parent.PointToClient(Cursor.Position))) { 
     littleButton_Click(littleButton, EventArgs.Empty); 
     return; 
    } 

    // Any code needed for MouseDown actually on big button goes here: 
} 

注意這使它可以將鼠標從小按鈕拖動到大按鈕並單擊兩者。相反,您可以將大按鈕的點擊代碼移至此方法。

另一種方法是隻使用一個按鈕,在MouseEnter上更改其外觀,並使用鼠標X/Y確定按鈕的哪個部分被點擊。

相關問題