Stécy
Stécy

Reputation: 12339

Is it possible to data bind a combo box and a text box between them?

Let's say you have a combobox and a textbox.

The combo box contains a list of items and I would want to bind the textbox "text" property to the current combobox selection.

In WPF this could be done but I've not found a way in WinForms.

Is there a way or is WinForms too limited?

Notes:

Upvotes: 1

Views: 1857

Answers (3)

IAbstract
IAbstract

Reputation: 19881

Yes you can bind the text box to the item (Text) selected in a combo box:

   public partial class Form1 : Form {
      Binding binding;

      public Form1() {
         InitializeComponent();

         binding = new Binding("Text", comboBox1, "Text");
         textBox1.DataBindings.Add(binding);
      }
   }

Upvotes: 2

Rasmus Søborg
Rasmus Søborg

Reputation: 3695

Here you go: These lines creates a delegate, whose begin called whether the selected index has been changed under the item. Afterwards it sets the textboxs, text property to the selected items text from the combobox :)

            TextBox txtbox = new TextBox();
            ComboBox cob = new ComboBox();
            cob.SelectedIndexChanged += new EventHandler(delegate(object sender, EventArgs e) { if (cob.SelectedIndex > -1) txtbox.Text = cob.Items[cob.SelectedIndex].ToString(); });

Upvotes: 0

Machinegon
Machinegon

Reputation: 1885

You could Add an onSelectedIndexChange event handler on the combo box and modify the textbox form there.

Upvotes: 2

Related Questions