Solved

Why the argument of DataBindings("Text") is Text?   vb.net

Posted on 2014-09-27
2
581 Views
Last Modified: 2014-10-06
There is a procedure FormatPhoneNumber(...) which is hooked to the Format event of a PhoneTextBox using:

Dim b as Binding = PhoneTextBox.DataBindings("Text")
AddHandler b.Format, AddressOf FormatPhoneNumber

Q1: Why the argument of DataBindings("Text") is Text?

What is another argument choice beside "Text" argument in Q1? Can you please build and example using it to illustrate its function?

Q2: How can I add the same event to FaxTextBox?
Q3: What are some alternate ways of writing the above 2 lines of code?  I was expecting to see .Add in this code example but there is none.

Thank you.
0
Comment
Question by:Mike Eghtebas
2 Comments
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 200 total points
ID: 40348813
Q1: You could bind another property.

As an example, you have an option that let's the user select the color of different elements of the interface, you could save that selection in a database and then bind the field in which you recorded the color with the following:

Dim b as Binding = PhoneTextBox.DataBindings("BackColor")

Q2: You can't. If the programmer who designed the Control did not see fit to provide you with a specific event, then you are out of luck. You can inherit from a Control and add events for the features that you add yourself in the derived class, but reacting to what happens in the base control when the original does not already have the event is usually impossible.

I assume that your FaxTextBox and PhoneTextBox come from the same library. In such a case, it could be an oversight of the programmer or simply a bad design. Controls that have similar functions should have similar structures.

This is also typical of controls designed in-house. The programmers are always on a rush to meet deadline, and they do not add properties, methods or events unless the initial application for which they create them do not need it. They might have needed it for the phone but not for the fax.

Q3: There is an Add method to the Bindings property of most of the framework controls. But your controls are not from the framework. There might be unknown reasons to do that but at first sight, same a for Q2, this apparently shows bad design decisions from the creator of these controls, or code that was written by somebody who does not have a lot of experience about inheritance in the framework.

When you create controls you should follow the conventions used for other controls in the framework. What is strange, and since the Control classes and TextBox class both have a Public Bindings.Add method, and since new input controls are usually derived from one of those, that method was there by default. It had to be hidden if you cannot use it.
0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 300 total points
ID: 40350689
The argument of the DataBindings() method is the name of the property you want to retrieve the binding for. In your case, you're retrieving the binding info for the Text property of your PhoneTextBox - I'm guessing that you've set up this binding somewhere else! You'll get a Null exception if not. As James says, it can be any property of your control that is bound to a data value.

If your PhoneTextBox and FaxTextBox are simple textbox controls, then just add the event handler:

AddHandler PhoneTextBox.DataBindings("Text").Format, AddressOf FormatPhoneNumber
AddHandler FaxTextBox.DataBindings("Text").Format, AddressOf FormatFaxNumber

Open in new window


Make sure you create your FormatFaxNumber() method with the correct signature (same as FormatPhoneNumber)

As for the .Add() that'll probably be elsewhere in your code - it's used to set up the data bindings - you've only shown us 2 lines of code, so impossible to know for sure. Here's a quick sample of binding a data value (the Telephone property of yourDataSource) to a control property (Text):

PhoneTextBox.DataBindings.Add(New Binding("Text", yourDataSource, "Telephone"))

Open in new window


Here's more of a snippet which creates the bindings, sets up the events and then adds the bindings to the textboxes:

Dim b As Binding

b = New Binding("Text", yourDataSource, "Telephone")
AddHandler b.Format, AddressOf FormatPhoneNumber
PhoneTextBox.DataBindings.Add(b)

b = New Binding("Text", yourDataSource, "Fax")
AddHandler b.Format, AddressOf FormatFaxNumber
FaxTextBox.DataBindings.Add(b)

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now