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

x
?
Solved

How do I format a string in databound control property?

Posted on 2006-06-15
5
Medium Priority
?
769 Views
Last Modified: 2012-06-21
For the life of me I can't figure out how to format a string, when I bind a forms text property to an item in bindingsource (which is then linked to a data table).

Heres my line of code:
Me.DataBindings.Add("text", bsBroker, "company", True, DataSourceUpdateMode.Never, "", "??????")

I cant figure out the formatstring parameter, or if I have figured it out, it's not working.....
All I want to do is put a string prefix and/or maybe a suffix after the field data (ie. "Form Label - company")
The data bind works fine, but all I get is the company field data, no formatted text.

Im using VS2005, Any help appreciated.
0
Comment
Question by:Jimby_Aus
  • 2
4 Comments
 
LVL 4

Expert Comment

by:BradB3030
ID: 16922168
I'm confused about what you're asking, could you reply with a few examples?

like if...
companyField = "compOne"

do you want something like...
result = "Form Label - compOne" ?

if so, just do something like this...
result = "Form Label - " & companyField

otherwise, please post a better example so I can help you better.
0
 
LVL 1

Author Comment

by:Jimby_Aus
ID: 16922208
I have a textbox with the .text property bound to a bindingsource field item called "company"
If the datasource string for the field "company" is "Microsoft" for example, then I want to know how to automatically have the bound textbox text property display "Broker - Microsoft".

I posted the line of code in question, the question marks represent the databinding parameter "formatstring", I want to know what the proper formatstring is to achieve this affect (assuming there is one).





0
 
LVL 4

Expert Comment

by:BradB3030
ID: 16922392
ahh, I understand now I think...

You should use format string if you have needs like white space in between the "Broker - " and "Microsoft" (in your example)

Some examples of this.  The arguments that are formatted are placed between vertical bar characters (|) to highlight the resulting alignment.  You don't need them for anything, it's just to display the example better.

1. FormatFName = String.Format("First Name = |{0,10}|", myFName)
2. FormatLName = String.Format("Last Name = |{0,10}|", myLName)
3. FormatPrice = String.Format("Price = |{0,10:C }|", myInt)
4. FormatFName = String.Format("First Name = |{0,-10}|", myFName)  <-- Note the minus sign for the different output
5. FormatLName = String.Format("Last Name = |{0,-10}|", myLName)
6. FormatPrice = String.Format("Price = |{0,-10:C }|", myInt)

1. First Name = |          Fred|
2. Last Name = |         Opals|
3. Price = |           $100.00|
4. First Name = |Fred      |
5. Last Name = |Opals     |
6. Price = |$100.00   |


At any rate, this doesn't seem totally necessary and I might still just go "Broker - " & company instead of using Format String if I were you...

Good luck.
0
 
LVL 34

Accepted Solution

by:
Sancler earned 1500 total points
ID: 16922980
I don't think you can do what you want.  The sort of formatting that is envisaged with the FormatString that you can attach to a Binding is, for want of a better expression, "simple" formatting.  Have a look at this

http://msdn2.microsoft.com/en-us/26etazsy.aspx

What I think you are seeking to do is what is referred to in the fourth paragraph of that as "composite formatting" which, as it says "uses one or more format strings to EMBED one or more formatted values in a result string" - I have added the EMPHASIS.  If you follow the link from that page to "Composite Formatting"

http://msdn2.microsoft.com/en-us/txafckwd.aspx

it makes clear that "The .NET Framework composite formatting feature takes a list of OBJECTS and a composite format string as input" - again EMPHASIS added.

Now, as I understand it, the problem that you face with trying to use this sort of composite formatting for a Binding's FormatString is that it accepts only a String: it has no capacity to accept, as well, the OBJECT (or list of objects) which is to replace the placeholders in the composite format string.  If the object/value to which the binding relates is, for example, a DateTime then that can be formatted just with a String - e.g. "dd/MM/yyyy hh:mm" - and the FormatString of the Binding object will accept it.  But to do what you want to do - that is, add some standard text before a value - you would need String.Format("Form Label - {0}", company) where company was the value that you wanted substituting for the {0}.  But that involves company as an object, or value, not as a String.  And because the FormatString property of the Binding object will only accept a String, it will either produce an error, or nothing will happen.

An alternative would be to add an expression column to your datatable, with its .Expression set to something like "'Form Label - ' + company" and bind your form .Text to that.

Roger
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…

564 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