Change the button text name

choudhmh
02-04-2008, 02:33 PM
I have this button where initially i've set the caption to "Ready". I need a bit of coding when i click the button once the text ready should change to "Steady" and when i click the button twice the text (caption) should change to "Go". In the go caption the button will need to execute some event i.e. 2*2 = 4. For now i just want the text to change after each click.
Any idea how i could perform this.

Thanks,

Roger_Wgnr
02-04-2008, 02:37 PM
In the click event check the buttons caption (I would use a case statement) and then based on the current caption do whatever.

choudhmh
02-04-2008, 04:25 PM
I have inserted this code:
If Command1.Caption = "Remove" Then
Command1.Caption = "Squeez"
If Command1.Caption = "Squeez" Then
Command1.Caption = "Dispense"
Text1.Text = 1
End If
End If

but after the first click it automatically goes to dispense bypassing dispense. Any idea other ways of doing this?

JPB
02-04-2008, 05:19 PM
Both If statements are being executed, so you don't see the Squeez caption as it gets set and then changed to Dispense right away.

You need to use ElseIf (or a Select Case structure).

For example:


If Command1.Caption = "Remove" Then
Command1.Caption = "Squeeze"
ElseIf Command1.Caption = "Squeeze" Then
Command1.Caption = "Dispense"
End If

the master
02-05-2008, 01:59 AM
The select case statement, as suggested by both Roger_Wgnr and JPB, would probably suit this situation a lot better.


select case Command1.Caption
case "Remove"
Command1.Caption = "Squeeze"
case "Squeez"
Command1.Caption = "Dispense"
end select

mkaras
02-05-2008, 06:53 AM
You may also need a way to reset the Command1.Caption back to the Initial "Remove" caption. --- But that may be outside the Command1_Click() routine.

choudhmh
02-05-2008, 05:12 PM
Thanks fro this, the case statement works perfectly. How would initialise after the the last button clicked.
I want the button to go back to remove after dispensed has been clicked. any ideas, i've got at the moment:
Select Case Command1.Caption
Case "Remove"
Command1.Caption = "Squeeze"
Case "Squeeze"
Command1.Caption = "Dispense"
Case "Dispense"
Command1.Caption = "Remove"

Roger_Wgnr
02-05-2008, 05:58 PM
Shoud work as you have it. Just add the End Select statement to the end.
Select Case Command1.Caption
Case "Remove"
Command1.Caption = "Squeeze"
Case "Squeeze"
Command1.Caption = "Dispense"
Case "Dispense"
Command1.Caption = "Remove"
End Select

Silo1337
02-05-2008, 10:59 PM
You can do the following as someone else suggested:

If Command1.Caption = "Remove" Then
Command1.Caption = "Squeeze"
ElseIf Command1.Caption = "Squeeze" Then
Command1.Caption = "Dispense"
End If

Or you can even do:

Private Sub Command1_Click()
If Command1.Caption = "Dispense" Then
Command1.Caption = "Remove"
GoTo end1
End If
If Command1.Caption = "Remove" Then
Command1.Caption = "Squeeze"
GoTo end1
End If
If Command1.Caption = "Squeeze" Then
Command1.Caption = "Dispense"
End If
end1:
End Sub

The coding may be a little bit ugly, but that will actually allow you to go through the three different captions over and over.

the master
02-06-2008, 01:37 AM
The coding may be a little bit ugly, but that will actually allow you to go through the three different captions over and over.

It will work but a select case statement would run faster and looks neater. If you have to come out of a sub then use "exit sub" instead of going to a label near the end of the sub (unless you need to run code at the end).

ElseIf would be a better choice because it was designed for cases like this where you only want to perform 1 action then move on but select case is still faster and usually the prefered method when you are checking the same variable for 2 or more values

Angry Kreyon
02-06-2008, 06:16 AM
another option I noticed is that you could just reverse the order of the original code you had, so that the routine checks for the variable in reverse order that would stop it from changing one to the other before you could notice it.
(IE: if you have a routine check in proper order it will make the first change and then because the second item is set to work on that change it makes it's change, if they are reversed, the routine will check for the variables in reverse stopping it's ability to run the next change because it checked for it first. in other words the original code "if a=1 then a=2, if a=2 then a=3" would change a from 1 to 3 in one check, but if it was coded as "if a=2 then a=3, if a=1 then a=2" the it will check in a reverse order that keeps it to one change in the loop.)

Hope this helps someone. :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum