Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 678
  • Last Modified:

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.
0
Mike Eghtebas
Asked:
Mike Eghtebas
2 Solutions
 
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 StanyonCommented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now