Use of MaskedTextBox with text - c#

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.

Related

Displaying phone format XAML/c#

In my windows phone project, I would like the user to enter his phone number in xxx-xxx-xxxx format. The country code it not required. I tried to implement regex, but i am not getting it right. I just want it to be displayed to the user as he enters it, nothing more, nothing less. This is what I have used
^\(\d{3}\) ?\d{3}( |-)?\d{4}$
But no matter what i put in, I always get this error (in this case 5) "Unrecognized escape sequence". I noticed, this is only with reference to the oblique. When I add a "" after it, the error goes away, but I do not get what I want. Is there a special way to input numbers in the textbox in than manner, on the XAML level?
Thanks in advance!
Put your regex inside verbatim string and also put the space, hyphen inside a group and make it as optional.
#"^\(\d{3}\)([- ]?)\d{3}\1\d{4}$"
DEMO
For testing your RegEx you can use this site: http://www.regexlib.com/RETester.aspx?AspxAutoDetectCookieSupport=1.
For your xxx-xxx-xxxx format I would use it:^\d{3}-?\d{3}-?\d{4}$

StatusStrip Labels Text are mirrored [duplicate]

I am using a StringBuilder in C# to append some text, which can be English (left to right) or Arabic (right to left)
stringBuilder.Append("(");
stringBuilder.Append(text);
stringBuilder.Append(") ");
stringBuilder.Append(text);
If text = "A", then output is "(A) A"
But if text = "بتث", then output is "(بتث) بتث"
Any ideas?
This is a well-known flaw in the Windows text rendering engine when asked to render Right-To-Left text, Arabic or Hebrew. It has a difficult problem to solve, people often fall back to Western words and punctuation when there is no good alternative word available in the language. Brand and company names for example. The renderer tries to guess at the proper render order by looking at the code points, with characters in the Latin character set clearly having to be rendered left-to-right.
But it fumbles at punctuation, with brackets being the most visible. You have to be explicit about it so it knows what to do, you must use the Unicode Right-to-left mark, U+200F or \u200f in C# code. Conversely, use the Left-to-right mark if you know you need LTR rendering, U+200E.
Use AppendFormat instead of just Append:
stringBuilder.AppendFormat("({0}) {0}", text)
This may fix the issue, but it may - you need to look at the text value - it probably has LTR/RTL markers characters embedded. These need to either be removed or corrected in the value.
I had a similar issue and I managed to solve it by creating a function that checks each Char in Unicode. If it is from page FE then I add 202C after it as shown below. Without this it gets RTL and LTF mixed for what I wanted.
string us = string.Format("\uFE9E\u202C\uFE98\u202C\uFEB8\u202C\uFEC6\u202C\uFEEB\u202C\u0020\u0660\u0662\u0664\u0668 Aa1");

System.Windows.Forms.ErrorProvider - displaying "&" character

I'm trying to set an error message which contains an ampersand via an ErrorProvider.
First attempt went like this :
errorProvider.SetError(someControl, "You have not accepted the Terms & Conditions");
This does not display the ampersand character. Googling suggested the following :
errorProvider.SetError(someControl, "You have not accepted the Terms &&& Conditions");
It works (i.e. displays one ampersand), but I would like to understand why it works. Any ideas ?
EDIT : for the System.Windows.Forms.Label control, there is a property called : UseMnemonic which can be set to False. Does ErrorProvider have anything like this ?
“&” is a special character in forms that is meant to underline the following character. So if you use it like:
myButton.Text = "&Close";
it will underline the 'C' character which will also become a hotkey. If you don't want that to happen, you use double ampersand like '&&' in your text.

Optimistic RegEx Matching for User Text Entry

I'm working on a text entry application that uses regular expressions to validate user input. The goal is to allow keypresses that fit a certain RegEx while rejecting invalid characters. One issue I've run into is that when a user starts inputting information they may create a string that doesn't yet match the given regex, but could cause a match in the future. These strings get erroneously rejected. Here's an example - given the following regex for inputting date information:
(0?[1-9]|10|11|12)/(0?[1-9]|[12]\\d|30|31)/\\d{2}\\d{2}
A user may begin entering "1/" which could be a valid date, but RegEx.IsMatch() will return false and my code ends up rejecting the string. Is there a way to "optimistically" test strings against a regular expression so that possible or partial matches are allowed?
Bonus: For this RegEx in particular there are some sequences which cause required characters. For example, if the user types "2/15" the only possible valid character they could enter next is "/". Is it possible to detect those scenarios so that the required characters could be automatically entered for the user to ease input?
What you can do is anchor your RegExp (i.e. adding ^ and $, as in start/end of line) and make some component optionnal for validation, but strictly defined if present.
Something looking like this:
^(0?[1-9]|10|11|12)(/((0?[1-9]|[12]\\d|30|31)(/(\\d{2}(\\d{2})?)?)?)?)?$
I do realize it looks horrible but as far as I know there is no way to tell the regexp engine to validate as long as the string satisfies the beginning of the regexp pattern.
In my opinion, the best way to achieve what you want to do is to create separate inputs for day/month/date and check their value when leaving the text field.
It also provides a better visibility and user-experience, as I believe no one likes to be prevented from typing certain characters into a text field with or without noticing them disappear as they type or having slashes inserted automatically and without notice.
Have you ever used and app or form that worked that way, simply refusing to accept any keypress it didn't like? If the answer is Yes, did it blow an electronic raspberry each time you pressed a wrong key?
If you really need to validate the input before the form is submitted, use a passive feedback mechanism like a red border around the textfield that disappears the regex matches the input. Also, make sure there's a Help button or a tooltip nearby to provide constructive feedback.
Of course, the best option would be to use a dedicated control like a date-entry widget. But whatever you do, don't do it in such a a way that it feels like you're playing guessing games with the user.

Getting values from Masked text box

I've set MaskedTextBox's Mask to: "Fl\air H\al ###.## , something here: ####.##"
When user inputs the value final text looks something like this:
Flair Hal 987.67 , something here: 1234.12
What will be the best way to extract 976.67 and 1234.12 from the MaskedTextBox's Text. I am looking for a List which will have all the values of the mask (976.67, 1234.12).
There can be any number of masks in the mask string and the mask can be any valid mask.
I am thinking of first removing '\' from the Mask and then in a for loop keep comparing the Mask with the Text and detect changes and add them to the List. But this doesnt sound good to me and i think there probably is a better way of doing it.
There are four values of the mask in your example: 987, 67, 1234, 12. The fact that blocks separated by a . are treated as one is your own logic, so I think you will just have to write code to get the information yourself.
Have a look at the MaskedTextProvider property of the MaskedTextBox, and its EditPositions property. The EditPositions give you the positions within the Text that the user could enter.
Well i found out that there is no good way of doing it. As adrianbanks said i have to code myself to get this information.
I have written my own usercontrol which uses combination of labels and maskedtexboxes to get the input.
I use curly braces to indicate where i wan the masked textbox and the user control puts one masked textbox per pair of curly braces.
"Flair Hal {###.##} , something here: {####.##}"
Then I can use the values collection which has the values for the masks.
I would think that you would be to use TextMaskFormat to remove the literals and prompt characters from the .Text property. That way you'd only get the numbers (and spaces).
Use a regular expression. The following regex will work for your example, but you may have to tweak it depending on your actual mask:
\d*[.]\d{2}

Categories