Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Super Charge Your Forms-based Apps with Advanced Data Binding  : Page 3

Data binding can be used for more than just data. When you want to change the value of controls based on the state of other controls, data binding is the right solution.


advertisement
Better Formatting
The code in the earlier section is very rudimentary; the following code can handle any color name typed in TextBox1.

If (TypeOf (e.Value) Is String) Then Try e.Value = Color.FromName(CType(e.Value, String)) Catch ex As Exception e.Value = Color.Empty End Try ' make sure we got a known color. If not, return empty ' If (Not CType(e.Value, Color).IsKnownColor) Then e.Value = Color.Empty End If End If

This code checks the user-entered value to make sure it is a string. Then the text is cast to a color type. Exceptions are handled accordingly.

Multiple Binding
What if you need to bind multiple controls in a chain? For example, imagine that you wanted to bind a ComboBox in your Panel to the Text Box. This in turn hooks up to the BackColor of the Form, creating a chain reaction.



I've added a number of color names to the ComboBox1 collection. The next step is to hook the ComboBox1 selected text property to the TextBox1 text property. You don't need to bother binding between the TextBox1 selected text and the BackColor of the form as that has already been done in the earlier piece of code.

To the form load event, you need to add the following, after the comment ComboBoxBinding, in the DataBound.vb file:

Me.TextBox1.DataBindings.Add("Text", ComboBox1, "Text")

Unlike the earlier binding you added for the BackColor of the form, this is a simple data binding.

Hit F5 to run the application, and choose a color from the ComboBox. Your application should resemble Figure 3.

Figure 3. Binding Multiple Controls: Here, the user can change the background color of the form by selecting a color from a ComboBox, which populates the TextBox used in Figure 2.
Versed in Versatility
Data binding can be used for more than just data. There are many practical problems that data binding can solve.

For example, consider a data entry form where a user is entering an amount for money being withdrawn or spent for a particular item. On this form, the user's cash balance is shown in varying colors: Orange if the balance is low and red if the balance is precariously low or under the account limit. Here you would alter the BackColor of the calculated balance TextBox depending on the value entered.

Similarly take an instance where users change their application settings, such as color, font, etc., using an options dialog. This kind of binding provides an instant preview and change the properties of your application on the fly. You could utilize a control like the PropertyGrid for this, but in simple scenarios it's overkill.

Thanks to a very flexible, general implementation, the places where you can apply the data binding infrastructure are innumerable. With just a little practice and a little time, you'll undoubtedly find many uses in your applications.



Sanjay Shetty is the CEO of Wireless Strategist & Consultants, a consultancy for mobility, design, architecture, and .NET migration. He is the Microsoft regional director in Mumbai, India. Reach him via .
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date