Link to home
Start Free TrialLog in
Avatar of Jimby_Aus
Jimby_AusFlag for Australia

asked on

How do I format a string in databound control property?

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.
Avatar of BradB3030
BradB3030
Flag of United States of America image

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.
Avatar of Jimby_Aus

ASKER

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).





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.
ASKER CERTIFIED SOLUTION
Avatar of Sancler
Sancler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial