Author |
Message |
Beej #1 / 14
|
 Feeling like an idiot... AGAIN Checkbox question
I could swear I have seen this option somewhere. I have a checkbox that I want to cycle between checked, unchecked and grayed (indication it will accept the values of the parent). Yet I can find no "tri-state" flag for the control. I have tried to manually set it by attaching code the click event only to have it recurse until it runs out of stack space. I have tried setting a flag to stop the recusion, whicked worked but the select statement in the routine seems to reevalute the value so I only every get unchecked and grayed. Surely there is a simple way to do this.
|
Mon, 08 Sep 2003 07:52:41 GMT |
|
 |
Bob Butle #2 / 14
|
 Feeling like an idiot... AGAIN Checkbox question
Quote: > I could swear I have seen this option somewhere. I have a checkbox > that I want to cycle between checked, unchecked and grayed (indication > it will accept the values of the parent). Yet I can find no > "tri-state" flag for the control. I have tried to manually set it by > attaching code the click event only to have it recurse until it runs > out of stack space. I have tried setting a flag to stop the recusion, > whicked worked but the select statement in the routine seems to > reevalute the value so I only every get unchecked and grayed. Surely > there is a simple way to do this.
|
Mon, 08 Sep 2003 08:06:12 GMT |
|
 |
Bob Butle #3 / 14
|
 Feeling like an idiot... AGAIN Checkbox question
Quote: > I could swear I have seen this option somewhere. I have a checkbox > that I want to cycle between checked, unchecked and grayed (indication > it will accept the values of the parent). Yet I can find no > "tri-state" flag for the control
no need -- the checkbox is always tristate: Check1.Value = vbChecked Check2.Value = vbUnchecked Check3.Value = vbGrayed
|
Mon, 08 Sep 2003 08:06:59 GMT |
|
 |
Ken Halte #4 / 14
|
 Feeling like an idiot... AGAIN Checkbox question
The following works while using the mouse... not with keys.. a bit more work's needed... '================== Option Explicit Private Sub Check1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Check1.Tag = Check1.Value End Sub Private Sub Check1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) Call subTriStateCheck(Check1) End Sub Private Sub subTriStateCheck(CBox As CheckBox) Select Case CBox.Tag Case vbChecked Debug.Print "Was vbChecked. Is vbGrayed" CBox.Value = vbGrayed Case vbUnchecked Debug.Print "Was vbUnchecked. Is vbChecked" CBox.Value = vbChecked Case Else Debug.Print "Was vbGrayed. Is vbUnchecked" CBox.Value = vbUnchecked End Select End Sub '==================
Quote: > I could swear I have seen this option somewhere. I have a checkbox > that I want to cycle between checked, unchecked and grayed (indication > it will accept the values of the parent). Yet I can find no > "tri-state" flag for the control. I have tried to manually set it by > attaching code the click event only to have it recurse until it runs > out of stack space. I have tried setting a flag to stop the recusion, > whicked worked but the select statement in the routine seems to > reevalute the value so I only every get unchecked and grayed. Surely > there is a simple way to do this.
|
Mon, 08 Sep 2003 23:50:12 GMT |
|
 |
Luc Van der Vek #5 / 14
|
 Feeling like an idiot... AGAIN Checkbox question
