Then the absolute easiest way would be to create a usercontrol that
will act as the keyboard. Then you just have worry about passing the
pressed key back to the "target" control.
Here's a quick sample I just wrote:
First comes the user control. For it you need to add some labels, one
for each key of the keyboard. I set the label's Textalign to
MiddleCenter and the BorderStyle to single to give it more of a key-
like look, though if this was a profession app, I would do some GDI+
work to make the label look more like a button. The reason you need to
use a label and not a button is that labels don't receive focus when
clicked.
Next, add the following code the user control. Note I changed the
inherits to Panel to prevent the usercontrol from getting focus, so
you'll have to change the inherits in the usercontrol.Designer.vb file
too.
Public Class Keyboard
Inherits Panel
' Wire up all the Label's click events to this event handler
Private Sub Label_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
OnKeyPressed(New KeyPressedEventArgs(DirectCast(sender,
Label).Text))
End Sub
Public Event KeyPressed As KeyPressedEventHandler
Protected Overridable Sub OnKeyPressed(ByVal e As
KeyPressedEventArgs)
RaiseEvent KeyPressed(Me, e)
End Sub
End Class
Public Delegate Sub KeyPressedEventHandler(ByVal sender As Object,
ByVal e As KeyPressedEventArgs)
Public Class KeyPressedEventArgs
Inherits EventArgs
Public Sub New(ByVal keyPressed As String)
Me.KeyPressed = keyPressed
End Sub
Private _KeyPressed As String
Public Property KeyPressed() As String
Get
Return _KeyPressed
End Get
Private Set(ByVal value As String)
_KeyPressed = value
End Set
End Property
End Class
That should be it for the usercontrol, now we just have to worry about
the "host" form. All you have to do is add the new keyboard
usercontrol to a form and then handle the usercontrol's new KeyPressed
event. Use something like the following:
Private Sub Keyboard1_KeyPressed(ByVal sender As System.Object,
ByVal e As KeyPressedEventArgs)
' You'll have to change this cast if you want to send text
' to a control type other than a TextBox
Dim tb As TextBox = TryCast(Me.ActiveControl, TextBox)
If Not tb Is Nothing Then
tb.Text &= e.KeyPressed
tb.SelectionStart = tb.Text.Length
End If
End Sub
Since neither the usercontrol or it's labels can get the focus, the
event handler will try to cast the active control (the control that
had focus before the user clicked the keyboard usercontrol) into a
textbox and then append the text from the KeyPressed event.
I'm guessing there is a more elegant solution to this, but at this
late at night I can't think of one
Anyways, I hope it helps you
out!
Thanks,
Seth Rowe