Sandipan
Sandipan

Reputation: 219

How to set a Checkbox checked property to true

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

Upvotes: 5

Views: 92361

Answers (7)

Xpired
Xpired

Reputation: 45

Checkbox1.Checked = true;

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

Upvotes: -1

A Reynaldos
A Reynaldos

Reputation: 141

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;
}

Upvotes: 12

Lucas Dalcolmo
Lucas Dalcolmo

Reputation: 41

chkEntregue.CheckState = CheckState.Checked;

Upvotes: 4

Imabezza
Imabezza

Reputation: 31

To set CheckBox:

 CheckBoxName.SetCurrentValue(CheckBox.IsCheckedProperty, true);

To unset CheckBox:

 CheckBoxName.SetCurrentValue(CheckBox.IsCheckedProperty, false);

Upvotes: 3

Matt Hamilton
Matt Hamilton

Reputation: 204129

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));
}

Upvotes: 2

Michael Petrotta
Michael Petrotta

Reputation: 60902

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.

Upvotes: 1

logicnp
logicnp

Reputation: 5836

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

Upvotes: 3

Related Questions