views:

168

answers:

1

Hi all,

I have barely any experience with WinForms, but I'm fairly sure that this is a simple task. I just need to enable and disable the Enabled property of a textbox based on the SelectedIndex of a ComboBox.

Can this be done in the designer using DataBindings, or am I required to write a handler of some kind?

A: 

You can bind it, but you have to write a Value -> Boolean converter to do the logic. I would suggest since winforms doesn't support the ViewModel paradigm you just go with an event handler as you'd likely have to define your databind in code anyway.

public void MyComboBox_SelectedIndexChanged(object sender, EventArgs args)
{
   ComboBox box = sender as ComboBox;
   if (box != null) return;

   switch(box.Text)
   {
      case "Value1":
      case "Value2":
      case "Value3":
         myTextBox.Enabled = false;
         break;
      default:
         myTextBox.Enabled = true;
   }
}
Aren
Yeah, I already have a lot of code like this, but I'm trying to move it towards the designer so that the code is less cluttered. It's a valid point that conversion is required in this case. I also have a similar situation where Enabled is bound to a CheckBox.Checked, but I can't seem to enter the databinding through the designer--only by manually coding it into the designer file, after which point it shows up (greyed out) in the designer.
bwerks
To further specify, when I'm in the "Formatting and Advanced Binding" window opened from the "(DataBindings)" section in a control's Properties window, the "Binding:" list is always empty, and I don't know how to add things to it.
bwerks