开发者

how to display formatted text but have the actual text

开发者 https://www.devze.com 2022-12-24 07:45 出处:网络
I have my own textbox which inherits System.Windows.Forms.TextBox I am trying to display texts like 5000000 formatted ==>5,000,000

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 kno开发者_运维技巧w 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).


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.


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


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


use Tag property that every control has


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号