我目前正在使用Form. 在彈出選單中,有一個Button. 該按鈕應ContextMenuStrip在左鍵單擊時打開,但不是右鍵單擊。這樣,我沒有在 Microsoft Visual Studio 的面板中將分配給按鈕ContextMenuStrip的ContextMenuStrip屬性。Design僅供參考,ContextMenuStrip的位置在Button.
以下是幫助您形象化的示例。
這是矩陣彈出選單的樣子:

這是打開后的
Button樣子ContextMenuStrip:
正如標題所示,我無法ContextMenuStrip在第一次單擊按鈕時打開。但是,它適用于下一次點擊。我已經嘗試了這些鏈接中的解決方案,但無濟于事:
- 解決方案 1。
- 解決方案 2。
- 解決方案 3。
以下是涉及的代碼(全部在里面MatrixPopupMenu.cs):
//global variable
private Point contextStripLocation;
//zoomFactorContextStrip -> name of the ContextMenuStrip object
//the button's click event handler
private void button15_Click(object sender, EventArgs e)
{
if((e as MouseEventArgs).Button == MouseButtons.Left)
{
zoomFactorContextStrip.Show(button15, contextStripLocation);
}
}
//matrix popup menu's load event handler
private void MatrixPopupMenu_Load(object sender, EventArgs e)
{
contextStripLocation = new Point(0, this.button15.Height);
}
請問我可以知道我能做些什么來解決這個問題嗎?
uj5u.com熱心網友回復:
當我在搞亂MouseUp和MouseDown事件處理程式時,我設法解決了我自己的問題。
簡單地說,我只需要兩件事:
- 在
MouseDown事件處理程式中,ContextMenuStrip.Close()被呼叫。 - 在
MouseUp事件處理程式中,ContextMenuStrip.Show()被呼叫。
以下是代碼:
//global variable
private Point contextStripLocation;
//zoomFactorContextStrip -> name of the ContextMenuStrip object
//the button's click event handler
//private void button15_Click(object sender, EventArgs e) -> not used anymore
//instead, replaced with:
//popup menu's mouseup event handler
private void button15_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.TopMost = true;
zoomFactorContextStrip.Show(button15, zoomFactorCSLocation);
}
}
//popup menu's mousedown event handler
private void button15_MouseDown(object sender, MouseEventArgs e)
{
zoomFactorContextStrip.Close();
}
//matrix popup menu's load event handler
private void MatrixPopupMenu_Load(object sender, EventArgs e)
{
contextStripLocation = new Point(0, this.button15.Height);
}
老實說,我真的不知道為什么會這樣,但它完全解決了我的問題。它可能與解決方案 2有某種關系。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/443399.html
上一篇:控制臺應用程式中的元檔案大小不正確,在Windows表單中正確
下一篇:如何獲取RAM名稱?
