Results 1 to 10 of 10

Thread: Simplest problem with if statement

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    16

    Simplest problem with if statement

    For some reason it wont load the first picture when i press P when i am on the right side, instead it loads the wrong picture
    Here is the code...



    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)


    If KeyCode = Asc("D") Or KeyCode = Asc("d") Then
    Image1.Picture = LoadPicture(App.Path & "/data/BRight.bmp")
    Image1.Left = Image1.Left + 40
    Text1.Text = "1"
    End If

    If KeyCode = Asc("A") Or KeyCode = Asc("A") Then
    Image1.Picture = LoadPicture(App.Path & "/data/BLeft.bmp")
    Image1.Left = Image1.Left - 40
    Text1.Text = "2"
    End If

    If KeyCode = Asc("p") Or KeyCode = Asc("p") And Text1.Text = "1" Then
    Image1.Picture = LoadPicture(App.Path & "/data/PRight.bmp")
    End If

    If KeyCode = Asc("P") Or KeyCode = Asc("p") And Text1.Text = "2" Then
    Image1.Picture = LoadPicture(App.Path & "/data/PLeft.bmp")
    End If



    End Sub

    Private Sub Form_Load()
    Text1.Text = "1"
    Image1.Picture = LoadPicture(App.Path & "/data/BRight.bmp")
    End Sub


    Private Sub Image1_Click()

    End Sub

  2. #2
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951
    Take a long look at the ANS & OR in the last two statements. I think you will see what is happening.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    16
    I am new at vb stuff can't u just tell me?

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Ascii codes are not KeyCodes. Run your program and hit the F1 key to see what I mean.

    In KeyDown events use the KeyCode constants instead of ASC anything.

    A Select Case statement might be better than several ifs.

    VB Code:
    1. Select Case KeyCode
    2.    Case vbKeyD
    3.         Image1.Picture = LoadPicture(App.Path & "/data/BRight.bmp")
    4.         Image1.Left = Image1.Left + 40
    5.         Text1.Text = "1"
    6.    Case vbKeyA
    7.          Image1.Picture = LoadPicture(App.Path & "/data/BLeft.bmp")
    8.          Image1.Left = Image1.Left - 40
    9.          Text1.Text = "2"
    10.    Case vbKeyP
    11.  
    12.        If Text1.Text = "1" Then
    13.            Image1.Picture = LoadPicture(App.Path & "/data/PRight.bmp")
    14.        Else
    15.            Image1.Picture = LoadPicture(App.Path & "/data/PLeft.bmp")
    16.        End If
    17.  
    18. End Select

  5. #5
    Hyperactive Member Jason Badon's Avatar
    Join Date
    Feb 2001
    Location
    Colorado
    Posts
    329
    It looks to me that your file path is a little wrong. You are using a "/" instead of a "\".


    VB Code:
    1. 'your code
    2. Image1.Picture = LoadPicture(App.Path & "/data/PLeft.bmp")
    3.  
    4. 'the way it should be
    5. Image1.Picture = LoadPicture(App.Path & "\data\PLeft.bmp")


    JB

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    VB, Windows (I think since ME but maybe 2000) support the forward slash as a path delimiter.

    The problem with the code is this line

    If KeyCode = Asc("p") Or KeyCode = Asc("p")

    Forget the fact that both "p" are lowercase, it is pointless to check for Asc(Any lower case letter) in the KeyDown event. Translated the above becomes

    If KeyCode = 112 Or KeyCode = 112. KeyCode 112 is for the F1 key. Again, Ascii codes are not KeyCodes.

    The code as written belongs in the KeyPress event not the KeyDown event.

  7. #7
    Hyperactive Member Blacknight's Avatar
    Join Date
    Nov 2002
    Posts
    381
    what Pasvorto means is that you put into image one the pright.bmp but since you pressed "p" and your next lines also repond to it you replace it with the pleft.bmp before you know it (you don't atually see it switch because it happens really really fast)
    VB Code:
    1. If KeyCode = Asc("p") Or KeyCode = Asc("p") And Text1.Text = "1" Then
    2. Image1.Picture = LoadPicture(App.Path & "/data/PRight.bmp")
    3. End If
    4.  
    5. If KeyCode = Asc("P") Or KeyCode = Asc("p") And Text1.Text = "2" Then
    6. Image1.Picture = LoadPicture(App.Path & "/data/PLeft.bmp")
    7. End If
    notice that both arguments (if lines) repond to the "p" ascii

  8. #8
    Hyperactive Member Blacknight's Avatar
    Join Date
    Nov 2002
    Posts
    381
    if a or a and b
    is like saying: if a
    so you should fix that

  9. #9
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951
    How bout this:

    If (KeyCode = Asc("p") Or KeyCode = Asc("p")) And Text1.Text = "1" Then
    Image1.Picture = LoadPicture(App.Path & "/data/PRight.bmp")
    End If

    If (KeyCode = Asc("P") Or KeyCode = Asc("p")) And Text1.Text = "2" Then
    Image1.Picture = LoadPicture(App.Path & "/data/PLeft.bmp")
    End If

    note the "or" is a separate thought from the 'and"

  10. #10
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    If this code were running in the KeyPress event that would be good. However, in the KeyDown event, I doubt they want the F1 key to be part of the If statement.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width