Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

here's a pretty simple question I guess:

I added user settings to a couple of checkboxes for my WinForm following this MSDN article: http://msdn.microsoft.com/en-us/library/aa730869%28v=vs.80%29.aspx[^]

I use VS 2012 Express for Desktop.

There's an easy way to add a setting for a control by choosing "new" in the application settings properties of the control. This is what I did, and it works as expected: All checkboxes remain their last state when the form loads.

BUT: the checkboxes toggle the visibility of some labels when the CheckedChanged event occurs:

C#
private void cbExample_CheckedChanged(object sender, EventArgs e)
        {
            lblLabel.Visible = cbExample.Checked;            
        }


When the form first loads, the checkboxes are checked, but the labels remain invisible until I manually uncheck and check the cb again.

What's the proper way to make sure all checked checkboxes, radio buttons and so on do what they should right away?

Thanks for your support,
Best
Dennis
Posted

When the form loads, the event cbExample_CheckedChanged is not called (it only gets executed when checkbox is clicked), so the label remains invisible.

You need to do this in form load event.
 
Share this answer
 
v2
Comments
[no name] 2-Jan-13 5:27am    
Alright, thanks; so do I have to call every single cb*_CheckedChanged manually in the form load event?
No you don't need to call cbExample_CheckedChanged manually, that is the inbuilt event for checkbox.
You just need to write the codes which you want to execute inside the form load.

Write something like below inside the form load event, but don't call the event....

if(cbExample.Checked)
{
lblLabel.Visible = cbExample.Checked;
}
[no name] 2-Jan-13 5:47am    
Thanks! I have another weird issue which I described in one of my comments in this thread ... label visibility works fine now, but I have to click some checkboxes and radio buttons twice to make them change their checked state...
For this, please check CheckBox.DoubleClick Event and try to implement the codes inside this event.
[no name] 2-Jan-13 6:11am    
Hey Tadit, I'm not sure if you misunderstood me ... I do not want to have a double click event; but SOME of the checkboxes seem to NEED to be clicked twice (not double clicked!) to change their checked state. Same thing for some radio buttons. If I have two radio buttons, both 'checked' properties are bound to my settings and one is checked by default, I ALWAYS have to click a rb twice until its checked state changes. More weird, already on the first click the other rbs checked state changes properly.
Hi,

The check change event attach after set of initial values, which you can set from property window.

So the event is not firing.

If you know what is you initial values are then you can easily set which label's you want to visible.

Or

You can set you default value after
C#
InitializeComponent()
function. Then event will fire.

:)
 
Share this answer
 
Alright, thanks; so do I have to call every single cb*_CheckedChanged manually in the form load event?
 
Share this answer
 
Comments
[no name] 2-Jan-13 5:39am    
Oh, I ran into a new weird issue: I need to click a checkbox two times until its checked state will change! this does not affect all checkboxes, but the ones I recently added user settings to ... weird. Can anybody imagine what has happened here? Also two radio buttons are affected: when I click the non-checked one, the checked one is disabled but it needs one more click to enable the clicked one...

One of the affected checkboxes has this code in *Designer.cs:

this.cbExample.Checked = global::MyApp.Properties.Settings.Default.cbExample;
this.cbExample.CheckState = System.Windows.Forms.CheckState.Checked;
this.cbExample.DataBindings.Add(new System.Windows.Forms.Binding("Checked", global::MyApp.Properties.Settings.Default, "cbExample", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
[no name] 2-Jan-13 6:00am    
At least I found out that the following code gets executed after clicking a cb the first time:

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("True")]
public bool cbExample {
get {
return ((bool)(this["cbExample"]));
}
set {
this["cbExample"] = value;
}
}

... value is "false" although checked state is true ... checked state is reset to false after execution of the above code.

So I guess I have to modify the default handling of the config files so they will not update everything on every change but on FormClose() or by clicking a "Save" button?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900