Quote:
>I could swear I have seen this option somewhere. I have a checkbox >that I want to cycle between checked, unchecked and grayed (indication >it will accept the values of the parent). Yet I can find no >"tri-state" flag for the control. I have tried to manually set it by >attaching code the click event only to have it recurse until it runs >out of stack space. I have tried setting a flag to stop the recusion, >whicked worked but the select statement in the routine seems to >reevalute the value so I only every get unchecked and grayed. Surely >there is a simple way to do this.
There is a pretty simple solution using such a flag, if you don't mind taking on good faith that enums always start at 0 and enum values are always incremented by 1 (enum in this case being CheckBoxConstants): Private Sub Check1_Click() Static SelfChange As Boolean Static State As CheckBoxConstants If Not SelfChange Then SelfChange = True State = (State + 1) Mod 3 Check1.Value = State SelfChange = False End If End Sub I declared State As CheckBoxConstants for readabilty only: you can make it an integer as well (but I kinda love self-commenting code ;-) I prefer using a static variable over the 'tag' property because it's faster (tag is a string value --> conversions every time) and clearer. The only drawback here is that it only works as expected if you never change the checkbox state from code and leave it unchecked (=0) in design mode, if you do differently you would want to declare State at the module level so you can keep it synchronized. -- Subtract 111 to get my real e-mail address. But please, reply to the newsgroup unless you have something private to say. Thanks!
|
Tue, 09 Sep 2003 00:28:35 GMT |
|
 |
Luc Van der Vek #6 / 14
|
 Feeling like an idiot... AGAIN Checkbox question
BTW, this works from the keyboard too. Sorry for following up to myself ;-) -- Subtract 111 to get my real e-mail address. But please, reply to the newsgroup unless you have something private to say. Thanks!
|
Tue, 09 Sep 2003 00:37:55 GMT |
|
 |
Beej #7 / 14
|
 Feeling like an idiot... AGAIN Checkbox question
On Wed, 21 Mar 2001 16:06:59 -0800, "Bob Butler"
I understand that the control will accept the three values, but I need a means to rotate the values between the three. Where as clicking on it only seems to toggle between the checked and unchecked. Quote:
>> I could swear I have seen this option somewhere. I have a checkbox >> that I want to cycle between checked, unchecked and grayed (indication >> it will accept the values of the parent). Yet I can find no >> "tri-state" flag for the control >no need -- the checkbox is always tristate: >Check1.Value = vbChecked >Check2.Value = vbUnchecked >Check3.Value = vbGrayed
|
Tue, 09 Sep 2003 00:31:34 GMT |
|
 |
Beej #8 / 14
|
 Feeling like an idiot... AGAIN Checkbox question
On Thu, 22 Mar 2001 07:50:12 -0800, "Ken Halter"
Thanks Ken. Looks like that is going to work for me. Just a little tweaking for use with the keyboard and I am set. Quote: >The following works while using the mouse... not with keys.. a bit more >work's needed... >'================== >Option Explicit >Private Sub Check1_MouseDown(Button As Integer, Shift As Integer, X As >Single, Y As Single) > Check1.Tag = Check1.Value >End Sub >Private Sub Check1_MouseUp(Button As Integer, Shift As Integer, X As Single, >Y As Single) > Call subTriStateCheck(Check1) >End Sub >Private Sub subTriStateCheck(CBox As CheckBox) > Select Case CBox.Tag > Case vbChecked > Debug.Print "Was vbChecked. Is vbGrayed" > CBox.Value = vbGrayed > Case vbUnchecked > Debug.Print "Was vbUnchecked. Is vbChecked" > CBox.Value = vbChecked > Case Else > Debug.Print "Was vbGrayed. Is vbUnchecked" > CBox.Value = vbUnchecked > End Select >End Sub >'==================
>> I could swear I have seen this option somewhere. I have a checkbox >> that I want to cycle between checked, unchecked and grayed (indication >> it will accept the values of the parent). Yet I can find no >> "tri-state" flag for the control. I have tried to manually set it by >> attaching code the click event only to have it recurse until it runs >> out of stack space. I have tried setting a flag to stop the recusion, >> whicked worked but the select statement in the routine seems to >> reevalute the value so I only every get unchecked and grayed. Surely >> there is a simple way to do this.
|
Tue, 09 Sep 2003 00:32:17 GMT |
|
 |
Bob Butle #9 / 14
|
 Feeling like an idiot... AGAIN Checkbox question
