Solved

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

Posted on 2014-09-27
2
610 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 43

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

807 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