只是添加一些邏輯來隱藏/取消隱藏啓用/禁用oposite組件。就像這樣:
private void button1_Click(object sender, EventArgs e)
{
addLog("Button 1 clicked");
button1.Enabled = false;
button2.Enabled = false;
panel1.Visible = false;
panel2.Visible = true;
button2.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
addLog("Button 2 clicked");
button2.Enabled = false;
panel2.Visible = false;
panel1.Visible = true;
button1.Enabled = true;
}
作品對我來說像一個風情萬種:
問候
約瑟夫
編輯: 現在,我看到了問題,鼠標點擊被排入窗口消息隊列,並且會點擊按鈕2上的點擊事件在禁用/隱藏的按鈕上1。
我發現這裏的解決方案:Ignoring queued mouse events
,並改變了代碼:
public static void ClearMouseClickQueue()
{
win32msg.NativeMessage message;
while (win32msg.PeekMessage(out message, IntPtr.Zero, (uint)win32msg.WM.WM_MOUSEFIRST, (uint)win32msg.WM.WM_MOUSELAST, 1))
{
}
}
...
private void button1_Click_1(object sender, EventArgs e)
{
addLog("Button 1 clicked");
button1.Enabled = false;
button2.Enabled = false;
panel1.Visible = false;
System.Threading.Thread.Sleep(2000);
ClearMouseClickQueue();
panel2.Visible = true;
button2.Enabled = true;
}
其中的PeekMessage等定義另一個類:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace PanelTest
{
public static class win32msg
{
[DllImport("coredll.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool PeekMessage(out NativeMessage lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax, uint wRemoveMsg);
public enum WM : uint{
/// <summary>
/// Use WM_MOUSEFIRST to specify the first mouse message. Use the PeekMessage() Function.
/// </summary>
WM_MOUSEFIRST = 0x0200,
/// <summary>
/// Use WM_MOUSELAST to specify the last mouse message. Used with PeekMessage() Function.
/// </summary>
WM_MOUSELAST = 0x020E,
}
[StructLayout(LayoutKind.Sequential)]
public struct NativeMessage
{
public IntPtr handle;
public uint msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point p;
}
}
}
請測試。
〜約瑟夫
所以你是說,通過雙擊Button1的你,第一次點擊的按鈕1開火,第二個是射擊BUTTON2?如果是這樣,請在其事件處理程序中禁用button1(或者可能是整個面板)。 –
我已經嘗試過了......它不工作...... :( – neerajMAX
你可以展示一些你的代碼嗎?還有一個與@ josef的答案一起去。 –