How to set a Checkbox checked property to true
Asked Answered
T

7

5

I want to set the default property of a checkbox to true

Trevethick answered 31/5, 2010 at 4:35 Comment(0)
E
12

I suppose you only mean that on opening a form, one or more check boxes are checked .

Simply write in the Form_Load method

private void Form_Loaded (object sender, RoutedEventArgs e) {
    CheckBox1.IsChecked = true;
}
Ethiopic answered 13/1, 2012 at 10:46 Comment(0)
F
4

chkEntregue.CheckState = CheckState.Checked;

Frederickfredericka answered 28/7, 2015 at 17:5 Comment(0)
S
3

Set the Checked property to True in the Properties window of Visual Studio at design time.

Sparker answered 31/5, 2010 at 4:37 Comment(0)
T
3

To set CheckBox:

 CheckBoxName.SetCurrentValue(CheckBox.IsCheckedProperty, true);

To unset CheckBox:

 CheckBoxName.SetCurrentValue(CheckBox.IsCheckedProperty, false);
Trophozoite answered 29/5, 2015 at 14:49 Comment(0)
M
2

You haven't specified which platform, so I'll answer this for WPF, in which it's definitely possible.

You can use the OverrideMetadata method on CheckBox.IsCheckedProperty to provide a default value of "true" for all CheckBoxes. Add this code to your App class (in App.xaml.cs):

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    CheckBox.IsCheckedProperty.OverrideMetadata(typeof(CheckBox),
        new FrameworkPropertyMetadata(true));
}
Mancini answered 31/5, 2010 at 4:50 Comment(0)
K
1

For a given checkbox? Edit the form in the forms designer, and change the Checked property to true.

For all checkboxes in the environment, without changing each individually? Can't be done. Though I suppose if you got really ambitious, you could write a post-compiler or some such.

Kizzee answered 31/5, 2010 at 4:38 Comment(0)
T
-1
Checkbox1.Checked = true;

Place that in your form_load or right after you initialise components.

Tiphani answered 21/8 at 23:31 Comment(2)
How does this answer differ from the other answers already posted?Masterly
concise, more helpful for meTiphani

© 2022 - 2024 — McMap. All rights reserved.