Results 1 to 13 of 13

Thread: [RESOLVED] this isn't triggering

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Resolved [RESOLVED] this isn't triggering

    this isn't triggering when it should.


    vb Code:
    1. Public Const BatchStartPosition As Double = 11242.11
    2. picBatch(Index).Left = 11242.11
    3.  
    4.  
    5. If BatchStartPosition = picBatch(i).Left Then
    6.         MsgBox ("Batch is in start position.")
    7.        
    8.        
    9.     End If

  2. #2
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: this isn't triggering

    Quote Originally Posted by TheUsed
    this isn't triggering when it should.


    vb Code:
    1. Public Const BatchStartPosition As Double = 11242.11
    2. picBatch(Index).Left = 11242.11
    3.  
    4.  
    5. If BatchStartPosition = picBatch(i).Left Then
    6.         MsgBox ("Batch is in start position.")
    7.        
    8.        
    9.     End If
    The problem is the decimals: the Left property is expressed in twips which is an Integer (actually a Long).

    Try 11242 and it'll work.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: this isn't triggering

    Assuming that picBatch is a control, it actually shouldn't trigger - as that is not a valid value for .Left (so it gets rounded to the nearest valid position).

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: this isn't triggering

    have you put a break point to see what is happening?
    what sub is this called in?

    i believe that positions of controls are set to the nearest appropriate value
    Code:
    Picture1.Left=11242.11
    ?Picture1.Left
     11242
    For a form, the Left and Top properties are always expressed intwips; for a control, they are measured in units depending on the coordinate system of its container. The values for these properties change as the object is moved by the user or by code. For the CommonDialog and Timer controls, these properties aren't available atrun time.

    For either property, you can specify a single-precision number.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Re: this isn't triggering

    okay here is a list of everything I tried before I posted.

    I created a picturebox on my form and set it where I wanted my button "add batch" to create a batch. I took the left property of it and inserted it in my code. I thought it was weird I got a .11 at the end.

    After several tries I have set debug.prints to print the left position of the batch. it shows it as 11242.11

    I have changed BatchStartPosition to a double, single, long, integer.

    Never triggers. That is unless I set this... to <

    vb Code:
    1. If BatchStartPosition = picBatch(i).Left Then
    2.         MsgBox ("Batch is in start position.")
    3.        
    4.        
    5.     End If

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Re: this isn't triggering

    here is the code of the entire project

    vb Code:
    1. Option Explicit
    2.  
    3.  
    4.  
    5. Private Sub cmdAddBatch_Click()
    6. intControlIndex = UBound(picBatch) + 1
    7. ReDim Preserve picBatch(intControlIndex)
    8.  
    9. Set picBatch(intControlIndex) = Controls.Add("VB.PictureBox", "PicBatch_" & CStr(intControlIndex))
    10.  
    11.  
    12. Call ADD_BATCH(intControlIndex)
    13.  
    14.  
    15. End Sub
    16.  
    17. Private Sub cmdStart_Click()
    18. If UBound(picBatch) = 0 Then
    19.     MsgBox "You need to add a batch.", vbExclamation, "Batch Required"
    20.     Exit Sub
    21. Else
    22.     Call Initialize_Movement
    23.    
    24. End If
    25. 'Timer1.Enabled = True
    26.  
    27. End Sub
    28.  
    29. Private Sub cmdStop_Click()
    30.  
    31. Timer1.Enabled = False
    32.  
    33. End Sub
    34.  
    35. Private Sub Form_Load()
    36.  
    37. intControlIndex = 0
    38. ReDim Preserve picBatch(intControlIndex)
    39.  
    40. End Sub
    41.  
    42. Private Sub Timer1_Timer()
    43. Call Collide
    44. End Sub
    45.  
    46.  
    47. MODULE CODE:
    48.  
    49.  
    50. Option Explicit
    51.  
    52. Public i As Integer
    53. Public picBatch() As PictureBox
    54.  
    55. Public intControlIndex As Integer
    56. Public Const BatchStartPosition As Double = 11242.11
    57.  
    58.  
    59.  
    60. Public Sub ADD_BATCH(Index As Integer)
    61.  
    62.  
    63. picBatch(Index).Height = 840
    64. picBatch(Index).Width = 630
    65. picBatch(Index).Top = 900
    66. picBatch(Index).Left = 11242.11
    67. picBatch(Index).Picture = LoadPicture(App.Path & "\batch.bmp")
    68. picBatch(Index).Visible = True
    69.     Debug.Print picBatch(Index).Left
    70. End Sub
    71.  
    72. Public Sub Initialize_Movement()
    73. Debug.Print picBatch(1).Left
    74. For i = UBound(picBatch) To LBound(picBatch) + 1 Step -1
    75.     If BatchStartPosition = picBatch(i).Left Then
    76.         MsgBox ("Batch is in start position.")
    77.        
    78.        
    79.     End If
    80.    
    81.  
    82. Next i
    83.  
    84. End Sub
    85.  
    86.  
    87. Public Sub Collide()
    88. Dim Index As Integer
    89.  
    90. If frmLaser.Timer1.Enabled = False Then
    91.    
    92.     Exit Sub
    93.  
    94. End If
    95.  
    96. For Index = UBound(picBatch) To LBound(picBatch) Step -1 'as Variant  in picbatch
    97.    
    98.     If UBound(picBatch) <= 1 Then _
    99.         Exit Sub
    100.  
    101.         For i = 1 To intControlIndex Step 1
    102.             If i = Index Then GoTo MOVE_ON
    103.                    
    104.                     If picBatch(Index).Left = picBatch(i).Left Then
    105.                    
    106.                         MsgBox ("We have " & picBatch(Index).Name & " collided with " & picBatch(i).Name)
    107.            
    108.                         Index = Index - 1
    109.                        
    110.                     End If
    111. MOVE_ON: 'USED TO MOVE TO THE NEXT I IF i= Index
    112.         Next i
    113.  
    114. Next Index
    115.  
    116. End Sub

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: this isn't triggering

    What are the actual values of BatchStartPosition and picBatch(i).Left when you reach the If statement?


    Note that there are very few times that GoTo is a good idea (many people say never), and how you used it is not apt.. instead of having this:
    Code:
       If i = Index Then GoTo MOVE_ON
    ...
    MOVE_ON:
    it would make more sense (and be more readable) to have this:
    Code:
       If i <> Index Then 
    ...
       End if

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Re: this isn't triggering

    the actual values are the exact same. picbatch(i).left DOES EQUAL BatchStartPosition

    that is 11242.11

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: this isn't triggering

    from the information in all the replys here, why do you not change your constant to an integer (whole number, no decimal places) then your code should work
    if you can not do that then round your values when you test like
    if picture1.left = round(batchstartposition, 0) then
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Re: this isn't triggering

    I did take it to a whole number as well, made it 11242 sorry I didn't say I did..

    I have no idea why it's not triggering the IF THEN event. the debug line checks for it then just goes right to end sub.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Re: this isn't triggering

    There is the image of debugging and getting the values then the yellow highlighted line is showing the next step from where the break point is set.
    Attached Images Attached Images  

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Location
    Toledo, OH
    Posts
    785

    Re: this isn't triggering

    I resolved it by rounding it 2 numbers past the decimal. I only knew to do that because I finally got a mismatched value


    however, why wouldn't it trigger while getting the same values..

    this is the updated code.

    vb Code:
    1. Public Sub Initialize_Movement()
    2. Debug.Print picBatch(1).Left
    3. For i = intControlIndex To 1 Step -1
    4. Debug.Print "CDbl(frmLaser.Line9.X1): " & CDbl(frmLaser.Line9.X1) & vbCrLf & "CDbl(BatchStartPosition): " & CDbl(BatchStartPosition)
    5.     If CDbl(BatchStartPosition) = Round(CDbl(frmLaser.Line9.X1), 2) Then
    6.         MsgBox ("Batch is in start position.")
    7.        
    8.        
    9.     End If
    10.    
    11.  
    12. Next i

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] this isn't triggering

    You've found a common issue with Double and Single.. they aren't particularly accurate (and like with Date values are not actually displayed the same way they are stored), so you need a certain level of rounding to match numbers that seem to be identical. Note that it needs to be done for both sides of the comparison, so your check should be like this:
    Code:
        If Round(BatchStartPosition,2) = Round(CDbl(frmLaser.Line9.X1), 2) Then
    (there is no need for CDbl there, as the variable is a Double already)

    Note that this issue would be less likely to happen if you had declared BatchStartPosition with the same data type as the things you are storing it to and comparing it to (so Single), but you should still use rounding to be safe.

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