Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

You can read all about formatting starting here;

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconformattingtypes.asp

 

As for your specific problem, I'm not really sure why the compiler is throwing a hissy fit over ToString("N") (perhaps it doesn't 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).

Gamer extraordinaire. Programmer wannabe.
Posted

Look again. Here's 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. I'm not surprised it doesn't 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?

Posted

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

Gamer extraordinaire. Programmer wannabe.
Posted

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.

Posted
In .NET you'd 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.
Gamer extraordinaire. Programmer wannabe.
Posted

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 don't 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 can't 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.

  • *Experts*
Posted

For things like phone numbers, you'll 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 don't want to use a MaskEdBox (a good thing), you'll 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 you're not using the binding features, you can use a couple of events (GotFocus and Leave are two common ones). If you're 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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...