2015-04-12 36 views
0

我正在通過VB.net在Windows窗體上工作。該表格包含兩個文本框和一個我自己編寫的屏幕鍵盤。確定winform上的哪個文本框中有光標

如何確定光標當前處於哪個文本框中,以便屏幕鍵盤將輸入正確的文本框?

該應用程序旨在完全基於觸摸。

+0

我很抱歉,我不能籤,因爲我不是在電腦前,但我相信你正在尋找如果Textbox1.Focused = true,那麼 – Malcor

+0

這已經在這裏回答:http://stackoverflow.com/questions/5629171/how-to-check-focused-textbox-in-vb-net-winforms – TheUknown

+0

不在VB.NET中,但這個答案會給你一個領先的達到你的解決方案http://stackoverflow.com/questions/4428100/find-out-the-control-with-last-focus – Steve

回答

0

我想猜你的屏幕鍵盤是一系列代表鍵盤的按鈕。

由於您有兩個文本框,您必須爲這兩個文本框都指定一個標誌,以表明它們具有焦點,而當其中一個具有焦點時,另一個不具有焦點。

Private Sub TextBox1_GotFocus(sender as Object, e as EventArgs) _ 
    Handles TextBox1.GotFocus 

// These flags are class level variables 
textbox1HasFocus = true; 
textbox2HasFocus = false; 

End Sub 

Private Sub TextBox2_GotFocus(sender as Object, e as EventArgs) _ 
    Handles TextBox2.GotFocus 

// These flags are class level variables 
textbox2HasFocus = true; 
textbox1HasFocus = false; 

End Sub 

爲了您的鍵盤事件

Private Sub KeyBoard_Click(sender As Object, e As EventArgs) Handles ButtonA.Click, ButtonB.Click, ButtonC.Click, etc... 
    ' I don't know how your using your keyboard, but buttonClickLetter could be determined in multiple ways. I would use either the Name of the control or even the Tag property for retrieving which letter to use 
    String buttonClickLetter = DirectCast(sender, Button).Tag.ToString() 
    If textbox1HasFocus Then 
     TextBox1.Text += buttonClickLetter 
    ElseIf textbox2HadFocus 
     TextBox2.Text += buttonClickLetter 
    End If 
End Sub