0

我有两个 ma 形式的面板。panel1 上带有 button1 的位置让我们说 x:10,y:10 和 panel2 上带有按钮 2 的位置 x:10,y:10。

button1 的实际作用:- 它隐藏 panel1 并在同一位置显示 panel2。

但是每当我在完成其过程后单击 button1 两次时,它都会触发 button2 单击事件,

请尽快帮助我

希望下面的链接能清楚地展示我的问题

http://www.youtube.com/watch?v=bpojl4XMweo&feature=g-upl

编辑:

目前使用的代码

void hidepanel()
{
    panel1.Visible = false;
    panel2.Visible = false;
}

private void Form1_Load(object sender, EventArgs e)
{
    hidepanel();
    panel1.Visible = true;
    panel2.Location = new Point(262,19);
    panel1.Location = new Point(0, 0);
}

private void button1_Click(object sender, EventArgs e)
{
    hidepanel();
    panel2.Location = new Point(0, 0);
    panel2.Visible = true;
}

private void button2_Click(object sender, EventArgs e)
{
    MessageBox.Show("2");
}
4

1 回答 1

3

只需添加一些逻辑来隐藏/取消隐藏启用/禁用相反组件。像这样:

    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;
            }
        }
    }

请测试。

~约瑟夫

于 2012-11-20T14:30:54.947 回答