2012-11-19 82 views
0

我在馬形成兩個面板。 panel1在button1上放置位置let x:10,y:10和panel2,其位置x:10,y:10。避免click事件,而面板刷新

究竟Button1的事: - 它隱藏PANEL1並顯示是Panel2在同一位置。

但每當我完成它的過程後兩次Button1的點擊觸發按鈕2單擊事件,

plz幫助我儘快

希望下面的鏈接將證明我的概率顯然

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

所以你是說,通過雙擊Button1的你,第一次點擊的按鈕1開火,第二個是射擊BUTTON2?如果是這樣,請在其事件處理程序中禁用button1(或者可能是整個面板)。 –

+0

我已經嘗試過了......它不工作...... :( – neerajMAX

+0

你可以展示一些你的代碼嗎?還有一個與@ josef的答案一起去。 –

回答

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 hereenter image description here

問候

約瑟夫

編輯: 現在,我看到了問題,鼠標點擊被排入窗口消息隊列,並且會點擊按鈕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; 
      } 
     } 
    } 

請測試。

〜約瑟夫

+0

感謝您的建議,但它不適用於我的情況。 ** \ n ** Button1的Click事件,如果你給hault顯示另一個面板之前,點擊按鈕1兩次,你也將獲得同樣的問題 試試這個: - System.Threading.Thread.Sleep(2000) ; panel2.visible = true; – neerajMAX

+0

???什麼是喧譁?你能提供你的代碼嗎?我無法複製您的問題與我的代碼。如果我點擊button1,會顯示panel2,現在點擊相同的位置會觸發button2點擊。 'click'在mouseup上執行。好的,當我在Button1中插入Sleep(2000)時,單擊我看到窗口已經在消息隊列中放置了一個包含當前位置的點擊,一旦button2可見/啓用,它將處理這個排隊的點擊消息。好的,我改變了我的答案。 – josef

+0

這正是我需要的....豎起大拇指@josef ... 它工作得很好...... :) – neerajMAX