The first one is the source of the data. It can be a DataSet or a custom object. The second one is the data binding display and third one is the component display.
Let me explain. If you want to display only the first 4 characters of a string to the screen but let the user write all what he wants (and of course only append data if necessery). You need to use what we have seen before : Format. The Format method of the Binding object let change the display information to the screen without changing the real data of the object. The code below is an exemple that truncate the string if it's more than 4 characters.
void b_Format(object sender, ConvertEventArgs e)
{
if (e.Value.ToString().Length > 5)
{
e.Value = e.Value.ToString().Substring(0, 4) + "...";
}
}
You can see that the value from the ConvertEventArgs contain the value from the object. If the length of the string is over 5 than we substring and all three dots. If you try, you will see that everything is fine until you set the focus on the control because when the get the focus, you still only have the 4 first characters and the three dots. In fact, we want to full string that contain the object below the data source object. To get that work, it needs some transformation. The transformation is needed when the focus is on the control. If you have for example a Text Box than you need to add an event on the Enter event. The code below show a brief example.
void textBox1_Enter(object sender, EventArgs e)
{
(sender as TextBox).Text = ((Product)bs.Current).Name;
}
As you can see, we convert the first parameter to get the Text property. We could have simply use the name of the control but it's much more elegant and ensure that future name change won't interfere with this event. This event set the name of the product but which one is the current one? The answer is with the BindingSource.Current. The binding source for this example was a list of Product. To know the current product, the binding source has a Current method. Once casted, you can have all public method of the object and than have the original full name.
The code below is a complet example. You need to add 1 textbox and 2 buttons.
BindingSource bs = new BindingSource();
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
List
listProduct1.Add(new Product(100, "Pantalon"));
listProduct1.Add(new Product(101, "Chemise"));
listProduct1.Add(new Product(102, "Casquette"));
bs.DataSource = listProduct1;
Binding b = new Binding("Text",bs,"Name");
b.Parse += new ConvertEventHandler(b_Parse);
b.Format += new ConvertEventHandler(b_Format);
textBox1.DataBindings.Add(b);
textBox1.Enter += new EventHandler(textBox1_Enter);
}
void textBox1_Enter(object sender, EventArgs e)
{
(sender as TextBox).Text = ((Product)bs.Current).Name;
}
void b_Format(object sender, ConvertEventArgs e)
{
if (e.Value.ToString().Length > 5)
{
e.Value = e.Value.ToString().Substring(0, 4) + "...";
}
}
private void button2_Click(object sender, EventArgs e)
{
if (bs.Position < (bs.Count-1))
bs.MoveNext();
else
{
bs.MoveFirst();
}
}
No comments:
Post a Comment