Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. [solved] Two-way binding winforms checkbox to property?

[solved] Two-way binding winforms checkbox to property?

Scheduled Pinned Locked Moved C#
csharpwpfwinformswcfdesign
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RobertSF
    wrote on last edited by
    #1

    I have this code

    public partial class TrimOptions : Form
    {
    public bool trimFirst { get; set; } = false;
    public bool trimLast { get; set; } = false;
    public bool ifBlank { get; set; } = true;

    public TrimOptions()
    {
        InitializeComponent();
        chkTrimFirst.DataBindings.Add(new Binding("Checked", trimFirst, null, false, DataSourceUpdateMode.OnPropertyChanged));
        chkTrimLast.DataBindings.Add(new Binding("Checked", trimLast, null, false, DataSourceUpdateMode.OnPropertyChanged));
        chkIfBlank.DataBindings.Add(new Binding("Checked", ifBlank, null, false, DataSourceUpdateMode.OnPropertyChanged));
    }
    

    Whatever I set the properties to in code is reflected in the UI when the form is displayed. So far, so good. However, the user clicking the checkboxes does not set the properties, and setting the properties programmatically does not change the UI (nor the Checked property of the control). I would like programmatic changes to the properties to update the UI, and I would like the user changing the UI to update the properties. How to do that?

    L 1 Reply Last reply
    0
    • R RobertSF

      I have this code

      public partial class TrimOptions : Form
      {
      public bool trimFirst { get; set; } = false;
      public bool trimLast { get; set; } = false;
      public bool ifBlank { get; set; } = true;

      public TrimOptions()
      {
          InitializeComponent();
          chkTrimFirst.DataBindings.Add(new Binding("Checked", trimFirst, null, false, DataSourceUpdateMode.OnPropertyChanged));
          chkTrimLast.DataBindings.Add(new Binding("Checked", trimLast, null, false, DataSourceUpdateMode.OnPropertyChanged));
          chkIfBlank.DataBindings.Add(new Binding("Checked", ifBlank, null, false, DataSourceUpdateMode.OnPropertyChanged));
      }
      

      Whatever I set the properties to in code is reflected in the UI when the form is displayed. So far, so good. However, the user clicking the checkboxes does not set the properties, and setting the properties programmatically does not change the UI (nor the Checked property of the control). I would like programmatic changes to the properties to update the UI, and I would like the user changing the UI to update the properties. How to do that?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You're confusing "data sources" and "data members". [Binding Constructor (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.binding.-ctor?view=net-5.0#System\_Windows\_Forms\_Binding\_\_ctor\_System\_String\_System\_Object\_System\_String\_)

      It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

      R 1 Reply Last reply
      0
      • L Lost User

        You're confusing "data sources" and "data members". [Binding Constructor (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.binding.-ctor?view=net-5.0#System\_Windows\_Forms\_Binding\_\_ctor\_System\_String\_System\_Object\_System\_String\_)

        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

        R Offline
        R Offline
        RobertSF
        wrote on last edited by
        #3

        Thank you!

        chkTrimFirst.DataBindings.Add(new Binding("Checked", this, "trimFirst", false, DataSourceUpdateMode.OnPropertyChanged));
        chkTrimLast.DataBindings.Add(new Binding("Checked", this, "trimLast", false, DataSourceUpdateMode.OnPropertyChanged));
        chkIfBlank.DataBindings.Add(new Binding("Checked", this, "ifBlank", false, DataSourceUpdateMode.OnPropertyChanged));

        This is working just fine now.

        L 1 Reply Last reply
        0
        • R RobertSF

          Thank you!

          chkTrimFirst.DataBindings.Add(new Binding("Checked", this, "trimFirst", false, DataSourceUpdateMode.OnPropertyChanged));
          chkTrimLast.DataBindings.Add(new Binding("Checked", this, "trimLast", false, DataSourceUpdateMode.OnPropertyChanged));
          chkIfBlank.DataBindings.Add(new Binding("Checked", this, "ifBlank", false, DataSourceUpdateMode.OnPropertyChanged));

          This is working just fine now.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Excellent! I see you recognized "this" (the data source) as the form.

          It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups