【问题标题】:avoid click event while panel is refreshing面板刷新时避免点击事件
【发布时间】:2012-11-19 06:41:13
【问题描述】:

我有两个 ma 形式的面板。 panel1 与 button1 位于位置让我们说 x:10,y:10 和 panel2 与 button 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");
}

【问题讨论】:

  • 你是说双击button1,第一次点击触发button1,第二次触发button2?如果是这样,则在其事件处理程序中禁用 button1(或者可能是整个面板)。
  • 我已经试过了..它不工作... :(
  • 你能展示一些你的代码吗?也试试@josef 的回答。
  • void hidepanel() { panel1.Visible = false; panel2.Visible = false; } private void Form1_Load(object sender, EventArgs e) { hidepanel(); panel1.Visible = true; panel2.Location = 新点(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"); }
  • @Sid:- 我是新手,所以我不知道如何像 josef 那样显示代码.. 请用上面的代码进行管理.. :P 你也可以检查我的问题中提到的链接,如果需要...

标签: c# windows-mobile window


【解决方案1】:

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

    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 上的单击事件。

我在这里找到了解决方案: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;
            }
        }
    }

请测试。

~约瑟夫

【讨论】:

  • 感谢您的建议,但在我的情况下不起作用。 \n button1 点击事件如果你在显示另一个面板之前给 hault 并点击 button1 两次你也会遇到同样的问题试试这个:- System.Threading.Thread.Sleep(2000); panel2.visible=true;
  • ???什么是牵引?你能提供你的代码吗?我无法用我的代码复制您的问题。如果我单击 button1,将显示 panel2,现在在同一位置单击将触发 button2 单击。 'click' 在 mouseup 上执行。好的,当我在 Button1 click 中插入 Sleep(2000) 时,我看到 windows 已经在消息队列中放置了当前位置的点击,并且只要 button2 可见/启用,它就会处理这个排队的点击消息。好的,我改变了答案。
  • 这正是我所需要的......竖起大拇指@josef......它工作得非常好...... :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-21
  • 2014-02-15
相关资源
最近更新 更多