|
-
Nov 10th, 2000, 04:18 PM
#1
Thread Starter
Lively Member
How do I check if a button is clicked?
To VB or not to VB, that's the question...
-
Nov 10th, 2000, 04:22 PM
#2
The Click() event will fire when the button is clicked.
Code:
Private Sub Command1_Click()
MsgBox "Command1 was clicked"
End Sub
-
Nov 10th, 2000, 04:24 PM
#3
Frenzied Member
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
-
Nov 10th, 2000, 04:29 PM
#4
Thread Starter
Lively Member
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...
-
Nov 10th, 2000, 04:33 PM
#5
Frenzied Member
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
-
Nov 10th, 2000, 04:38 PM
#6
Thread Starter
Lively Member
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...
-
Nov 10th, 2000, 04:39 PM
#7
Frenzied Member
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
-
Nov 10th, 2000, 04:45 PM
#8
Lively Member
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.
-
Nov 10th, 2000, 04:47 PM
#9
Lively Member
You may have to add DoEvents to your loop to release the User interface.
-
Nov 10th, 2000, 04:52 PM
#10
Frenzied Member
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]
-
Nov 10th, 2000, 04:55 PM
#11
Lively Member
LOL
"Hyperactive Member"?
Wonder how you got that title!
-
Nov 10th, 2000, 04:56 PM
#12
Fanatic Member
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
-
Nov 10th, 2000, 05:03 PM
#13
Code:
Private Sub Command1_Click()
Static blnClicked As Boolean
Debug.Print "Clicked=" & blnClicked
blnClicked = Not blnClicked
End Sub
-
Nov 10th, 2000, 05:21 PM
#14
Thread Starter
Lively Member
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...
-
Nov 10th, 2000, 05:59 PM
#15
New Member
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.
-
Nov 10th, 2000, 06:05 PM
#16
Thread Starter
Lively Member
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...
-
Nov 10th, 2000, 06:54 PM
#17
Hyperactive Member
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.
-
Nov 10th, 2000, 07:10 PM
#18
Thread Starter
Lively Member
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...
-
Nov 10th, 2000, 07:21 PM
#19
Frenzied Member
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
-
Nov 10th, 2000, 09:09 PM
#20
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]
-
Jan 3rd, 2022, 01:39 AM
#21
New Member
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
-
Jan 3rd, 2022, 01:44 AM
#22
Re: If button is clicked then....
This thread is 21 years old…
-
Jan 3rd, 2022, 01:52 AM
#23
New Member
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
-
Jan 3rd, 2022, 03:39 AM
#24
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|