La meilleure façon de databind un groupe de radiobuttons dans WinForms

je travaille actuellement sur la base de données de certains de mes formulaires Windows existants, et j'ai rencontré un problème en trouvant la bonne façon de la base de données d'un groupe de commandes radiobutton dans une boîte de groupe.

mon objet d'affaires a une propriété entière que je veux databind par rapport à 4 radiobuttons (où chacun d'eux représente les valeurs 0 - 3).

je suis actuellement lié à un objet presenter qui fonctionne comme le liant entre la forme et l'objet business, et la façon dont je l'ai fait, c'est maintenant à 4 ont des propriétés distinctes dont chacune se lie à l'encontre de chacune de ces valeurs (je ne l'utilisation de INotifyPropertyChanged, mais ne comprenant pas qu'ici):

Private int _propValue;

Public bool PropIsValue0 
{ 
  get { return _propValue == 0; }
  set
  {
    if (value) 
      _propValue = 0;
  }
}

Public bool PropIsValue1 { // As above, but with value == 1 }
Public bool PropIsValue2 { // As above, but with value == 2 }
Public bool PropIsValue3 { // As above, but with value == 3 }

et je lie ensuite chacun des radiobuttons à leur propriété respective comme ci-dessus.

cela ne me semble pas juste, donc tout conseil est très apprécié.

23
demandé sur Geir-Tore Lindsve 2009-03-23 23:37:25

9 réponses

RadioPanel). Il suffit d'y ajouter des RadioButtons, de définir leurs tags à différents entiers et de se lier à la propriété 'Selected'.

public class RadioGroupBox : GroupBox
{
    public event EventHandler SelectedChanged = delegate { };

    int _selected;
    public int Selected
    {
        get
        {
            return _selected;
        }
        set
        {
            int val = 0;
            var radioButton = this.Controls.OfType<RadioButton>()
                .FirstOrDefault(radio =>
                    radio.Tag != null 
                   && int.TryParse(radio.Tag.ToString(), out val) && val == value);

            if (radioButton != null)
            {
                radioButton.Checked = true;
                _selected = val;
            }
        }
    }

    protected override void OnControlAdded(ControlEventArgs e)
    {
        base.OnControlAdded(e);

        var radioButton = e.Control as RadioButton;
        if (radioButton != null)
            radioButton.CheckedChanged += radioButton_CheckedChanged;
    }

    void radioButton_CheckedChanged(object sender, EventArgs e)
    {
        var radio = (RadioButton)sender;
        int val = 0;
        if (radio.Checked && radio.Tag != null 
             && int.TryParse(radio.Tag.ToString(), out val))
        {
            _selected = val;
            SelectedChanged(this, new EventArgs());
        }
    }
}

notez que vous ne pouvez pas vous lier à la propriété 'Selected' via le concepteur en raison de problèmes d'ordre d'initialisation dans InitializeComponent (la liaison est effectuée avant que les boutons radio soient initialisés, donc leur balise est nulle dans la première tâche). Donc, juste vous lier de la sorte:

    public Form1()
    {
        InitializeComponent();
        //Assuming selected1 and selected2 are defined as integer application settings
        radioGroup1.DataBindings.Add("Selected", Properties.Settings.Default, "selected1");
        radioGroup2.DataBindings.Add("Selected", Properties.Settings.Default, "selected2");
    }
21
répondu Ohad Schneider 2010-03-06 23:10:01

je pense que je voudrais utiliser mon propre Zone. Je lierais la CustomGroupBox à votre modèle et définirais le RadioButton correct (en utilisant les propriétés de l'étiquette ou du nom) à partir de la valeur liée.

2
répondu ArielBH 2009-03-24 16:51:35

je sais que ce post est vieux, mais dans ma recherche d'une réponse pour ce même problème, je suis tombé sur ce poste et il n'a pas résolu mon problème. J'ai fini par avoir une ampoule qui a explosé au hasard il y a une minute et je voulais partager ma solution.

d'un objet de classe personnalisé comme source de données.

Objet De Classe:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BAL
{
class ProductItem
{

    // Global Variable to store the value of which radio button should be checked
    private int glbTaxStatus;
    // Public variable to set initial value passed from 
    // database query and get value to save to database
    public int TaxStatus
    {
        get { return glbTaxStatus; }
        set { glbTaxStatus = value; }
    }

    // Get/Set for 1st Radio button
    public bool Resale
    {
        // If the Global Variable = 1 return true, else return false
        get
        {
            if (glbTaxStatus.Equals(1))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        // If the value being passed in = 1 set the Global Variable = 1, else do nothing
        set
        {
            if (value.Equals(true))
            {
                glbTaxStatus = 1;
            }
        }
    }

    // Get/Set for 2nd Radio button
    public bool NeverTax
    {
        // If the Global Variable = 2 return true, else return false
        get
        {
            if (glbTaxStatus.Equals(2))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        // If the value being passed in = 2 set the Global Variable = 2, else do nothing
        set
        {
            if (value.Equals(true))
            {
                glbTaxStatus = 2;
            }
        }
    }

    // Get/Set for 3rd Radio button
    public bool AlwaysTax
    {
        // If the Global Variable = 3 return true, else return false
        get
        {
            if (glbTaxStatus.Equals(3))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        // If the value being passed in = 3 set the Global Variable = 3, else do nothing
        set
        {
            if (value.Equals(true))
            {
                glbTaxStatus = 3;
            }
        }
    }

// More code ...

trois variables publiques séparées avec get/set accédant à la même une globale variable.

dans le code derrière, j'ai une fonction appelée pendant le Page_Load() qui définit toutes les bases de données des contrôles. Pour chaque bouton radio j'ajoute sa propre banque de données.

radResale.DataBindings.Add("Checked", glbProductList, "Resale", true, DataSourceUpdateMode.OnPropertyChanged, false);
radNeverTax.DataBindings.Add("Checked", glbProductList, "NeverTax", true, DataSourceUpdateMode.OnPropertyChanged, false);
radAlwaysTax.DataBindings.Add("Checked", glbProductList, "Always", true, DataSourceUpdateMode.OnPropertyChanged, false);

j'espère que cela aide quelqu'un!!

2
répondu Paul - SGS PfINDE 2013-02-15 22:41:29

c'est mon approche pour lier une liste de boutons radio à un enum.

en utilisant L'Enum comme une chaîne dans la propriété Tag du bouton, j'utilise le reliure.Format et reliure.Analyser événement à décider sur quel bouton doit être vérifié.

public enum OptionEnum
{
   Option1 = 0,
   Option2
}

OptionEnum _rbEnum = OptionEnum.Option1;
OptionEnum PropertyRBEnum
{
    get { return _rbEnum; }
    set
    {
        _rbEnum = value;
        RaisePropertyChanged("PropertyRBEnum");
    }
}

public static void FormatSelectedEnum<T>(object sender, ConvertEventArgs args) where T : struct
{
    Binding binding = (sender as Binding);
    if (binding == null) return;

    Control button = binding.Control;

    if (button == null || args.DesiredType != typeof(Boolean)) return;

    T value = (T)args.Value;
    T controlValue;

    if (Enum.TryParse(button.Tag.ToString(), out controlValue))
    {
        args.Value = value.Equals(controlValue);
    }
    else
    {
        Exception ex = new Exception("String not found in Enum");
        ex.Data.Add("Tag", button.Tag);

        throw ex;
    }
}

public static void ParseSelectedEnum<T>(object sender, ConvertEventArgs args) where T : struct
{
    Binding binding = (sender as Binding);
    if (binding == null) return;

    Control button = binding.Control;
    bool value = (bool)args.Value;

    if (button == null || value != true) return;

    T controlValue;

    if (Enum.TryParse(button.Tag.ToString(), out controlValue))
    {
        args.Value = controlValue;
    }
    else
    {
        Exception ex = new Exception("String not found in Enum");
        ex.Data.Add("Tag", button.Tag);

        throw ex;
    }
}

alors configurez votre data binding comme ceci:

radioButton1.Tag = "Option1";
radioButton2.Tag = "Option2";

foreach (RadioButtonUx rb in new RadioButtonUx[] { radioButton1, radioButton2 })
{
    Binding b = new Binding("Checked", this, "PropertyRBEnum");
    b.Format += FormatSelectedRadioButton<OptionEnum>;
    b.Parse += ParseSelectedRadioButton<OptionEnum>;

    rb.DataBindings.Add(b);
}
1
répondu Michelangelo Partipilo 2015-01-05 13:24:26

j'ai commencé à résoudre le même problème.

j'ai utilisé une classe de reliure qui encapsule tous les radiobuttons environ un enum dans la source de données.

cette classe garde tous les boutons radio dans une liste et fait une recherche pour l'enum :

class RadioButtonBinding : ILookup<System.Enum, System.Windows.Forms.RadioButton>
{
    private Type enumType;
    private List<System.Windows.Forms.RadioButton> radioButtons;
    private System.Windows.Forms.BindingSource bindingSource;
    private string propertyName;

    public RadioButtonBinding(Type myEnum, System.Windows.Forms.BindingSource bs, string propertyName)
    {
        this.enumType = myEnum;
        this.radioButtons = new List<System.Windows.Forms.RadioButton>();
        foreach (string name in System.Enum.GetNames(this.enumType))
        {
            System.Windows.Forms.RadioButton rb = new System.Windows.Forms.RadioButton();
            rb.Text = name;
            this.radioButtons.Add(rb);
            rb.CheckedChanged += new EventHandler(rb_CheckedChanged);
        }
        this.bindingSource = bs;
        this.propertyName = propertyName;
        this.bindingSource.DataSourceChanged += new EventHandler(bindingSource_DataSourceChanged);
    }

    void bindingSource_DataSourceChanged(object sender, EventArgs e)
    {
        object obj = this.bindingSource.Current;
        System.Enum item = obj.GetType().GetProperty(propertyName).GetValue(obj, new object[] { }) as System.Enum;
        foreach (System.Enum value in System.Enum.GetValues(this.enumType))
        {
            if (this.Contains(value))
            {
                System.Windows.Forms.RadioButton rb = this[value].First();
                if (value.Equals(item))
                {
                    rb.Checked = true;
                }
                else
                {
                    rb.Checked = false;
                }
            }
        }
    }

    void rb_CheckedChanged(object sender, EventArgs e)
    {
        System.Windows.Forms.RadioButton rb = sender as System.Windows.Forms.RadioButton;
        System.Enum val = null;
        try
        {
            val = System.Enum.Parse(this.enumType, rb.Text) as System.Enum;
        }
        catch(Exception ex)
        {
            // cannot occurred if code is safe
            System.Windows.Forms.MessageBox.Show("No enum value for this radio button : " + ex.ToString());
        }
        object obj = this.bindingSource.Current;
        obj.GetType().GetProperty(propertyName).SetValue(obj, val, new object[] { });
        this.bindingSource.CurrencyManager.Refresh();
    }

    public int Count
    {
        get
        {
            return System.Enum.GetNames(this.enumType).Count();
        }
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.radioButtons.GetEnumerator();
    }

    public bool Contains(Enum key)
    {
        return System.Enum.GetNames(this.enumType).Contains(key.ToString());
    }

    public IEnumerable<System.Windows.Forms.RadioButton> this[Enum key]
    {
        get
        {
            return this.radioButtons.FindAll(a => { return a.Text == key.ToString(); });
        }
    }

    IEnumerator<IGrouping<Enum, System.Windows.Forms.RadioButton>> IEnumerable<IGrouping<Enum, System.Windows.Forms.RadioButton>>.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    public void AddControlsIntoGroupBox(System.Windows.Forms.GroupBox gb)
    {
        System.Windows.Forms.FlowLayoutPanel panel = new System.Windows.Forms.FlowLayoutPanel();
        panel.Dock = System.Windows.Forms.DockStyle.Fill;
        panel.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft;
        foreach (System.Windows.Forms.RadioButton rb in this.radioButtons)
        {
            panel.Controls.Add(rb);
        }
        gb.Controls.Add(panel);
    }
}

vous utilisez la classe dans un formulaire en ajoutant ce code dans le constructeur du formulaire:

    public PageView()
    {
        InitializeComponent();
        RadioButtonBinding rbWidth = new RadioButtonBinding(typeof(Library.EnumConstraint), this.pageBindingSource, "ConstraintWidth");
        rbWidth.AddControlsIntoGroupBox(this.groupBox1);
        RadioButtonBinding rbHeight = new RadioButtonBinding(typeof(Library.EnumConstraint), this.pageBindingSource, "ConstraintHeight");
        rbHeight.AddControlsIntoGroupBox(this.groupBox3);
        this.pageBindingSource.CurrentItemChanged += new EventHandler(pageBindingSource_CurrentItemChanged);
    }
0
répondu user1587368 2013-01-07 20:22:30

définissez le nom d'étiquette de vos boutons radio à quelque chose qui représente la valeur.

créer un paramètre de chaîne, par exemple, OptionDuplicateFiles, et lui donner la valeur par défaut du nom de la balise pour votre bouton radio par défaut.

pour sauvegarder votre bouton radio coché:

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

pour charger votre bouton radio coché:

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

Tada!

0
répondu paultechguy 2015-06-27 12:36:15

j'aime l'idée d'un RadioButtonGroupBox mais j'ai décidé de créer une version autonome. Il n'y a aucune raison d'ajouter de la valeur à l'attribut Tag ou d'introduire de nouveaux attributs de valeur. Tout bouton radio assigné est toujours un membre de la RadioButtonGroupBox et la séquence de radiobuttons est définie pendant le développement. Soo, j'ai modifié le code. Maintenant, je peux obtenir et définir le radiobutton sélectionné par position d'index, par nom de contrôle et par texte. Le texte BTW n'est utilisable que si votre Le texte est différent pour chaque radiobutton.

public class RadioButtonGroupBox : GroupBox
{
    public event EventHandler SelectedChanged = delegate { };

    int _nIndexPosCheckRadioButton = -1;
    int _selected;
    public int Selected
    {
        get
        {
            return _selected;
        }
    }


    public int CheckedRadioButtonIndexPos
    {
        set
        {
            int nPosInList = -1;
            foreach (RadioButton item in this.Controls.OfType<RadioButton>())
            {
                // There are RadioButtonItems in the list...
                nPosInList++;

                // Set the RB that should be checked
                if (nPosInList == value)
                {
                    item.Checked = true;
                    // We can stop with the loop
                    break;
                }
            }
            _nIndexPosCheckRadioButton = nPosInList;
        }
        get
        {
            int nPosInList = -1;
            int nPosCheckeItemInList = -1;

            foreach (RadioButton item in this.Controls.OfType<RadioButton>())
            {
                // There are RadioButtonItems in the list...
                nPosInList++;

                // Find the RB that is checked
                if (item.Checked)
                {
                    nPosCheckeItemInList = nPosInList;
                    // We can stop with the loop
                    break;
                }
            }
            _nIndexPosCheckRadioButton = nPosCheckeItemInList;
            return _nIndexPosCheckRadioButton;
        }
    }

    public string CheckedRadioButtonByText
    {
        set
        {
            int nPosInList = -1;
            foreach (RadioButton item in this.Controls.OfType<RadioButton>())
            {
                // There are RadioButtonItems in the list...
                nPosInList++;

                // Set the RB that should be checked
                if (item.Text == value)
                {
                    item.Checked = true;
                    // We can stop with the loop
                    break;
                }
            }
            _nIndexPosCheckRadioButton = nPosInList;
        }
        get
        {
            string cByTextValue = "__UNDEFINED__";
            int nPosInList = -1;
            int nPosCheckeItemInList = -1;

            foreach (RadioButton item in this.Controls.OfType<RadioButton>())
            {
                // There are RadioButtonItems in the list...
                nPosInList++;

                // Find the RB that is checked
                if (item.Checked)
                {
                    cByTextValue = item.Text;
                    nPosCheckeItemInList = nPosInList;
                    // We can stop with the loop
                    break;
                }
            }
            _nIndexPosCheckRadioButton = nPosCheckeItemInList;
            return cByTextValue;
        }
    }

    public string CheckedRadioButtonByName
    {
        set
        {
            int nPosInList = -1;
            foreach (RadioButton item in this.Controls.OfType<RadioButton>())
            {
                // There are RadioButtonItems in the list...
                nPosInList++;

                // Set the RB that should be checked
                if (item.Name == value)
                {
                    item.Checked = true;
                    // We can stop with the loop
                    break;
                }
            }
            _nIndexPosCheckRadioButton = nPosInList;
        }
        get
        {
            String cByNameValue = "__UNDEFINED__";
            int nPosInList = -1;
            int nPosCheckeItemInList = -1;

            foreach (RadioButton item in this.Controls.OfType<RadioButton>())
            {
                // There are RadioButtonItems in the list...
                nPosInList++;

                // Find the RB that is checked
                if (item.Checked)
                {
                    cByNameValue = item.Name;
                    nPosCheckeItemInList = nPosInList;
                    // We can stop with the loop
                    break;
                }
            }
            _nIndexPosCheckRadioButton = nPosCheckeItemInList;
            return cByNameValue;
        }
    }


    protected override void OnControlAdded(ControlEventArgs e)
    {
        base.OnControlAdded(e);

        var radioButton = e.Control as RadioButton;
        if (radioButton != null)
            radioButton.CheckedChanged += radioButton_CheckedChanged;
    }


    void radioButton_CheckedChanged(object sender, EventArgs e)
    {
        _selected = CheckedRadioButtonIndexPos;
        SelectedChanged(this, new EventArgs());
    }

}
0
répondu Jan Hoogma 2015-07-24 17:36:44

Mon approche est de mettre chaque bouton radio dans son propre panneau avant de se lier à une propriété booléenne:

    public static Binding Bind<TObject>(this RadioButton control, object dataSource, string dataMember)
    {
        // Put the radio button into its own panel
        Panel panel = new Panel();
        control.Parent.Controls.Add(panel);
        panel.Location = control.Location;
        panel.Size = control.Size;
        panel.Controls.Add(control);
        control.Location = new Point(0, 0);

        // Do the actual data binding
        return control.DataBindings.Add("Checked", dataSource, dataMember);
    }
0
répondu SiuFay 2015-09-03 07:16:21
try
    {
      val = System.Enum.Parse(this.enumType, rb.Text) as System.Enum;
    }
    catch(Exception ex)
    {
      // cannot occurred if code is safe
      System.Windows.Forms.MessageBox.Show("No enum value for this radio button : " + ex.ToString());
    }
    object obj = this.bindingSource.Current;
    obj.GetType().GetProperty(propertyName).SetValue(obj, val, new object[] {
    }
  );
  this.bindingSource.CurrencyManager.Refresh();

Si une erreur se produit dans le bloc try, le bloc catch sera exécuté. Le code continuera à s'exécuter après le bloc de capture. Comme il n'y a pas eu de traitement de la source de liaison, les variables qui suivent la capture pourraient se retrouver dans un état indéterminé et jeter une autre exception qui peut ou ne peut pas être traitée.

Une meilleure approche est comme suit

 try
        {
            val = System.Enum.Parse(this.enumType, rb.Text) as System.Enum;

        object obj = this.bindingSource.Current;
        obj.GetType().GetProperty(propertyName).SetValue(obj, val, new object[] { });
        this.bindingSource.CurrencyManager.Refresh();
        }
        catch(EntityException ex)
        {
            // handle error
        }
        catch(Exception ex)
        {
            // cannot occurred if code is safe
            System.Windows.Forms.MessageBox.Show("No enum value for this radio button : " + ex.ToString());
        }

cela permet de traiter l'erreur de valeur enum ainsi que d'autres erreurs qui peuvent se produire. Cependant, utilisez L'EntityException ou ses variantes avant le bloc Exception (tous les décendents D'Exception doivent venir en premier). On peut obtenir des informations d'état d'entité spécifiques pour une erreur de framework d'entité en utilisant les classes de framework d'entité au lieu de la classe de base D'Exception. Cela peut être utile pour déboguer ou fournir des messages d'exécution de temps plus clairs pour l'utilisateur.

quand je mets en place des blocs try-catch j'aime le voir comme un" calque " sur le dessus du code. Je prends des décisions sur le flux des exceptions à travers le programme, leur affichage à l'utilisateur, et quel que soit le nettoyage nécessaire pour permettre au programme de continuer à fonctionner correctement sans objets dans un état indéterminé qui peut passer à d'autres erreurs.

0
répondu Creative 2017-12-04 15:39:31