I am writing a program on a handheld in Visual Basic .NET.  Unfortunatly, this uses the .NET Compact Framework instead of just the normal .NET Framework package, but it is still VB.NET :)

I have a bunch of button on the screen, touch screen buttons that the users prefer to use over the numeric keys themselves on the handheld device.  some of the time these buttons represent words or letters and i have programmed for that.  However, most of the time the buttons just represent numbers.  I do not want to write code for the buttons to tell them where to write a "1" to, instead i just want to make the button act as though they hit the numeric key.  I thought this might have been with the SendKey function but I am not having any luck.  It may be because I do it wrong or it may be because the .NET Compact Framework will not support it.  A simple example of how to do something like this would be much apperciated!  


Who is Participating?
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
When you "click" the button it has the focus...and thus it will receive the keystroke you generate.  Probably not what you wanted...

You need a way to track the control that LAST had the focus before the button was clicked.  Then you can focus that control again before sending your keystroke.

This can be as simple as declaring a variable to reference the last focused control.  Add handlers for the GotFocus() event of all controls that should receive keystrokes.  In the GotFocus() event, simple set your global variable to whatever control got the focus (the "sender" parameter).

    Private lastControl As Control


    If Not IsNothing(lastControl) Then
        ' send keystrokes
    End If
NavicertsAuthor Commented:
I thought there might have been something easier, maybe a bit more clean that was used for touch screen buttons.  Seems kinda messy to me, to have to add in code to all the controls.  I guess i was looking for a way to hit the button without loosing focus on the current control, basically never give the button the focus.  I probally hoped for too much :)
Mike TomlinsonMiddle School Assistant TeacherCommented:
"to have to add in code to all the controls."

You don't need seperate code for each control.

Iterate over the Controls collection in the Form_Load() event (or whatever the CF equivalent is) and use the AddHandler() function to wire up all the desired controls.  You can use the TypeOf function to determine the type of each control so you can only wire up TextBoxes for instance.

I don't have CF experience but I can show you WinForms code...
NavicertsAuthor Commented:
I'll run with what you gave me so far and see how far i make it :)  I post again if i cant figure it out, thank you sir!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.