C# type conversion in controls

NewToC

Member
Joined
Jun 26, 2003
Messages
10
I am not new to programming but I am new to programming Windows controls. So let me ask the ultimate simple question. Beleive it or not, I have searched Help and a book or two and cant find a simple example.

I have data in an SQL file that is a double. I want to display it in a text box and allow the user to edit it. I want it to appear in the format 999,999.99 when the form is populated. If the user changes the number, as they leave the textbox, change to format of the new number to 999,999.99.

I do not want to use a MaskEdit control - I want a pure .Net solution.

I am filling the textbox like this:
PricetextBox.Text =prop.Price.ToString("N");

I need to put code in the PricetextBox_Validated event handler (or somewhere similar) that will put newly entered data in the same format.

I tried PricetextBox.Text = PricetextBox.Text.ToString("N") but the compiler choked.

In addition to an answer, I would also appreciate a link to some on line help in the entire text box input/output formatting area.

Thanks
 
You can read all about formatting starting here;
http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconformattingtypes.asp

As for your specific problem, Im not really sure why the compiler is throwing a hissy fit over ToString("N") (perhaps it doesnt like trying to format strings as numeric values). Perhaps you should try either ;

Double.Parse(prop.Price.ToString()).ToString("N");

or

String.Format("{0:N}", prop.Price.ToString());

Although I have a funny feeling that String.Format will throw errors if the ToString is (again, trying to format strings as numbers.. no idea).
 
Look again. Heres the line the compiler chokes on:

PricetextBox.Text = PricetextBox.Text.ToString("N")

Note I am trying to format the text in the text box, which is already a string. Im not surprised it doesnt work, but it shows what I am trying to do.

If the Text in the textbox is 123456 I could process it one character at a time to create a new string 123,456 - I just thought with all the power of .NET there would be an easier way.

Is there?
 
*looks blankly*

Did the idea in my post not work? Refer to my post above, try;
PricetextBox.Text = Double.Parse(PricetextBox.Text).ToString("N")

Also try PricetextBox.ToString("N") (notice the .Text is gone), or String.Format("{0:N}", PricetextBox.Text).. both of which would be preferable then parsing a double then recasting it back to a string.
 
Only the Double.Parse works, but that seems more efficient than processing the entire string.

But then, how about a phone number? After the user enters one into a textbox, do you process it character by character into (999)999-9999 format, or is there a better way?

This is all stuff the MaskEdit control did in earlier versions. I am just curious about the .NEt aternatives.

Thank you for helping.
 
In .NET youd use regular expressions. I know ASP.NET has some premade controls that offer different types of filtering/formatting like phone numbers etc, not sure about a regular window forms control. You can easily make your own though. Look up the Regex class in MSDN or your object browser.
 
I have been looking at regex and can only see where it solves half the problem. Am I the only person who has ever tried to deal with a phone number in a text box in .net, or am I the only one too dense to figure out how? Please dont answer that.


I can test the text like this:

if !(Regex.Match(txtPhone.Text, @"^[1-9]\d{2}-[1-9]\d{2}-\d{4}$"))) {format it};

Can I use regular expressions to _create_ the formatted string? I cant quite see how. I could easily process each character in the string and insert the formatting characters, but if Regex can do it, that would be a nicer solution.
 
For things like phone numbers, youll have to convert yourself. Assuming your Database always stores the number as "5551112222" then you can easily format this to meet your needs.

Since you dont want to use a MaskEdBox (a good thing), youll have to manually parse the string into a valid phone number. A regular expression is an easy (relatively) way to validate the data they typed is in a "good enough" format such as "(xxx) xxx-xxxx" or "xxx xxx-xxxx" or other combos. Using regular expressions, you can name each group and extract out the info so that you can use one regex to test AND extract data without having to worry "did they use format #1 or format #2".

As for where you do the parsing, if youre not using the binding features, you can use a couple of events (GotFocus and Leave are two common ones). If youre binding, you can use the Format and Parse events which are meant to do exactly what you want.

Converting numbers are easier since you can use the built in parse and format functions for each datatype. You will still have to decide which events to use or go with binding.

-Nerseus
 
Thanks for the response. The coding will be easy, I just wanted to be sure I understood the "accepted" method.
 
Back
Top