Quote: > On Wed, 21 Mar 2001 16:06:59 -0800, "Bob Butler"
> I understand that the control will accept the three values, but I need > a means to rotate the values between the three. Where as clicking on > it only seems to toggle between the checked and unchecked.
Sorry, I must have misunderstood the question. I see Ken gave you a possible solution.
|
Tue, 09 Sep 2003 00:52:34 GMT |
|
 |
Ken Halte #10 / 14
|
 Feeling like an idiot... AGAIN Checkbox question
Quote: > BTW, this works from the keyboard too. > Sorry for following up to myself ;-)
I haven't tried your solution... but I hope there's nothing wrong with following up on yourself as I do it contantly :-) Quote: > I prefer using a static variable over the 'tag' property because it's > faster (tag is a string value --> conversions every time) and clearer.
That's a good argument for something that's going to run 10 or more times per second... but we're talking clicks here.. If a user that clicks on a control can tell the difference between processing a variant/string once vs processing a static variable once, they are badly in need of an upgrade.
|
Tue, 09 Sep 2003 02:17:29 GMT |
|
 |
Luc Van der Vek #11 / 14
|
 Feeling like an idiot... AGAIN Checkbox question
On Thu, 22 Mar 2001 10:17:29 -0800, "Ken Halter" Quote:
>That's a good argument for something that's going to run 10 or more times >per second... but we're talking clicks here.. If a user that clicks on a >control can tell the difference between processing a variant/string once vs >processing a static variable once, they are badly in need of an upgrade.
True :-) It's just a habit of mine to try to consistently do things the same way everywhere, makes it easier to remember what I was doing exactly when I look at the code a year later :-) You can use the Tag property of course: Private Sub Form_Load() ' Must initialize Tag or else runtime error on first click Check1.Tag = Check1.Value End Sub Private Sub Check1_Click() Static Recursing As Boolean If Not Recursing Then Recursing = True Check1.Tag = (Check1.Tag + 1) Mod 3 Check1.Value = Check1.Tag Recursing = False End If End Sub -- Subtract 111 to get my real e-mail address. But please, reply to the newsgroup unless you have something private to say. Thanks!
|
Tue, 09 Sep 2003 16:50:37 GMT |
|
 |
í-í?? #12 / 14
|
 Feeling like an idiot... AGAIN Checkbox question
Quote: > I could swear I have seen this option somewhere. I have a checkbox > that I want to cycle between checked, unchecked and grayed (indication > it will accept the values of the parent). Yet I can find no > "tri-state" flag for the control. I have tried to manually set it by > attaching code the click event only to have it recurse until it runs > out of stack space. I have tried setting a flag to stop the recusion, > whicked worked but the select statement in the routine seems to > reevalute the value so I only every get unchecked and grayed. Surely > there is a simple way to do this.
|
Sat, 20 Sep 2003 16:30:53 GMT |
|
 |
Eric #13 / 14
|
 Feeling like an idiot... AGAIN Checkbox question
Changing the value to either -1, 0, 1, should do the trick Quote:
> > I could swear I have seen this option somewhere. I have a checkbox > > that I want to cycle between checked, unchecked and grayed (indication > > it will accept the values of the parent). Yet I can find no > > "tri-state" flag for the control. I have tried to manually set it by > > attaching code the click event only to have it recurse until it runs > > out of stack space. I have tried setting a flag to stop the recusion, > > whicked worked but the select statement in the routine seems to > > reevalute the value so I only every get unchecked and grayed. Surely > > there is a simple way to do this.
|
Sun, 21 Sep 2003 04:42:46 GMT |
|
 |
Randy Birc #14 / 14
|
 Feeling like an idiot... AGAIN Checkbox question
That's 0, 1 and 2. -- Randy Birch MVP Visual Basic Take the vb.net poll at: http://www.mvps.org/vbnet/ http://www.mvps.org/ccrp/ Please respond only to the newsgroups so all can benefit.
: Changing the value to either -1, 0, 1, should do the trick : > > I have a checkbox : > > that I want to cycle between checked, unchecked and grayed
|
Sun, 21 Sep 2003 06:10:49 GMT |
|
|