How to click a checkbox programmatically?

  • Thread starter Thread starter Zytan
  • Start date Start date
Z

Zytan

I could call chkMyCheckBox_CheckedChanged() myself, but this is the
response to the click, not the click itself. While this is fine for a
button, it is insufficent for a check box, as the check box should
update its position. Of course, I could just set the check box to the
new value (which required inverting its current value).

Is there a better way?

(More generally, my issue is: I have a GUI for some settings, and I
want the program to set some of them programatically, at start up.
The GUI should reflect the decisions made. The end result should = as
if the user loaded the program, and went to the settings, and clicked
some options on and off.)

Zytan
 
Zytan said:
I could call chkMyCheckBox_CheckedChanged() myself, but this is the
response to the click, not the click itself. While this is fine for a
button, it is insufficent for a check box, as the check box should
update its position. Of course, I could just set the check box to the
new value (which required inverting its current value).

Is there a better way?

(More generally, my issue is: I have a GUI for some settings, and I
want the program to set some of them programatically, at start up.
The GUI should reflect the decisions made. The end result should = as
if the user loaded the program, and went to the settings, and clicked
some options on and off.)

That would be this.

chkMyCheckBox.Value = true; ' true = checked false = unchecked.

You'll most likely need a global OnStatup-flag = true as well, as setting
chkbox.Value = true is going to fire the chkbox_Changed event. Checking the
Onstart-flag = true in the Change Event allows you to bypass executing code
in the event during app start-up. Then you set the flag = false after app
start-up.
 
Mr. Arnold said:
You'll most likely need a global OnStatup-flag = true as well, as setting
chkbox.Value = true is going to fire the chkbox_Changed event. Checking
the Onstart-flag = true in the Change Event allows you to bypass executing
code in the event during app start-up. Then you set the flag = false after
app start-up.

The OP wants the checkbox change event to fire.

Michael
 
Zytan said:
I could call chkMyCheckBox_CheckedChanged() myself, but this is the
response to the click, not the click itself. While this is fine for a
button, it is insufficent for a check box, as the check box should
update its position. Of course, I could just set the check box to the
new value (which required inverting its current value).

This is a bad idea. You should never call an event directly.
 
I could call chkMyCheckBox_CheckedChanged() myself, but this is the
response to the click, not the click itself. While this is fine for a
button, it is insufficent for a check box, as the check box should
update its position. Of course, I could just set the check box to the
new value (which required inverting its current value).

Is there a better way?

(More generally, my issue is: I have a GUI for some settings, and I
want the program to set some of them programatically, at start up.
The GUI should reflect the decisions made. The end result should = as
if the user loaded the program, and went to the settings, and clicked
some options on and off.)

Zytan

Just call CheckBox1.Checked = True '(or False)

The CheckBox changed event will fire.

Chris
 
This is a bad idea. You should never call an event directly.

Yeah, I dislike doing that. Just seems ugly.

Zytan
 
Just call CheckBox1.Checked = True '(or False)
The CheckBox changed event will fire.

Actually, it fires ONLY if it changes!

So, the default state set in the GUI (in the designer code) has to be
set properly, such that when you change it, it actually changes, and
fires the event.

(Obviously, the event is not run until it is setup, which is why the
designer sets Checked before setting up the event handler,
CheckedChanged.)

Zytan
 
Michael C said:
The OP wants the checkbox change event to fire.
Then he'll know what to do on a form-load and he wants to check it on and
not execute the code in the event, by bypassing the code.
 
Zytan said:
Actually, it fires ONLY if it changes!

That is correct. Generally though you can set things up so it doesn't
matter. Eg, if the change event fires events setting other controls to a
certain state then set the other controls to the correct state by default.

Michael
 
Zytan said:
I could call chkMyCheckBox_CheckedChanged() myself, but this is the
response to the click, not the click itself. While this is fine for a
button, it is insufficent for a check box, as the check box should
update its position. Of course, I could just set the check box to the
new value (which required inverting its current value).

Is there a better way?

(More generally, my issue is: I have a GUI for some settings, and I
want the program to set some of them programatically, at start up.
The GUI should reflect the decisions made. The end result should = as
if the user loaded the program, and went to the settings, and clicked
some options on and off.)

You could do something like the following so that when you are setting up
the form you use the same methods that the check changed events use.

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
this.setCheckBox1(this.checkBox1.Checked);
}

private void setCheckBox1(bool isChecked)
{
if(isChecked)
{
// ...
}
else
{
// ...
}
}
 
You could do something like the following so that when you are setting up
the form you use the same methods that the check changed events use.

Ok, thanks for the idea.

Zytan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top