只需添加一些逻辑来隐藏/取消隐藏启用/禁用相反组件。像这样:
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;
}
像魅力一样为我工作:
问候
约瑟夫
编辑:现在,我看到了问题,尽管您单击了禁用/隐藏的按钮 1,但鼠标单击已排入 Windows 消息队列,并将触发按钮 2 上的单击事件。
我在这里找到了解决方案:忽略排队的鼠标事件
并将代码更改为:
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;
}
}
}
请测试。
~约瑟夫