I have a Form with TextBox on it like this:
Form f = new Form();
TextBox t = new TextBox ();
t.Click += new EventHandler(t_Click);
t.LostFocus += new EventHandler(t_LostFocus);
Testus tt = new Testus();
t.DataBindings.Add("Left", Testus , "X");
t.DataBindings.Add("Text", Testus , "Test");
f.Controls.Add(t);
f.ShowDialog();
And Testus class like this:
class Testus
{
public string Test
{
get
{
return _text;
}
set
{
Console.WriteLine("Acomplished: text change");
_text = value;
}
}
private string _text;
public int X
{
get
{
return x;
}
set
{
Console.WriteLine("Acomplished: X changed");
x = value;
}
}
int x;
public Testus()
{
}
}
As you can see, I bind my TextBox to Testus class. Specifically I bind TextBox.Left to Testus.X and TextBox.Text to Testus.Test. I would like to acomplish that changing Controls Left value will affect Testus.X value and in reverse. And the same for TextBox.Text vs Testus.Test.
I've added handlers for Click and LostFocus of my TextBox control like this:
static void t_LostFocus(object sender, EventArgs e)
{
Console.WriteLine("TextBox lost focus");
}
static void t_Click(object sender, EventArgs e)
{
Console.WriteLine("Moving to right...");
((Control)sender).Left = 100;
}
And I do this test:
- run application
- enter text into TextBox
- Change focus to other control
And I get this results in Console:
TextBox lost focus
And thats it! Testus.Test does not change it's value!?
But when I do this:
- run application
- click on textbox (change left value)
I get this results:
Moving to right...
Acomplished: X changed
It seems that Binding Left to X works. And Text to Test not. And when I will change places of bindings to this:
t.DataBindings.Add("Text", Testus , "Test");
t.DataBindings.Add("Left", Testus , "X");
Than Text Binding works and left to X binding does not. So in conclusion: only first DataBinding works.
So my quesion is: how to binding two properties of TextBox (Left, Text) to two properties of my object (Testus) (X, Test) so that it will work good?