Results 1 to 24 of 24

Thread: If button is clicked then....

  1. #1

    Thread Starter
    Lively Member Skateboarder's Avatar
    Join Date
    Nov 2000
    Location
    Delfzijl, the Netherlands
    Posts
    66

    Question

    How do I check if a button is clicked?
    To VB or not to VB, that's the question...

  2. #2
    Guest
    The Click() event will fire when the button is clicked.
    Code:
    Private Sub Command1_Click()
        MsgBox "Command1 was clicked"
    End Sub

  3. #3
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    use the return value of the message box...it tells you which button was clicked.

    Code:
    Private Sub Command1_Click()
        Dim Response As Integer
        
        Response = MsgBox("Click a button", vbOKCancel)
        
        If Response = vbOK Then
            MsgBox "you clicked ok"
        Else
            MsgBox "you clicked cancel"
        End If
    End Sub
    ~seaweed

  4. #4

    Thread Starter
    Lively Member Skateboarder's Avatar
    Join Date
    Nov 2000
    Location
    Delfzijl, the Netherlands
    Posts
    66

    I ment:

    What i ment was something else. Here's some code:
    Code:
    Private Sub Command1_Click()
    
    Do
    ani = ani + 1
    If ani = 7 Then ani = 1
    box1.Picture = MyPic(ani)
    box2.Picture = MyPic(ani)
    box3.Picture = MyPic(ani)
    'this place right here
    Loop
    
    proceed:
    Randomize
    a1 = Int((6 * Rnd) + 1)
    a2 = Int((6 * Rnd) + 1)
    a3 = Int((6 * Rnd) + 1)
    box1.Picture = MyPic(a1)
    box2.Picture = MyPic(a2)
    box3.Picture = MyPic(a3)
    
    End Sub
    You see, I'm already in a button. And this is a button for a slot machine, so it activates the looping of the fruit pictures. But I want to use the same button to stop the looping also. So at the place I marked (see code) I want something like:
    Code:
    if command1 = clicked then goto proceed
    U see what I mean?
    To VB or not to VB, that's the question...

  5. #5
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Oops, I was distracted by the code in your footer and thought you wanted to know how you know which button is clicked in response to a message box!

    Megatron is right...the Click event for the button the user pushes will fire for a button you put on a form.

    To find out what button they pushed in response to a MsgBox, use my code. You specify what buttons will be available in the message box function (in my example the buttons will be Ok and Cancel).

    Some button constants are:
    vbOk, vbCancel, vbYes, vbNo, vbRetry, vbAbort, vbIgnore
    ~seaweed

  6. #6

    Thread Starter
    Lively Member Skateboarder's Avatar
    Join Date
    Nov 2000
    Location
    Delfzijl, the Netherlands
    Posts
    66

    Red face Damn...

    Still not what I'm looking for. Please scroll two messages
    upwards and read my reply. Then you'll see what I mean..."
    To VB or not to VB, that's the question...

  7. #7
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    I don't think you can do that. You are already in the Click event...there is no way to click the button again if the event is still running.

    You should try something else, like using a timer control and placing the code to move the pictures there.

    Then you can set the Timer1.Interval property from the command button. This is about as close to Multi-Threading as you can get with VB without causing a bunch of headaches.

    So, put the code to move the pictures in the timer control. Then in the click event of the button type this:
    Code:
    Private Sub Command1_Click()
        If Timer1.Interval = 0 Then
            Timer1.Interval = 100 ' set to whatever speed you want
        Else
            Timer1.Interval = 0 ' This stops the timer, and therefore stops the pictures
        End If
    End Sub
    ~seaweed

  8. #8
    Lively Member
    Join Date
    Nov 2000
    Location
    NC
    Posts
    66
    Private Sub Command1_Click()
    If Command1.text = "Go" ThenDo
    ani = ani + 1
    If ani = 7 Then ani = 1
    box1.Picture = MyPic(ani)
    box2.Picture = MyPic(ani)
    box3.Picture = MyPic(ani)
    'this place right here
    Loop

    proceed:
    Randomize
    a1 = Int((6 * Rnd) + 1)
    a2 = Int((6 * Rnd) + 1)
    a3 = Int((6 * Rnd) + 1)
    box1.Picture = MyPic(a1)
    box2.Picture = MyPic(a2)
    box3.Picture = MyPic(a3)
    Command1.text = "Stop"

    Else
    Stop Spinning
    Comand1.text = "Go"

    End If
    End Sub


    Or change the icons on the buttons or whatever. Better yet put the spin and the stop code in sepearate procedures then you can also add menu items or a button to call the procedures.

  9. #9
    Lively Member
    Join Date
    Nov 2000
    Location
    NC
    Posts
    66
    You may have to add DoEvents to your loop to release the User interface.

  10. #10
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    How about this...

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Dim PicNum1 As Integer
        Dim PicNum2 As Integer
        Dim PicNum3 As Integer
        
        If Timer1.Interval = 0 Then
            Command1.Caption = "Stop"
            Timer1.Interval = 100 ' set to whatever speed you want
        Else
            Command1.Caption = "Start"
            Timer1.Interval = 0
            
            ' One last move of the pictures...
            Randomize
        
            PicNum1 = Int((6 * Rnd) + 1)
            PicNum2 = Int((6 * Rnd) + 1)
            PicNum3 = Int((6 * Rnd) + 1)
            
            box1.Picture = MyPic(PicNum1)
            box2.Picture = MyPic(PicNum2)
            box3.Picture = MyPic(PicNum3)
        End If
    End Sub
    
    Private Sub Form_Load()
        Timer1.Interval = 0
        Command1.Caption = "Start"
    End Sub
    
    Private Sub Timer1_Timer()
        Static ani As Integer
        
        ' Change pictures
        ani = ani + 1
        If ani = 7 Then ani = 1
        box1.Picture = MyPic(ani)
        
        ani = ani + 1
        If ani = 7 Then ani = 1
        box2.Picture = MyPic(ani)
        
        ani = ani + 1
        If ani = 7 Then ani = 1
        box3.Picture = MyPic(ani)
    End Sub
    [Edited by seaweed on 11-10-2000 at 05:08 PM]
    ~seaweed

  11. #11
    Lively Member
    Join Date
    Nov 2000
    Location
    NC
    Posts
    66
    LOL
    "Hyperactive Member"?
    Wonder how you got that title!

  12. #12
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Ok, So, in the place you specified, you want to determine if the button had been clicked previously and if so, goto PROCEED:

    Let's see if this works ...

    Code:
    Private Sub Command1_Click()
        Do
            ani = ani + 1
            If ani = 7 Then ani = 1
            box1.Picture = MyPic(ani)
            box2.Picture = MyPic(ani)
            box3.Picture = MyPic(ani)
            If Command1.Tag = "CLICKED" Then
                Command1.Tag = ""
                Goto PROCEED
            End If
        Loop
    
    PROCEED:
        Randomize
        a1 = Int((6 * Rnd) + 1)
        a2 = Int((6 * Rnd) + 1)
        a3 = Int((6 * Rnd) + 1)
        box1.Picture = MyPic(a1)
        box2.Picture = MyPic(a2)
        box3.Picture = MyPic(a3)
        If Command1.Tag <> "CLICKED" Then
            Command1.Tag = "CLICKED"
        End If
    End Sub
    -Excalibur

  13. #13
    Guest

    Code:
    Private Sub Command1_Click()
        
        Static blnClicked As Boolean
    
        Debug.Print "Clicked=" & blnClicked
        blnClicked = Not blnClicked
    
    End Sub

  14. #14

    Thread Starter
    Lively Member Skateboarder's Avatar
    Join Date
    Nov 2000
    Location
    Delfzijl, the Netherlands
    Posts
    66

    nothing works

    Originally posted by seaweed
    [B]I don't think you can do that. You are already in the Click event...there is no way to click the button again if the event is still running.
    This is what you said. But what if I took a second button, so I have two buttons, one for starting and one for stopping. Is that possible?
    To VB or not to VB, that's the question...

  15. #15
    New Member
    Join Date
    Nov 2000
    Location
    POCONOS OF PA
    Posts
    1

    SLOTS

    Try making a second button. Hide it.
    First button click/hide and show second.

    Just swap them back and forth.

    The user thinks it is the same button.


  16. #16

    Thread Starter
    Lively Member Skateboarder's Avatar
    Join Date
    Nov 2000
    Location
    Delfzijl, the Netherlands
    Posts
    66

    Yes

    I already tried that, bu how do I make the second button
    stop de looping in the procedure of the first button and
    proceed with the rest of the procedure in the first button?
    Know what I'm sayin'?
    To VB or not to VB, that's the question...

  17. #17
    Hyperactive Member dsy5's Avatar
    Join Date
    Jul 2000
    Location
    Lockport, NY
    Posts
    362
    In your loop, place a DoEvents. In your second button click routine,
    make it set a boolean. In your loop do a Do While the boolean is true (or false, whatever).
    Then toggle the boolean before you quit your routine so you can use it again.
    Donald Sy - VB (ab)user

  18. #18

    Thread Starter
    Lively Member Skateboarder's Avatar
    Join Date
    Nov 2000
    Location
    Delfzijl, the Netherlands
    Posts
    66

    Mmm...

    I'm not quite sure what u mean, but I'll give it a try...
    To VB or not to VB, that's the question...

  19. #19
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    I think he means something like this:
    Code:
    Option Explicit
    
    ' This tells whether the images are changing or not
    Private ImagesChanging As Boolean
    
    Private Sub cmdStart_Click()
        ' Set the global variable = True
        ImagesChanging = True
        
        ' Hide the start button and show the stop button
        cmdStart.Visible = False
        cmdStop.Visible = True
        
        ' Move your images around while the global variable is true
        Do
            ' Move your images around here
            ' ....
            ' ....
            
            DoEvents  ' Don't forget the DoEvents!
        Loop While ImagesChanging
        
    End Sub
    
    Private Sub cmdStop_Click()
        ' Set the global variable = False.  This should stop images from changing
        ImagesChanging = False
        
        ' Hide the stop button and show the start button again
        cmdStart.Visible = True
        cmdStop.Visible = False
    End Sub
    ~seaweed

  20. #20
    Guest
    you can come through with one button. try somethig like this:

    Code:
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Private mbytCounter As Byte
    Private mblnLoop As Boolean
    
    Private Sub Form_Load()
    
        With Me
            .ScaleMode = vbPixels
            .AutoRedraw = True
        End With
    
        cmdRun.Caption "&Start"
    
        DoLoop False
        mbytCounter = 1
        SetCounter mbytCounter
        
    End Sub
    
    Private Sub cmdRun_Click()
        
        DoLoop (Not mblnLoop)
        
    End Sub
    
    Private Sub DoLoop(ByRef Run As Boolean)
    
        mblnLoop = Run
        
        If mblnLoop Then
            cmdRun.Caption = "&Stop"
        Else
            cmdRun.Caption = "&Start"
        End If
        
        RunLoop
        
    End Sub
    
    Private Sub RunLoop()
    
        Do While mblnLoop
            SetCounter mbytCounter
            Sleep 100
            DoEvents
        Loop
        
    End Sub
    
    Private Sub SetCounter(ByRef Counter As Byte)
    
        With Me
            .Cls
            .CurrentX = 100
            .CurrentY = 100
        End With
        
        Me.Print Counter
        
        If Counter = 20 Then
            Counter = 1
        Else
            Counter = Counter + 1
        End If
        
    End Sub
    [Edited by Sascha on 11-10-2000 at 09:13 PM]

  21. #21
    New Member vbdevelopersubi's Avatar
    Join Date
    Aug 2019
    Posts
    6

    Re: I ment:

    When you click on the button first time then change some properties of that button like Caption, then you can read that property to check whether you clicked or not e.g.,
    beginning assume that your command button caption = "Click here"

    Private Sub Command1_Click()
    if(Command1.Caption = "Click here") Then
    'do all your code here when you click first time
    Command1.Caption = "Clicked once" ' Something like this
    Elseif(Command1.Caption = "Clicked once") Then
    ' Do your all other code here when you click second time
    End if
    End Sub

  22. #22
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,743

    Re: If button is clicked then....

    This thread is 21 years old…

  23. #23
    New Member vbdevelopersubi's Avatar
    Join Date
    Aug 2019
    Posts
    6

    Re: If button is clicked then....

    You are correct but still this question may remains the same right. Suppose still may have doubts for many developers / students

  24. #24
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,743

    Re: If button is clicked then....

    The same answer was already given in post #8

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