面板刷新时避免单击事件

时间:2012-11-19 06:41:13

标签: c# windows-mobile window

我有两个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");
}

1 个答案:

答案 0 :(得分:3)

添加一些逻辑来隐藏/取消隐藏启用/禁用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;

    }

对我来说就像魅力一样:

enter image description here enter image description here

问候

约瑟夫

编辑: 现在,我看到了问题,鼠标单击被排入Windows消息队列,并且会在按钮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;
            }
        }
    }

请测试。

〜约瑟夫