Results 1 to 3 of 3

Thread: How to make a command occur only once

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    8

    How to make a command occur only once

    Hey,
    I have a command which has to occur when an object becomes visible. However the command must occur only once, and no more times. so i cant put it in a timer section. Where should i put it, or how can i make the command happen when the object become visible, and then ignore the fact that the object is still visible?

  2. #2
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: How to make a command occur only once

    This should do it:

    VB Code:
    1. Dim Command_Once As Boolean
    2.  
    3. Public Sub Game_Loop()
    4.  
    5.      Do
    6.  
    7.           DoEvents
    8.  
    9.           'This is your part when you want an object to do
    10.           'something once
    11.  
    12.           If Command_Once = False And Object_Whatever.Visible = True then
    13.  
    14.                Command_Once = True
    15.  
    16.                'Do whatever that command is
    17.  
    18.           End If
    19.  
    20.          'Draw stuff here
    21.  
    22.      Loop
    23.  
    24. End Sub

  3. #3
    Junior Member noi_max's Avatar
    Join Date
    Jul 2004
    Location
    Portland OR
    Posts
    18

    Re: How to make a command occur only once

    This is almost along the lines of pseudo-code.. but perhaps it will help. The idea is to use a boolean that keeps it's value between calls. This could be a Global, or a local static.

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.    'Start with an invisible control
    4.    myControl.Visible = False
    5.    
    6. End Sub
    7.  
    8. Private Sub Command1_Click()
    9.  
    10.    'In one event, make the control visible.
    11.    myControl.Visible = True
    12.    
    13. End Sub
    14.  
    15. Private Sub Command2_Click()
    16.  
    17.    'In another event, check for visibility.
    18.    'Use a static boolean to ensure the code after the check
    19.    'runs only once.
    20.    Static Switch As Boolean
    21.    
    22.    If myControl.Visible = True And Switch = False Then
    23.       MsgBox "Control is visible"
    24.       Switch = True
    25.    End If
    26.    
    27. End Sub

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