Posted on 2006-05-24
Last Modified: 2008-09-22

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!  


Question by:Navicerts
    LVL 85

    Accepted Solution

    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
    LVL 7

    Author Comment

    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 :)
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    "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...
    LVL 7

    Author Comment

    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!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: HTML5 Mobile App Development with PhoneGap

    PhoneGap can help you leverage your already existing HTML5, JavaScript, and CSS skills in order to create and deploy cross-platform mobile apps.This program comprehensively covers HTML5 mobile app development from top to bottom.

    Suggested Solutions

    Title # Comments Views Activity
    Doc'in system (example?) BA 7 64
    tenRun challenge 28 67
    hasOne  challenge 59 63
    firstChar challenge 13 55
    This article is filled with multiple code samples and explanations for mathematical calculations. They are as follows: 1. General tips 2. Quadratic formula 3. Object collision 4. Projectile path General Tips       Here are some of my tips f…
    A short article about a problem I had getting the GPS LocationListener working.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now