Command button caption problem
hi all
i have a command button with caption = "V".
when i click, i want to toggle between "v" and "X", so here
is what i wrote in the code:
VB Code:
Private Sub cmdFillAgain_Click()
If gFillAgain = True Then
cmdFillAgain.Caption = "X"
Else
cmdFillAgain.Caption = "V"
End If
gFillAgain = Not gFillAgain
End Sub
but in the first time i click the button, the Caption remains "x" even though
the gFillAgain variable is changing as it should.. it looks like only the first time the button is clicked doesnt work, because after that i have no problems...
any idea???
thanks
Re: Command button caption problem
Quote:
Originally Posted by stingran
hi all
i have a command button with caption = "V".
when i click, i want to toggle between "v" and "X", so here
is what i wrote in the code:
VB Code:
Private Sub cmdFillAgain_Click()
If gFillAgain = True Then
cmdFillAgain.Caption = "X"
Else
cmdFillAgain.Caption = "V"
End If
gFillAgain = Not gFillAgain
End Sub
but in the first time i click the button, the Caption remains "x" even though
the gFillAgain variable is changing as it should.. it looks like only the first time the button is clicked doesnt work, because after that i have no problems...
any idea???
thanks
I am assuming that gFillAgain is a Boolean Variable. This code worked fine for me:
VB Code:
Option Explicit
Dim gFillAgain as Boolean
Private Sub Form_Load()
gFillAgain = True
cmdFillAgain.Caption = "V"
End Sub
Private Sub cmdFillAgain_Click()
If gFillAgain Then
cmdFillAgain.Caption = "X"
Else
cmdFillAgain.Caption = "V"
End If
gFillAgain = Not gFillAgain
End Sub
Re: Command button caption problem
yes, this variable is defined as a boolean in a module:
[public gFillAgain as boolean]
and in Form Load i assign in to TRUE.
so why doesnt it work for me?
Re: Command button caption problem
In your code, just move the re-assignment from the bottom of the proc to the top:
VB Code:
Private Sub cmdFillAgain_Click()
[b]gFillAgain = Not gFillAgain[/b]
If gFillAgain = True Then
cmdFillAgain.Caption = "X"
Else
cmdFillAgain.Caption = "V"
End If
'gFillAgain = Not gFillAgain
End Sub
chem
Re: Command button caption problem
indeed!!!! it is working...
but why is it needed to assign the valua BEFORE???
is there a logic explenation?
Re: Command button caption problem
It just seemed, whatever you were doing, didnt work the first time, which meant the conditional statement wasn't to what it should be at the start. To show you, set the default caption of the button to "V". Then run it using your first lot of code.
chem
Re: Command button caption problem
You could eleminate an extra unnecessary variable and achieve the same in one line also like this:
VB Code:
Private Sub cmdFillAgain_Click()
cmdFillAgain.Caption = IIf(cmdFillAgain.Caption = "X", "V", "X")
End Sub
Re: Command button caption problem
When you assigned the value, it called the routine, which switched the value. I had that happen to me. I always set the initial value to the opposite of what I wanted in the form load.