2010-05-13 69 views
2

我需要讓我的編輯框對標籤和回車鍵都起作用。如何以編程方式在netcf中發送TAB鍵擊

我在應用程序中發現了很多問題。有什麼方法可以將製表鍵發送到窗體/編輯框。

(請注意,這必須是在緊湊型框架。)


解決方案:
這裏是我最終使用:

// This class allows us to send a tab key when the the enter key is pressed for the mooseworks mask control. 
public class MaskKeyControl : MaskedEdit 
{ 
    [DllImport("coredll.dll", EntryPoint = "keybd_event", SetLastError = true)] 
    internal static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo); 

    public const Int32 VK_TAB = 0x09; 

    protected override void OnKeyDown(KeyEventArgs e) 
    { 
     if (e.KeyData == Keys.Enter) 
     { 
      keybd_event(VK_TAB, VK_TAB, 0, 0); 
      return; 
     } 
     base.OnKeyDown(e); 
    } 

    protected override void OnKeyPress(KeyPressEventArgs e) 
    { 
     if (e.KeyChar == '\r') 
      e.Handled = true; 
     base.OnKeyPress(e); 
    } 
} 

我給答案漢斯,因爲他的代碼讓我走向正確的解決方案。

回答

2

你可以試試這個控制:

using System; 
using System.Windows.Forms; 

class MyTextBox : TextBox { 
    protected override void OnKeyDown(KeyEventArgs e) { 
     if (e.KeyData == Keys.Enter) { 
      (this.Parent as ContainerControl).SelectNextControl(this, true, true, true, true); 
      return; 
     } 
     base.OnKeyDown(e); 
    } 
    protected override void OnKeyPress(KeyPressEventArgs e) { 
     if (e.KeyChar == '\r') e.Handled = true; 
     base.OnKeyPress(e); 
    } 
} 
+0

唉不起作用。不知怎麼的標籤工作,但選擇下一個控制不。 我認爲這是因爲只有菜單被啓用,直到按下標籤並且控件失去焦點。 非常令人沮喪.... – Vaccano 2010-05-18 17:32:56

+0

不確定你的意思,但如果沒有啓用或可見,則無法選擇其他控件。 – 2010-05-18 17:42:40

+0

感謝讓我開始漢斯。看到在問題中工作的解決方案。 – Vaccano 2010-05-19 16:19:07

相關問題