How do u make keyboard arrow keys move a picture box?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob

    How do u make keyboard arrow keys move a picture box?

    well i pretty much said it in the title.

    i was wondering how would you make a picture box or an object repond
    to the arrow keys.

    for example i have a picture and i want to move right when i press the
    right arrow on my keyboard. what is code for that?

    if anyones knows the code please answer.

    thank you very much.
  • Tom Fahey

    #2
    Re: How do u make keyboard arrow keys move a picture box?


    "Bob" <coste2001@hotm ail.com> wrote in message
    news:2c5344e.04 02181309.4bcff5 03@posting.goog le.com...[color=blue]
    > well i pretty much said it in the title.
    >
    > i was wondering how would you make a picture box or an object repond
    > to the arrow keys.
    >
    > for example i have a picture and i want to move right when i press the
    > right arrow on my keyboard. what is code for that?
    >
    > if anyones knows the code please answer.
    >
    > thank you very much.[/color]

    Try this:

    Private sub Form_KeyDown(Ke yCode as integer, Shift as integer)

    Select case KeyCode
    case vbKeyRight

    put what you want it to do here

    end select
    end sub

    I'm showing you a select case style, since it is likely you will have other
    keys doing other things and this way it cuts out a lot of if...then stuff.

    By the way, the KeyDown event and the keycode constants are available in the
    help files.


    Comment

    • Bob

      #3
      Re: How do u make keyboard arrow keys move a picture box?

      thank you but i was just wondering, how does case select work and where is it

      used. i haven't programmed in a while and i m trying to re-learn everything

      thanky you

      Comment

      • Tom Fahey

        #4
        Re: How do u make keyboard arrow keys move a picture box?


        "Bob" <coste2001@hotm ail.com> wrote in message
        news:2c5344e.04 02201523.29ee33 70@posting.goog le.com...[color=blue]
        > thank you but i was just wondering, how does case select work and where is[/color]
        it[color=blue]
        >
        > used. i haven't programmed in a while and i m trying to re-learn[/color]
        everything[color=blue]
        >
        > thanky you[/color]

        Any time you want one action (KeyDown, MouseDown,) or sub to do more than
        one thing depending on the value of a variable (MouseX, KeyCode) (or any
        other variable), there are two ways to do it.

        One is to create a list of if,then,else statements with your code inside
        each one. That's asking for slower code and more likelihood for error. The
        other is to use a select case arrangement. In that, you simply type select
        case (required argument) where the required argument is shown in the
        procedure (take a look at Form_KeyDown and notice that it is followed by
        KeyCode as one of the required arguments)

        then simply type case (what the required argument is) and follow it with the
        code you want to execute. When you are all finished with the different
        cases, type end select and you're done.

        so you could, in the KeyDown example have the following:

        if keycode=somethi ng then
        do things
        end if
        if keycode=somethi ng else then
        do other things
        end if

        or you could have
        select case keycode
        case vbkeyescape
        do something
        case vbkeya
        do something else
        end select

        See? You should be able to find out more in the Visual Basic help, or with
        the purchase of almost any book on Visual Basic.


        Comment

        • Bob

          #5
          Re: How do u make keyboard arrow keys move a picture box?

          Thnx a lot, i understand every now a lot better. i hoep teh program will work now.

          Comment

          Working...