0

i have two panel in ma form. panel1 with button1 on it at location let say x:10,y:10 and panel2 with button 2 on it at location x:10,y:10.

what actually button1 do:- it hide panel1 and shows panel2 at the same location.

but whenever i click on button1 twice after completion its process it fire button2 click event,

plz help me ASAP

hope below link will demonstrate my prob clearly

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

EDIT:

Code used so far

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");
}
Sid Holland
  • 2,871
  • 3
  • 27
  • 43
neerajMAX
  • 269
  • 3
  • 4
  • 14
  • 1
    So are you saying that by double clicking your button1, the first click is firing button1 and the second is firing button2? If so then disable button1 (or perhaps the whole panel) in its event handler. – Sid Holland Nov 19 '12 at 06:55
  • i already tried it.. it is not working... :( – neerajMAX Nov 20 '12 at 06:39
  • Can you show some of your code? Have a go with @josef's answer as well. – Sid Holland Nov 20 '12 at 15:49
  • 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"); } – neerajMAX Nov 21 '12 at 04:54
  • @Sid:- i am newbee so i dnt knw how to show code like josef did.... kindly manage with above one.. :P and you can also check link mentioned in my question if required... – neerajMAX Nov 21 '12 at 05:15

1 Answers1

3

just add some logic to hide/unhide enable/disable the oposite components. Just like this:

    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;

    }

works for me like a charme:

enter image description here enter image description here

regards

Josef

EDIT: Now, I see the problem, the mouse clicks are enqueued into windows message queue and will fire the click event on button2 although you clicked on a disabled/hidden button1.

I found the solution here: Ignoring queued mouse events

and changed the code to:

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

where PeekMessage etc is defined another class:

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

Please test.

~josef

Community
  • 1
  • 1
josef
  • 5,951
  • 1
  • 13
  • 24
  • thanks for your suggestion but it is not working in my case. **\n** button1 click event if you give hault before showing another panel and click on button1 twice you will also get the same problem try this:- System.Threading.Thread.Sleep(2000); panel2.visible=true; – neerajMAX Nov 21 '12 at 05:07
  • ??? What is hault? Can you provide your code? I cannot replicate your issue with the my code. If I click button1, panel2 is shown, clicking now at same location will fire button2 click. The 'click' is executed on mouseup. OK, when I insert Sleep(2000) in Button1 click I see that windows has put a click with current posistion in the message queue and as soon as button2 is visible/enabled it will process this queued click message. OK, I changed my answer. – josef Nov 21 '12 at 09:14
  • this is exactly what i needed.... thumbs up @josef... it is working perfectly fine... :) – neerajMAX Nov 21 '12 at 11:00