1

Due to differences between naming and enum I cannot simply bind. Thought of the following:

  • Use an array of translated values as datasource;
  • Databinding the property with format/parse events to convert between enum and displayed values.

Example:

BindingSource bs = new BindingSource();
bs.DataSource = this.testdata;

this.textBox1.DataBindings.Add("Text", this.bs, "CurrentState", true, DataSourceUpdateMode.OnPropertyChanged);
this.textBox1.ReadOnly = true;
this.comboBox1.DataSource = new List<string>() { "Goed", "Slecht", "Lelijk", "-" };
Binding b = new Binding("SelectedValue", this.bs, "CurrentState", true, DataSourceUpdateMode.OnPropertyChanged);
b.Parse += new ConvertEventHandler(TextToState);
b.Format += new ConvertEventHandler(StateToText);
this.comboBox1.DataBindings.Add(b);

[...]

   void StateToText(object sender, ConvertEventArgs e)
    {
        State state = (State)Enum.Parse(typeof(State), e.Value as string);

        switch (state)
        {
            case State.Good:
                e.Value = "Goed";
                break;
            case State.Bad:
                e.Value = "Slecht";
                break;
            case State.Ugly:
                e.Value = "Lelijk";
                break;
            default:
                e.Value = "-";
                break;
        }
    }

    void TextToState(object sender, ConvertEventArgs e)
    {
        switch (e.Value as string)
        {
            case "Goed":
                e.Value = State.Good;
                break;
            case "Slecht":
                e.Value = State.Bad;
                break;
            case "Lelijk":
                e.Value = State.Ugly;
                break;
            default:
                e.Value = State.None;
                break;
        }
    }

This is just an example to test the functionality. The textbox is used to verify the value displayed in the combobox is really the value of the databound property.

This code works. However there are two issues I cannot get around:

  • The combobox selects the first value in the datasource (not the state of the property) until the selected item is changed one time. Then the binding works fine.
  • I cannot close the form.

I do not understand why the binding is failing to update on load. I tried resetting the binding, etc., but nothing works. Also I have no clue as to why the form is prevented from closing.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RB84
  • 331
  • 4
  • 15
  • Use the DescriptionAttribute - see http://stackoverflow.com/questions/4367723/get-enum-from-description-attribute – Paul Sullivan Mar 09 '13 at 20:19
  • Thanks for your answer. This seems like a good alternative to avoid the issues I am experiencing. However, I'm still interested as to why I'm experiencing the two described issues. Would like to understand what is happening. – RB84 Mar 09 '13 at 20:33

1 Answers1

2

Why don't you consider writing a class that contains the key/value pairs you want to use? Here is a small example implementation assuming you have a combobox named combobox1.

namespace WindowsFormsApplication1
{

   public enum State {None, Good, Bad, Ugly }

    public partial class Form1 : Form
    {
        List<StateTextPair> pairs;

        public Form1()
        {
            InitializeComponent();
            InitializeList();
            this.comboBox1.DataSource = pairs;
            this.comboBox1.ValueMember = "State";
            this.comboBox1.DisplayMember = "Name";
            this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
            ComboBox box = (ComboBox)sender;
            StateTextPair selected = (StateTextPair)box.SelectedItem;
            switch (selected.State) {
                case State.None:
                    // do nothing
                    break;
                default:
                    MessageBox.Show(selected.State.ToString() + " == " + selected.Name);
                    break;
            }
        }

        private void InitializeList() {
            pairs = new List<StateTextPair>();
            pairs.Add(new StateTextPair(State.None, " - Please select a value - "));
            pairs.Add(new StateTextPair(State.Good, "Goed"));
            pairs.Add(new StateTextPair(State.Bad, "Slecht"));
            pairs.Add(new StateTextPair(State.Ugly, "Lelijk"));
        }
    }

    public class StateTextPair {
        private string name;
        private State state;

        public State State { get { return state; } }
        public string Name { get { return name; } }

        public StateTextPair(State s, string n)
        {
            this.name = n;
            this.state = s;
        }
    }
}
Gojira
  • 2,941
  • 2
  • 21
  • 30