C# custom mask for textbox - c#

I know there is masked textbox component for C#, but what I need is to create masked text box which requires entered text in format: LLL/LLL but when I enter such mask into Mask property in preview and mask I see separator "." but not "/" as I want to have. Any help?
Thanks

The / character is the date separator character in the mask. What you'll actually get depends on your culture preferences. To get a literal / you'll have to escape it with a \. Like this:
this.maskedTextBox1.Mask = #"LLL\/LLL";
Don't use the # when you use the Properties window.

Thanks for this clue
there is one more problem in maskedtextbox that is when the system short date changes the mask also changes for example..
Before
System date : d/M/yy
Mask Format : __/__/__
After
System date : d-M-yy
Mask Format : __-__-__
Using escape char hepled me.
Just add escape char in mask. For example:
textbox1.Mask = 00/\00/\00

Related

C# display / (forward slash) in MaskedTextBox

I noticed that when the mask defined for input in a MaskedTextBox contains "/", it is automatically substituted by "-" in the textbox.
I tried this using the default Date format available in VS, which in the form's Designer results in the following code
this.maskedTextBox2.Name = "maskedTextBox2";
this.maskedTextBox2.Mask = "00/00/0000";
this.maskedTextBox2.ValidatingType = typeof(System.DateTime);
and also for another MaskedTextBox by defining my custom mask in the form's constructor like this
InitializeComponent();
this.maskedTextBox1.Mask = #"00/00/0000";
In both cases the prompt displayed in the text boxes looks like this
__-__-____
Is there a way to actually display slashes there, instead of dashes?
Marek
According to this page, a slash is a 'date separator' and therefore I'm guessing that your system currently runs in a locale where a date separator is a dash.
/ Date separator. The actual display character used will be the date symbol appropriate to the format provider, as determined by the control's FormatProvider property.
If you really DO want a forward slash then "00\/00\/00" should do the trick (but then it's not really a compliant date input).

Parsing Text from a Masked Text Box

I have a WinForms application written in C#
I have until recently many textboxes on my forms where the user inputs financial amounts. I have not incorporated any form of mask initially and whenever I need to work with the values input by the users I would Parse the text from each box into Decimal values with Decimal.Parse;
However I have been asked to make the textboxes look like financial amounts
i.e. £1,050.75 rather than 1050.75
I therefore started to change the textboxes into MaskedTextBox and gave them a Mask of £#,##0.00
However now each attempt to Parse the text from the MaskedTextBoxes gives an error 'Input string not in the correct format'.
How do I obtain the users input from the MaskedTextBox and parse into decimal format to work with?
Should I be using MaskedTextBox at all, or is there another way of showing a financial type formatting on the form, without effecting the Decimal.Parse method?
When you are getting the value from Maskedtextbox, it is taking the value as £#,##0.00 . so the symbol will not be converted to decimal. Try to remove the symbol and convert the value to decimal. like
string val= maskedTextBox1.Text.Replace("£","");
Decimal.Parse(val);
You can use a format option with AllowCurrencySymbol. It has to match the currency symbol of the culture. This code I converted from VB so I hope it's correct.
Application.CurrentCulture = New Globalization.CultureInfo("en-GB");
Decimal.Parse("£12,345.67", Globalization.NumberStyles.AllowThousands | Globalization.NumberStyles.AllowDecimalPoint | Globalization.NumberStyles.AllowCurrencySymbol);
Also, see this question if you don't want to change the culture:
Problem parsing currency text to decimal type
You can check MaskFull to see if text is properly enter and then apply anti-mask (by removing that, what your mask is adding).
Unfortunately, I am not aware about automated unmasking. But you can do something like:
if(maskedTextBox1.Mask)
{
var enteredText = maskedTextBox1.SubString(1).Replace(",", null); // remove pound symbol and commas
// ... parse as with normal TextBox
}

C# Windows form text box format with data binding

I have a windows form with a text box: txtMyText.
txtMyText.Text is Bound to a data source: long lMyLongValue.
On the form I would like the value to display as a six digit value with leading zeros. Example 000123.
How can this be accomplished?
I believe Binding.Format event, can be pretty suitable for your needs, if not, you can bind it not to long property, but to string property and handle conversion from long -> srtring and vice versa "by hand".
for convert it into formatted string use pretty simple example:
long l =13;
string sformat = l.ToString("000000"); // 000013
Regards.
Yes, follow link
How to: Pad a Number with Leading Zeros

Use of MaskedTextBox with text

I'm facing a simple problem that bugs me...
I have a MaskedTextBox and I want the user to be able to enter 3 numbers at the end :
"My Masked Text Box : XXX"
This text will be translated. The problem is, this control uses Microsoft's recipe to validate the input and in this example, the final display will look like this :
"My M_sked Text Box : _"
The letter 'a' is considered like a control caracter instead of a simple text caracter. I can backslash it but when the text is translated I have to do it again and I think it's ridiculous to have to do something like that...
I hope I'm being clear...
Thanks in advance for your help !
The 'a' needs to be quoted as a literal. The Mask should be something like:
My M\asked Text Box : 000
You should use '9' rather than '0' as the placeholder for an optional numerical character.
Of course any other of the characters that match mask options should be 'literal' too.
http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask.aspx
After reading the comments I'd like to add another suggestion (you clearly understood about literals -- the problem is your translation.)
Would it be feasible to run the translated text strings through a filter that put literal-quotes in where possible? It's not that nice a solution, because if Microsoft added new control characters it'll break your filter, but I think it would work.

Celsius symbol in RichTextBox

I write windows application using C# and .NET2.0.
In RichTextBox I would like to show Celsius symbol.
How to do it? Is it possible?
Do you mean Celsius symbol as in 37°C? If so you can simply put that character where it should be, I guess:
richTextBox.Text = string.Format("{0}°C", degrees);
If you are looking for character codes (or just want to find character to copy/paste them), you can use the Character Map application in Windows (in Start -> Programs -> Accessories -> System Tools).
Do you mean °C? You get ° from the keyboard as ALT + 0176 on the numeric keypad.
If you are wary of embedding a non-ASCII character in your source code, you could use the following instead:
richTextBox.Text = string.Format("{0}\u00B0C", degrees);
(B0 is the hexadecimal for 176.)
richTextBox1.Text = "°" will display a degree symbol in a rich textbox but I'm pretty sure you want something else. Please rephrase your question if that's the case.
° //html entity.

Categories