daily pastebin goal
96%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. How to bind a nullable bool to a checkbox?
  2. bool? foo = null;
  3. checkBox1.CheckState = foo.HasValue ?
  4.     (foo == true ? CheckState.Checked : CheckState.Unchecked) :
  5.     CheckState.Indeterminate;
  6.    
  7. public static CheckState ToCheckboxState(this bool booleanValue)
  8.     {
  9.         return booleanValue.ToCheckboxState();
  10.     }
  11.  
  12.     public static CheckState ToCheckboxState(this bool? booleanValue)
  13.     {
  14.         return booleanValue.HasValue ?
  15.                (booleanValue == true ? CheckState.Checked : CheckState.Unchecked) :
  16.                CheckState.Indeterminate;
  17.     }
  18.    
  19. if(foo.HasValue)
  20. {
  21.     if(foo == true)
  22.        checkBox1.CheckState = CheckState.Checked;
  23.     else
  24.        checkBox1.CheckState = CheckState.Unchecked;
  25. }
  26. else
  27.     checkBox1.CheckState  = CheckState.Indeterminate;
  28.    
  29. checkBox1.CheckState = GetCheckState(foo);
  30.  
  31. public CheckState GetCheckState(bool? foo)
  32. {
  33.     if(foo.HasValue)
  34.     {
  35.         if(foo == true)
  36.            return CheckState.Checked;
  37.         else
  38.            return CheckState.Unchecked;
  39.     }
  40.     else
  41.         return CheckState.Indeterminate
  42.  
  43. }
  44.    
  45. public static void SetCheckedNull(this CheckBox c, bool? Value)
  46. {
  47.     if (!c.ThreeState)
  48.         c.Checked = Value == true;
  49.     else
  50.         c.CheckState = Value.HasValue ?
  51.             (Value == true ? CheckState.Checked : CheckState.Unchecked) :
  52.             CheckState.Indeterminate;
  53. }
  54.    
  55. checkBox1.Checked = someBool;
  56.    
  57. checkBox2.SetCheckedNull(someNullableBool);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top