Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Odd If statement problem --> code triggered when 'Or' is placed in

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    95

    Resolved [RESOLVED] [2005] Odd If statement problem --> code triggered when 'Or' is placed in

    I have this code where i testing weather a picturbox's X value is the same as or similar (within about 3 pixels) as another picturbox's X value. The code is below:
    vb Code:
    1. Private Sub AI_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AI.Tick
    2.         Dim Left, Right, Left2, Right2, Left3, Right3, Left4, Right4 As Integer
    3.         Dim Up, Down, Up2, Down2, Up3, Down3, Up4, Down4
    4.  
    5.         Left = P1CoOrds.X
    6.         Left2 = P1CoOrds.X - 1
    7.         Left3 = P1CoOrds.X - 2
    8.         Left4 = P1CoOrds.X - 3
    9.         Right = P1CoOrds.X
    10.         Right2 = P1CoOrds.X + 1
    11.         Right3 = P1CoOrds.X + 2
    12.         Right4 = P1CoOrds.X + 3
    13.  
    14.         lbl1.Text = Pic1.Location.X
    15.         lbl2.Text = Pic1.Location.Y
    16.         lbl3.Text = AI1.Location.X
    17.         lbl4.Text = AI1.Location.Y
    18.  
    19.         If AI1.Left = Left Or Left2 Or Left3 Or Left4 Then
    20.             CreatePicBox(50, 20, AI1.Location, Color.Blue) 'This is only here to see if the code is being triggered.
    21.         End If
    22.  
    23.     End Sub
    24.  
    25.     Private Sub CreatePicBox(ByVal Height As Integer, ByVal Width As Integer, ByVal Pos As Point, ByVal Colour As Color)
    26.         PicBox = New PictureBox
    27.         PicBox.Height = Height
    28.         PicBox.Width = Width
    29.         PicBox.Location = Pos
    30.         PicBox.BackColor = Colour
    31.         Controls.Add(PicBox)
    32.         PicBoxNo += 1
    33.         lbl5.Text = PicBoxNo
    34.     End Sub

    the first block of code is in a timer running at 1 millisecond. Later on I'll probably reduce that. Anyway, with the "Left or Left2 or Left3 or Left4" it is triggering the code where ever the picturebox is. The x values are completely different and the code is still being triggered.
    It works however if i house all the "Lefts" in their own If Then statements. Same goes with all the "Rights", "Ups" and "Downs".

    Could someone provide insight into this rather annoying problem?
    thanks.
    Last edited by squrrilslayer; May 3rd, 2008 at 04:29 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Odd If statement problem --> code triggered when 'Or' is placed in

    This is incorrect syntax:
    vb.net Code:
    1. If AI1.Left = Left Or Left2 Or Left3 Or Left4 Then
    It should be:
    vb.net Code:
    1. If AI1.Left = Left OrElse AI1.Left = Left2 OrElse AI1.Left = Left3 OrElse AI1.Left = Left4 Then
    Apart from that, a Timer with an Interval of 1 millisecond is completely unacceptable. How long do you think it would take to execute the code inside the Tick event handler?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    95

    Re: [2005] Odd If statement problem --> code triggered when 'Or' is placed in

    i have no idea how long that would take to tick through a timer!

    and thanks. OrElse, not Or!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2005] Odd If statement problem --> code triggered when 'Or' is placed in

    There are 1000 milliseconds in 1 second. You're displaying text in four Lables and creating a PictureBox in that event handler. Do you expect to be able to do that 1000 times per second, plus whatever else your app has to do?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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