Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Toggle a Boolean?

Status
Not open for further replies.

JaeBrett

Programmer
May 5, 2003
196
0
0
CN
I have a number of components that I have visible at certain points and not visible at other points. Is there a way to toggle it, like have a procedure that does I can call to toggle them like the following.

txtBox1.Visible = !txtBox1.Visible

I know that doesn't work but you get the ideas.



Thanks all.
 
Text1.Visible = Text1.Visible + 1

it works but i dont know if it will have any side effects!!

(i use it to change boolean values with no errors)

good luck!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
The Great Date Debate Thread222-368305
File Formats Galore @ or
 
Public Function BooleanToggle(Variable as Boolean)
If Variable = true then
BooleanToggle = false
else
BooleanToggle = true
End if
End Function

Then you just enter, say, Text1.visible = BooleanToggle(Text1.visible)
 
Ag! Clever! I never thought of that!

...Still, if the variable is NONboolean, it's best to write a function.
 
But then you could not call your function because it must be passed a boolean.
Public Function BooleanToggle(Variable as Boolean)
If Variable = true then
BooleanToggle = false
else
BooleanToggle = true
End if
End Function


Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Surely you mean:

Public Function BooleanToggle(Variable as Boolean)
BooleanToggle = Not Variable
End Function

... but isn't the whole point that the value of BooleanToggle can be, well, variable... [smile]


Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
Surely you mean:

Public Function BooleanToggle(Variable as Boolean) _
As Boolean
BooleanToggle = Not Variable
End Function

Let's stay away from those evil default Variants.
I like
txtBox1.Visible = Not txtBox1.Visible
because it is one-line and in-line.

One-line boolean setting is useful for replacing If/Else or function calls in many places, as in

bOK = (Balance >= 0 and Balance <= 100)

Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 

JohnYingling

> Surely you mean:
> As Boolean

You are right, of course. [smile]

I prefer

With txtBox1
.Visible = Not .Visible
End With

because I remember reading somewhere that

txtBox1.Visible = Not txtBox1.Visible

creates two distinct references to txtBox1, whose values are then compared, whereas the With .. End With uses only one reference, which, in principle, should be quicker and consume fewer resources.

Plus I think it's easier to read, but that's me.


Andy
&quot;Logic is invincible because in order to combat logic it is necessary to use logic.&quot; -- Pierre Boutroux
&quot;Why does my program keep showing error messages every time something goes wrong?&quot;
 
I do not think that if you time the difference between
With txtBox1
.Visible = Not .Visible
End With
and
txtBox1.Visible = Not txtBox1.Visible
that you will see any difference because there are still two references to txtbox or the With object variable.

With is useful when there is an object hierarchy involved as in
colW(I).Visible = Not colW(I).Visible




Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 

>I do not think that if you time the difference between ...

Quite true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top