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

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.
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
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
Chris StanyonWebDevCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.