0

I have my own textbox which inherits System.Windows.Forms.TextBox
I am trying to display texts like 5000000 formatted ==> 5,000,000 but the problem is that Control.Text should return 5000000 but it should display 5,000,000.

I know it is WTF, but i really need it and i couldn't Google a lot because my native language is not English(and anyone can get it from my grammar and Im sorry for that).

5 Answers 5

1

When you want to retrieve it, convert the displayed string to an integer using int.Parse and CultureInfo.CurrentCulture then convert it back to a string using ToString and CultureInfo.InvariantCulture.

1
  • 1
    Neither have I, try it, but how else would you know the two values are equivalent - there has to be some conversion somewhere.
    – Jeff Yates
    Commented Mar 29, 2010 at 18:55
1

(Note: haven't got time to look up the right method names, but hopefully I'm close enough to make sense...)

Create a custom control that derives from TextBox.

Add handlers to the control for focus events (or better, override the methods for OnFocus/OnBlur). When the control loses focus, store the current text in a private variable (say, OriginalText) and update the actual text to your formatted version. When the control gets focus, reinstate the original text.

1
  • +1 for OnBlur(i didn't know about that),but i have already done other things before but the problem was "how to show it?".
    – Behrooz
    Commented Mar 29, 2010 at 19:03
1

You can use the tag property, as Andrey suggested, and update the value of the tag property in the TextChange event.

0
0

use Tag property that every control has

2
  • @Andrey:unfortunately the text is not readonly, and the user should fill it.
    – Behrooz
    Commented Mar 29, 2010 at 18:49
  • I will try to do it if other people don't send better answers.
    – Behrooz
    Commented Mar 29, 2010 at 18:50
0

You can keep origianl value in the Tag property (object).

Not the answer you're looking for? Browse other questions tagged or ask your own question.