DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Wait for key press

  1. #1
    JeffMorris Guest

    Wait for key press


    In Quick BASIC, I can write a program like this:

    print "Stillwell Avenue"
    while inkey$="":wend
    print "86th Street"
    while inkey$="":wend
    print "Avenue U"
    while inkey$="":wend
    print "Kings Highway"

    Visual BASIC 6.0 doesn't have a function that is similar to the inkey$ function
    in Quick BASIC. In Visual BASIC, I can use a modal form or select case/end
    select loop and a variable to keep track of subway station names but if I
    make a mistake while writing the program, I waste my time rewriting the program
    and debugging the program. How can I simulate the inkey$ function or a function
    that waits for me to press a key before displaying the next station name?
    Jeffrey.

  2. #2
    Rick Rothstein Guest

    Re: Wait for key press

    You'll need to change how you approach problems in VB. Being an event driven
    language, the user of your program is in control of the program's flow
    (which your use of the inkey$ function attempts to implement) and VB makes
    that automatic. A top of the head answer to your problem (and I don't
    pretend this is anywhere near the best solution) . . .

    1) Start a new project and put a command button on the form.

    2) Double click on the button and put this code in the Click event that
    came up.

    3) Static WhichStation As Long
    WhichStation = WhichStation + 1
    Select Case WhichStation
    Case 1: Print "Stillwell Avenue"
    Case 2: Print "86th Street"
    Case 3: Print "Avenue U"
    Case 4: Print "Kings Highway"
    End Select

    and then run the program. Click on the button and watch what prints
    on the form. If you set the Default property of the button to True,
    then hitting the Return key will do the same thing as clicking on the
    button does.

    There are many other ways to approach this problem (a lot depends on what
    you actually are trying to do with these stations); and you can even make it
    respond to the press of any key like you did in your original program --
    like so

    4) Delete the button and all the code in the code window.
    Paste this into the code window instead:

    Private Sub Form_KeyPress(KeyAscii As Integer)
    Static WhichStation As Long
    WhichStation = WhichStation + 1
    Select Case WhichStation
    Case 1: Print "Stillwell Avenue"
    Case 2: Print "86th Street"
    Case 3: Print "Avenue U"
    Case 4: Print "Kings Highway"
    End Select
    End Sub

    and then run it. Press any key and watch what prints on the form.
    I think you might benefit from reading one or more of the beginner
    books on programming in VB just to see how this event-driven
    "thing" works and to become familiar with using it.

    Rick



    "JeffMorris" <jeffmorris@delphi.com> wrote in message
    news:38cafa6b@news.devx.com...
    >
    > In Quick BASIC, I can write a program like this:
    >
    > print "Stillwell Avenue"
    > while inkey$="":wend
    > print "86th Street"
    > while inkey$="":wend
    > print "Avenue U"
    > while inkey$="":wend
    > print "Kings Highway"
    >
    > Visual BASIC 6.0 doesn't have a function that is similar to the inkey$

    function
    > in Quick BASIC. In Visual BASIC, I can use a modal form or select case/end
    > select loop and a variable to keep track of subway station names but if I
    > make a mistake while writing the program, I waste my time rewriting the

    program
    > and debugging the program. How can I simulate the inkey$ function or a

    function
    > that waits for me to press a key before displaying the next station name?
    > Jeffrey.




  3. #3
    Jeffmorris Guest

    Re: Wait for key press


    "Rick Rothstein" <rick_newsgroup@email.com> wrote:
    >You'll need to change how you approach problems in VB. Being an event driven
    >language, the user of your program is in control of the program's flow
    >(which your use of the inkey$ function attempts to implement) and VB makes
    >that automatic. A top of the head answer to your problem (and I don't
    >pretend this is anywhere near the best solution) . . .
    >
    >1) Start a new project and put a command button on the form.
    >
    >2) Double click on the button and put this code in the Click event that
    > came up.
    >
    >3) Static WhichStation As Long
    > WhichStation = WhichStation + 1
    > Select Case WhichStation
    > Case 1: Print "Stillwell Avenue"
    > Case 2: Print "86th Street"
    > Case 3: Print "Avenue U"
    > Case 4: Print "Kings Highway"
    > End Select
    >
    > and then run the program. Click on the button and watch what prints
    > on the form. If you set the Default property of the button to True,
    > then hitting the Return key will do the same thing as clicking on the
    > button does.
    >
    >There are many other ways to approach this problem (a lot depends on what
    >you actually are trying to do with these stations); and you can even make

    it
    >respond to the press of any key like you did in your original program --
    >like so
    >
    >4) Delete the button and all the code in the code window.
    > Paste this into the code window instead:
    >
    > Private Sub Form_KeyPress(KeyAscii As Integer)
    > Static WhichStation As Long
    > WhichStation = WhichStation + 1
    > Select Case WhichStation
    > Case 1: Print "Stillwell Avenue"
    > Case 2: Print "86th Street"
    > Case 3: Print "Avenue U"
    > Case 4: Print "Kings Highway"
    > End Select
    > End Sub
    >
    > and then run it. Press any key and watch what prints on the form.
    > I think you might benefit from reading one or more of the beginner
    > books on programming in VB just to see how this event-driven
    > "thing" works and to become familiar with using it.
    >
    >Rick
    >
    >
    >
    >"JeffMorris" <jeffmorris@delphi.com> wrote in message
    >news:38cafa6b@news.devx.com...
    >>
    >> In Quick BASIC, I can write a program like this:
    >>
    >> print "Stillwell Avenue"
    >> while inkey$="":wend
    >> print "86th Street"
    >> while inkey$="":wend
    >> print "Avenue U"
    >> while inkey$="":wend
    >> print "Kings Highway"
    >>
    >> Visual BASIC 6.0 doesn't have a function that is similar to the inkey$

    >function
    >> in Quick BASIC. In Visual BASIC, I can use a modal form or select case/end
    >> select loop and a variable to keep track of subway station names but if

    I
    >> make a mistake while writing the program, I waste my time rewriting the

    >program
    >> and debugging the program. How can I simulate the inkey$ function or a

    >function
    >> that waits for me to press a key before displaying the next station name?
    >> Jeffrey.

    >
    >


    Rick, My program has the select case/end select loop and the variable to
    keep track of subway station names. At some stations, I want to choose alternate
    routes and choose if a train is to run express or local. Here is part of
    my code:

    Case 412
    Text1.Text = "Stop at 149th Street"
    Text2.Text = "2 - To 161st Street"
    Text3.Text = "3 - To 3rd Avenue"
    command02
    If buttonpressed = 2 Then
    cleartextboxes
    Text7.Text = "Express or Local?"
    command01
    stopindex = 412
    End If
    If buttonpressed = 3 Then
    cleartextboxes
    command01
    stopindex = 892
    End If

    Here, command01 and command02 are subroutes that enable or disable command
    buttons, cleartextboxes is a subroutine that clear text from text boxes.
    buttonpressed is a variable that tells me which command button was pressed
    and stopindex is a variable that keeps track of subway stations. The form
    has seven text boxes, nine command buttons, and two option buttons. Jeffrey.


  4. #4
    Rick Rothstein Guest

    Re: Wait for key press

    I can't tell from your post if your problem has been solved or not. It would
    appear that the code snippet for Case 412 is actually a small part of some
    *much* larger decision tree. And the question posed initially doesn't seem
    to apply to the layout concept indicated by this latest post. I may have
    missed the point of your original question.

    In my own designs, I tend (not a hard-and-fast rule though) to provide user
    interfaces that take advantage of dynamically loaded CheckBoxes,
    RadioButtons, ComboBoxes, ListBoxes, etc., allowing the user to select
    whatever options are enabled at the time, and provide movement through the
    "tree" of options via a NEXT type CommandButton that the user presses when
    ready. (Depending on the application, I might even store the decision nodes
    in the order visited by the user and provide a PREVIOUS button allowing
    him/her to move backward through the selections he/she made.) However, it is
    hard to know from your descriptions so far whether that type of approach
    would work for you on this application (although I suspect it would).

    Rick


    "Jeffmorris" <jeffmorris@delphi.com> wrote in message
    news:38cb83eb$1@news.devx.com...
    >
    > "Rick Rothstein" <rick_newsgroup@email.com> wrote:
    > >You'll need to change how you approach problems in VB. Being an event

    driven
    > >language, the user of your program is in control of the program's flow
    > >(which your use of the inkey$ function attempts to implement) and VB

    makes
    > >that automatic. A top of the head answer to your problem (and I don't
    > >pretend this is anywhere near the best solution) . . .
    > >
    > >1) Start a new project and put a command button on the form.
    > >
    > >2) Double click on the button and put this code in the Click event that
    > > came up.
    > >
    > >3) Static WhichStation As Long
    > > WhichStation = WhichStation + 1
    > > Select Case WhichStation
    > > Case 1: Print "Stillwell Avenue"
    > > Case 2: Print "86th Street"
    > > Case 3: Print "Avenue U"
    > > Case 4: Print "Kings Highway"
    > > End Select
    > >
    > > and then run the program. Click on the button and watch what prints
    > > on the form. If you set the Default property of the button to True,
    > > then hitting the Return key will do the same thing as clicking on

    the
    > > button does.
    > >
    > >There are many other ways to approach this problem (a lot depends on what
    > >you actually are trying to do with these stations); and you can even make

    > it
    > >respond to the press of any key like you did in your original program --
    > >like so
    > >
    > >4) Delete the button and all the code in the code window.
    > > Paste this into the code window instead:
    > >
    > > Private Sub Form_KeyPress(KeyAscii As Integer)
    > > Static WhichStation As Long
    > > WhichStation = WhichStation + 1
    > > Select Case WhichStation
    > > Case 1: Print "Stillwell Avenue"
    > > Case 2: Print "86th Street"
    > > Case 3: Print "Avenue U"
    > > Case 4: Print "Kings Highway"
    > > End Select
    > > End Sub
    > >
    > > and then run it. Press any key and watch what prints on the form.
    > > I think you might benefit from reading one or more of the beginner
    > > books on programming in VB just to see how this event-driven
    > > "thing" works and to become familiar with using it.
    > >
    > >Rick
    > >
    > >
    > >
    > >"JeffMorris" <jeffmorris@delphi.com> wrote in message
    > >news:38cafa6b@news.devx.com...
    > >>
    > >> In Quick BASIC, I can write a program like this:
    > >>
    > >> print "Stillwell Avenue"
    > >> while inkey$="":wend
    > >> print "86th Street"
    > >> while inkey$="":wend
    > >> print "Avenue U"
    > >> while inkey$="":wend
    > >> print "Kings Highway"
    > >>
    > >> Visual BASIC 6.0 doesn't have a function that is similar to the inkey$

    > >function
    > >> in Quick BASIC. In Visual BASIC, I can use a modal form or select

    case/end
    > >> select loop and a variable to keep track of subway station names but if

    > I
    > >> make a mistake while writing the program, I waste my time rewriting the

    > >program
    > >> and debugging the program. How can I simulate the inkey$ function or a

    > >function
    > >> that waits for me to press a key before displaying the next station

    name?
    > >> Jeffrey.

    > >
    > >

    >
    > Rick, My program has the select case/end select loop and the variable to
    > keep track of subway station names. At some stations, I want to choose

    alternate
    > routes and choose if a train is to run express or local. Here is part of
    > my code:
    >
    > Case 412
    > Text1.Text = "Stop at 149th Street"
    > Text2.Text = "2 - To 161st Street"
    > Text3.Text = "3 - To 3rd Avenue"
    > command02
    > If buttonpressed = 2 Then
    > cleartextboxes
    > Text7.Text = "Express or Local?"
    > command01
    > stopindex = 412
    > End If
    > If buttonpressed = 3 Then
    > cleartextboxes
    > command01
    > stopindex = 892
    > End If
    >
    > Here, command01 and command02 are subroutes that enable or disable command
    > buttons, cleartextboxes is a subroutine that clear text from text boxes.
    > buttonpressed is a variable that tells me which command button was pressed
    > and stopindex is a variable that keeps track of subway stations. The form
    > has seven text boxes, nine command buttons, and two option buttons.

    Jeffrey.
    >




  5. #5
    Jeffmorris yerffej Guest

    Re: Wait for key press


    Rick, I use command buttons to select alternate subway routes and option buttons
    to choose if a subway train is to run express or local. The Text7.Text line
    asks if I want the train to run express or local but I don't have to choose
    if the train is to run express or local each time the Text7.Text line appears.
    There are about 960 Case decisions in my program. Jeffrey.

    "Rick Rothstein" <rick_newsgroup@email.com> wrote:
    >I can't tell from your post if your problem has been solved or not. It would
    >appear that the code snippet for Case 412 is actually a small part of some
    >*much* larger decision tree. And the question posed initially doesn't seem
    >to apply to the layout concept indicated by this latest post. I may have
    >missed the point of your original question.
    >
    >In my own designs, I tend (not a hard-and-fast rule though) to provide user
    >interfaces that take advantage of dynamically loaded CheckBoxes,
    >RadioButtons, ComboBoxes, ListBoxes, etc., allowing the user to select
    >whatever options are enabled at the time, and provide movement through the
    >"tree" of options via a NEXT type CommandButton that the user presses when
    >ready. (Depending on the application, I might even store the decision nodes
    >in the order visited by the user and provide a PREVIOUS button allowing
    >him/her to move backward through the selections he/she made.) However, it

    is
    >hard to know from your descriptions so far whether that type of approach
    >would work for you on this application (although I suspect it would).
    >
    >Rick
    >
    >
    >"Jeffmorris" <jeffmorris@delphi.com> wrote in message
    >news:38cb83eb$1@news.devx.com...
    >>
    >> "Rick Rothstein" <rick_newsgroup@email.com> wrote:
    >> >You'll need to change how you approach problems in VB. Being an event

    >driven
    >> >language, the user of your program is in control of the program's flow
    >> >(which your use of the inkey$ function attempts to implement) and VB

    >makes
    >> >that automatic. A top of the head answer to your problem (and I don't
    >> >pretend this is anywhere near the best solution) . . .
    >> >
    >> >1) Start a new project and put a command button on the form.
    >> >
    >> >2) Double click on the button and put this code in the Click event that
    >> > came up.
    >> >
    >> >3) Static WhichStation As Long
    >> > WhichStation = WhichStation + 1
    >> > Select Case WhichStation
    >> > Case 1: Print "Stillwell Avenue"
    >> > Case 2: Print "86th Street"
    >> > Case 3: Print "Avenue U"
    >> > Case 4: Print "Kings Highway"
    >> > End Select
    >> >
    >> > and then run the program. Click on the button and watch what prints
    >> > on the form. If you set the Default property of the button to True,
    >> > then hitting the Return key will do the same thing as clicking on

    >the
    >> > button does.
    >> >
    >> >There are many other ways to approach this problem (a lot depends on

    what
    >> >you actually are trying to do with these stations); and you can even

    make
    >> it
    >> >respond to the press of any key like you did in your original program

    --
    >> >like so
    >> >
    >> >4) Delete the button and all the code in the code window.
    >> > Paste this into the code window instead:
    >> >
    >> > Private Sub Form_KeyPress(KeyAscii As Integer)
    >> > Static WhichStation As Long
    >> > WhichStation = WhichStation + 1
    >> > Select Case WhichStation
    >> > Case 1: Print "Stillwell Avenue"
    >> > Case 2: Print "86th Street"
    >> > Case 3: Print "Avenue U"
    >> > Case 4: Print "Kings Highway"
    >> > End Select
    >> > End Sub
    >> >
    >> > and then run it. Press any key and watch what prints on the form.
    >> > I think you might benefit from reading one or more of the beginner
    >> > books on programming in VB just to see how this event-driven
    >> > "thing" works and to become familiar with using it.
    >> >
    >> >Rick
    >> >
    >> >
    >> >
    >> >"JeffMorris" <jeffmorris@delphi.com> wrote in message
    >> >news:38cafa6b@news.devx.com...
    >> >>
    >> >> In Quick BASIC, I can write a program like this:
    >> >>
    >> >> print "Stillwell Avenue"
    >> >> while inkey$="":wend
    >> >> print "86th Street"
    >> >> while inkey$="":wend
    >> >> print "Avenue U"
    >> >> while inkey$="":wend
    >> >> print "Kings Highway"
    >> >>
    >> >> Visual BASIC 6.0 doesn't have a function that is similar to the inkey$
    >> >function
    >> >> in Quick BASIC. In Visual BASIC, I can use a modal form or select

    >case/end
    >> >> select loop and a variable to keep track of subway station names but

    if
    >> I
    >> >> make a mistake while writing the program, I waste my time rewriting

    the
    >> >program
    >> >> and debugging the program. How can I simulate the inkey$ function or

    a
    >> >function
    >> >> that waits for me to press a key before displaying the next station

    >name?
    >> >> Jeffrey.
    >> >
    >> >

    >>
    >> Rick, My program has the select case/end select loop and the variable

    to
    >> keep track of subway station names. At some stations, I want to choose

    >alternate
    >> routes and choose if a train is to run express or local. Here is part

    of
    >> my code:
    >>
    >> Case 412
    >> Text1.Text = "Stop at 149th Street"
    >> Text2.Text = "2 - To 161st Street"
    >> Text3.Text = "3 - To 3rd Avenue"
    >> command02
    >> If buttonpressed = 2 Then
    >> cleartextboxes
    >> Text7.Text = "Express or Local?"
    >> command01
    >> stopindex = 412
    >> End If
    >> If buttonpressed = 3 Then
    >> cleartextboxes
    >> command01
    >> stopindex = 892
    >> End If
    >>
    >> Here, command01 and command02 are subroutes that enable or disable command
    >> buttons, cleartextboxes is a subroutine that clear text from text boxes.
    >> buttonpressed is a variable that tells me which command button was pressed
    >> and stopindex is a variable that keeps track of subway stations. The form
    >> has seven text boxes, nine command buttons, and two option buttons.

    >Jeffrey.
    >>

    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center