?
Solved

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

Posted on 2014-09-27
2
Medium Priority
?
646 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 800 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 1200 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

801 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