VB.NET阻止按钮获得焦点

时间:2013-06-13 17:21:06

标签: .net vb.net controls

我刚开发了一个自定义控件(下图)

enter image description here

它的“迷你屏幕KeyBoard”=)现在我已经放置了一个简单的TextBox,我用sendkeys代码填充了键盘的所有按钮

    Private Sub BTN4_Click(sender As Object, e As EventArgs) Handles BTN4.Click
         SendKeys.Send("4")
    End Sub

问题是,如果我点击其中一个按钮,则不会写入文本框,这是因为按钮在发送角色之前会获得焦点!

我google了很多没有任何结果,所以我开始梳理MSDN寻找有用的东西,我发现了这个

Me.SetStyle(ControlStyles.Selectable, False)

仍然无效。你能以任何方式帮助我吗?我正在做一个应该做得很短的项目,我很生气,因为我无法解决这样一个白痴问题!

感谢您的一切

2 个答案:

答案 0 :(得分:1)

在您的Sub中,在SendKeys.Send("4")之前,插入以下行:

Me.TextBox1.Focus()
Me.TextBox1.SelectionStart = TextBox1.Text.Length

TextBox1是文本框的名称。

Source

答案 1 :(得分:1)

为什么不用事件创建键盘控件并使用WithEvents键声明它?然后在键盘控件中声明以下事件:

Public Class CustomKeyboardControl

    Inherits Control

    Public Event KeyboardButtonPressed(ByVal KeyValue As String)

    ' ...

    ' And on click event for each of your buttons :
    Private Sub BTN4_Click(sender As Object, e As EventArgs) Handles BTN4.Click
         RaiseEvent KeyboardButtonPressed("4")
    End Sub

End Class

最后,在使用此自定义键盘控件的应用程序中,只需添加一个处理程序。

Public Class MyForm
    Private WithEvents MyKeyboard As New CustomKeyboardControl()
    ' Should be declared in your Designer...
    ' Just add the "WithEvents" if that declaration...

    Private Sub HandleKeyboardInputs(ByVal KeyValue As String) Handles MyKeyboard.KeyboardButtonPressed
        MyTextBox.Text = MyTextBox.Text + KeyValue
        ' Of course, you can use SelectionStart/SelectionLength (...)
        ' to replace or insert the input at the correct place
        ' without forgetting to update the values of SelectionStart and SelectionLength...
    End Sub

    ' ...

End Class

这种方法可以让你在许多不同的情况下使用键盘控制......

希望这会有所帮助。我可以看到你实际上正在使用类似Android的布局...在这种情况下,我不认为我的想法更好......