.NET - Who checked my checkbox?!

SoftwareEng

Senior member
Apr 24, 2005
553
4
81
Hi -

In .NET, I have code that checks a check box (chkBlah.Checked = true). Of course, that triggers the Checked event handler. The event handler is also triggered when the USER checks that check box.

What's the easiest way to find out who checked that box - my code or the user? If there a "originator" property to tell me why the event was fired?

C#/VB.NET

Thanks guys
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
bool softwareCheckedTheBoxByItself = false;

...

softwareCheckedTheBoxByItself = true;
chkBlah.checked = true;

eventHandler(){
if(softwareCheckedTheBoxByItself){
Console.writeln("I did this"!);
}
else{
Console.writeln("You did this!");
}
softwareCheckedTheBoxByItself = false;
}
 

SoftwareEng

Senior member
Apr 24, 2005
553
4
81
Thanks, this works. But I was hoping for some built-in magicalproperty, e.g. .ChangedByuser :)

Oh well, maybe in .NET 3.0

Originally posted by: notfred
bool softwareCheckedTheBoxByItself = false;

...

softwareCheckedTheBoxByItself = true;
chkBlah.checked = true;

eventHandler(){
if(softwareCheckedTheBoxByItself){
Console.writeln("I did this"!);
}
else{
Console.writeln("You did this!");
}
softwareCheckedTheBoxByItself = false;
}

 

WannaFly

Platinum Member
Jan 14, 2003
2,811
1
0
Originally posted by: SoftwareEng
Thanks, this works. But I was hoping for some built-in magicalproperty, e.g. .ChangedByuser :)

Oh well, maybe in .NET 3.0

Originally posted by: notfred
bool softwareCheckedTheBoxByItself = false;

...

softwareCheckedTheBoxByItself = true;
chkBlah.checked = true;

eventHandler(){
if(softwareCheckedTheBoxByItself){
Console.writeln("I did this"!);
}
else{
Console.writeln("You did this!");
}
softwareCheckedTheBoxByItself = false;
}


Not the best way to do it, since you'd need a global scoped variable for each checkbox.

I'm sure there are other better way's, but i'd recommend removing/adding the eventhandler when you do it programtically...

 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
I'm sure you could write your own or subclass CheckBox to accomplish this without too much trouble.

Originally posted by: WannaFly
Not the best way to do it, since you'd need a global scoped variable for each checkbox.
It would not have to be global, just the same scope as the check box itself, which is usually private. So long as your event handler is local anyway.
 

XZeroII

Lifer
Jun 30, 2001
12,572
0
0
Just use the Sender object that is passed in. That is what it is there for.

Edit: Sorry, I guess that wont work. Read the guy two above. He's got it.