I can't seem to get basic databinding to work in the WinForms designer. (using VS2012)
- I create a new Win Forms Application project called DatabindTest.
- To this project I add a class called Class1.cs.
- In this class I create a (String) property called
MyProperty
along with a constructor that setsMyProperty
to "abc". - I build the solution.
- Using the Form1 designer, I add a Textbox to the form (
textBox1
). - In the Properties of
textBox1
, I expand DataBindings and open the Advanced dialog. - I expand the Binding dropdown, and click Add Project Data Source..., and select the Object datasource. Then I expand the
DatabindTest
node and selectClass1
as the Data Object. - I confirm that the Binding field now says "class1BindingSource - MyProperty" (as expected).
- In Form1.cs at the start of the
Form1
class, I create a new instance of Class1 (see code below). - At this point, I build and start the program. I expect to see "abc" in the textbox, but it is empty.
What have I done wrong?
public partial class Form1 : Form
{
Class1 c1 = new Class1();
public Form1()
{
InitializeComponent();
//this.textBox1.Text = c1.MyProperty; //if I uncomment this line,
//"abc" appears in textBox1
//so why not through databinding?
}
}