Best way to databind a group of radiobuttons in WinForms

前端 未结 9 552
无人共我
无人共我 2020-12-03 00:58

I\'m currently working on databinding some of my existing Windows Forms, and I\'ve ran into an issue figuring out the proper way of databinding a group of radiobutton contro

9条回答
  •  温柔的废话
    2020-12-03 01:57

    Set the tag name of your radio buttons to something that represents the value.

    Create a string setting, for example, OptionDuplicateFiles, and give it the default value of the tag name for your default radio button.

    To save your checked radio button:

    Settings.Default.OptionDuplicateFiles = gbxDuplicateFiles.Controls
       .OfType()
       .Where(b => b.Checked)
       .Select(b => b.Tag)
       .First()
       .ToString();
    

    To load your checked radio button:

    (gbxDuplicateFiles.Controls
       .OfType()
       .Where(b => b.Tag.ToString() == Settings.Default.OptionDuplicateFiles)
       .First())
       .Checked = true;
    

    Tada!

提交回复
热议问题