Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How to bind a nullable bool to a checkbox?
- bool? foo = null;
- checkBox1.CheckState = foo.HasValue ?
- (foo == true ? CheckState.Checked : CheckState.Unchecked) :
- CheckState.Indeterminate;
- public static CheckState ToCheckboxState(this bool booleanValue)
- {
- return booleanValue.ToCheckboxState();
- }
- public static CheckState ToCheckboxState(this bool? booleanValue)
- {
- return booleanValue.HasValue ?
- (booleanValue == true ? CheckState.Checked : CheckState.Unchecked) :
- CheckState.Indeterminate;
- }
- if(foo.HasValue)
- {
- if(foo == true)
- checkBox1.CheckState = CheckState.Checked;
- else
- checkBox1.CheckState = CheckState.Unchecked;
- }
- else
- checkBox1.CheckState = CheckState.Indeterminate;
- checkBox1.CheckState = GetCheckState(foo);
- public CheckState GetCheckState(bool? foo)
- {
- if(foo.HasValue)
- {
- if(foo == true)
- return CheckState.Checked;
- else
- return CheckState.Unchecked;
- }
- else
- return CheckState.Indeterminate
- }
- public static void SetCheckedNull(this CheckBox c, bool? Value)
- {
- if (!c.ThreeState)
- c.Checked = Value == true;
- else
- c.CheckState = Value.HasValue ?
- (Value == true ? CheckState.Checked : CheckState.Unchecked) :
- CheckState.Indeterminate;
- }
- checkBox1.Checked = someBool;
- checkBox2.SetCheckedNull(someNullableBool);
Add Comment
Please, Sign In to add comment