Devexpress RegEx Mask with different Displayformat doesn't work - c#

I have a Textedit Control that I want to behave in a certain way:
When the Control has input focus, I want to only allow the input of positive whole Numbers (not zero). I achieve that by using Properties.Mask, which works fine.
When the Control does NOT have input focus, I want it to Display the entered number but with ",00" at the end.
So basically, while I enter something, I only see what I enter e.g. "17" but when the Control loses focus, I want it to show "17,00". So that I am only allowed to enter whole positive numbers but the Control will always add ",00" afterwards.
My understanding is that there are basically two different "modes": DisplayMode and EditMode.
EditMode = The Control has focus.
DisplayMode = The Control does not have focus.
In EditMode, I can type things into my Textedit Control. What I can and can not enter is determined by the Mask.
When I lose focus, it goes into DisplayMode. Here I cant type anything into the TextEdit but now the displayed text is not determined by the mask any more but by the Property "Properties.DisplayFormat". So to achieve my goal, I tried to set the DisplayFormat.FormatString to "0.00", so that it would always show two decimal places "x,00".
Somehow, this doesn't work as expected. The DisplayFormat doesn't seem to do anything and even in DisplayMode, the TextEdit still shows just the whole number without the decimal places.
I realize that I could use events to work around this problem but I think that's what DisplayFormat, EditFormat and Mask are for and I really don't want to handle multiple events for something that small.

Accodring to DevExpress Knowledge Base topic DisplayFormat is not working in unbound mode.
Problems with formatting occurs because an unbound text editor stores
a value as a string, therefore formatting cannot be applied.
If you use XtraEditors 3 or higher, you may wish to set the editor's
Mask.MaskType property to Numeric. In this case, the editor is forced
to handle the edit value as a number and, therefore, it can format it.
If you wish not to use the Numeric (or DateTime) mask, please use the
ParseEditValue event to convert a string to a number.
I can suggest you to use Numeric mask with n0 as edit mask:

To accomplish the above is fairly simple:
To only allow positive whole numbers, you need to set the MaskType to Numeric and use the EditMask ##########;. The number of # represents the possible number of didgets so ten times # means you can use a ten-digit number. (see nempoBu4's answer)
To show an additional ,00 when the control loses the focus, you simply need to set the DisplayFormat as FormatType = Numeric and FormatString = n2.

Related

Label displays punctuation incorrectly with RightToLeft

I'm trying to learn C# and .NET by creating a calculator app. However, I'm seeing some weird behavior with WinForms label and punctuation. My app has a series of number buttons, a "period" button for decimals, and various operators. When you press a button, I add the value to the label that is displaying the value:
displayLbl.Text += selectedButton.Text;
or
displayLbl.Text += ".";
The label has RightToLeft set to "true" to mimic the display of a typical calculator.
However, when a period first appears in the label, it appears ahead of the rest of the numbers that were added before it. For example, it will look like ".456" even though the "456" was added earlier. As soon as you add another number, the period will then appear back in its right place like "456.7".
This also happens with the negative sign (-). If you add "-478" to the label, it will appear as "478-".
This seems really buggy. Is there any way to fix this?
I set RightToLeft to "No" and then did the following and it worked beautifully:
this.displayLbl.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

WPF TextBox reacting weirdly with stringformat 0:N2

This is hard to explain so please look at the WPF gif below -
I have a textbox with a stringformat {0:N2} and bound to a property.
There are two problems here -
(1) When hitting the decimal key, another decimal is added.
(2) During backspace, after deleting the decimal digits - the caret doesn't jump over the decimal rather starts adding '00' to the number itself.
I did a workaround for point (1) -
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Decimal)
{
e.Handled = true;
((TextBox)sender).CaretIndex += 1;
}
}
If there is a better way to handle such things in application - please do let me know.
Now, how do I handle the second problem? The workaround Im trying to implement is - if decimal point reached, handle the key press and move the caret position. Not sure if this is a good solution and if so, how to know when I have reached the decimal point?
Comparing the caret index to the length of the textbox value just to know if it has reached the decimal point seems like overdoing it. What is the correct solution to these 2 problems?
The problem is due to when the stringformat is being applied to the value.
Which is as the value comes back from the binding Source ( the bound string property ) to the TextBox Text ( the Target ).
And it's linked to metadata of textpropety.
The Textbox TextProperty binds twoway, this is set in the metadata of that dependency property.
The default behaviour of a textbox is to copy the value to the bound source when the control loses focus.
If you use that then the n2 format is applied once the user has finished editing. They may fix up whatever they like with the zeroes and decimal point and the format only gets applied when they tab away.
By contrast, if you set UpdateSourceTrigger="PropertyChanged" in your binding then you will find it behaves weirdly. This is because the user types a letter, the whole value is transferred to the bound property which notifies the control it changed.
All sorts of weird bad things happen as the formatting is being applied.
It's possible that this is an edge case whoever built the textbox didn't consider.

Use images in textbox

I am creating a control that displays a hex value (For example FFAA-CA10-56D7) . Right now the value consists of images. The value of the control looks like:
For a read only control I have no problems. I am able to place the appropriate images according to the value. So if I have to set the value to FF for example I will iterate through the characters on the string and then place the corresponding image.
Now the problem is with read-write controls. It will be nice if I could use the same font. Right now I append images as the user types values. But it is not friendly. What if the user wants to select 3 characters by dragging the mouse?. Or how could I perform the shift+arrow key combination that will select multiple characters.
So in short I will like to keep the same look and feel of the control and I do not want to replace the images for a textbox. What approach do you guys recommend so that I can keep the same look and feel.
Things I have tried:
I tried to create my custom font from those png's. But haven't been successful on finding the right program that will enable me to create a font from png images.
I would do this by using a custom ListBox with a Horizontal StackPanel as its ItemPanelTemplate, and its ItemsSource would be a list of char. then you could provide an appropiate DataTemplate containing the corresponding images (dependending on char value, maybe using DataTriggers). ListBox has Multiselection, so you should have no problem in imitating the Text Selection behavior of a TextBox, and by having a List of char as the ItemsSouce you can also allow Copying (by doing your own implementation of EditCommands.Copy in your ListBox.
Edit: This is one of those cases in which you will definitely want to implement your own Custom Control

WPF C# Text box with hour string format

How to make that user can only input hour format in text box for example HH:MM?
Depending on strictness of requirements you may want to create a specific behaviour that will be attached to KeyDown event and disable keys other than currently accepted given format and current text input position in (HH:MM), you could even think of different ways to guarantee automatic : input by your behaviour if you want to be limited by textbox control for time input.
you can get some inspiration here I think
numeric only behaviour example
Look at Binding.StringFormat. It allows for conversion to DateTime or TimeSpan while specifying a certain string format to define how the user inputs the value, and how the value is displayed.
It sounds like you are looking for a masked text box.
You can fine one on codeplex here...
WPF Toolkit
EDIT.
There is also a datetimeupdown control. Which might be nearer to what you want.

Validation to have only numeric data in a textbox in a window form

I want that a user only enters numeric data into the textbox in a windows form. How can I achieve this?
The easiest way would be to use a MaskedTextBox (use the Mask property) or a NumericUpDown control. If you really need fine-grained control that these controls do not provide, handle the KeyPress and other appropriate events of the TextBox control as required.
EDIT: Clarified that KeyPress is not the only relevant event, as mentioned by ho1.
What do you want to happen when the user attempts to enter a non-numeric value? Does this matter before a data submission attempt? Without entering your code block every time a key is pressed, I think it'd be cleaner to just let the user enter non-numerics and validate either when the form is submitted and/or when the text box loses focus. Then if validation fails, notify the user of the strict numeric format. This would be less invasive to a user, rather than interrupting.
As for validation, just use either regular expressions or try to parse the text box's text as an integer.
Regular Expression
System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), #"^\d$");
Integer Parse
int validNumbers = 0;
bool isValid = int.TryParse(myTextBox.Text, out validNumbers);
First it depends on what kind of numeric data you want to allow. (Integer, Double, ..., or something app-specific? Like an ISBN or something?)
Short:
The easiest way would be like Ani said, but if you need a more specific way, you should subcribe an matching Event. For Example TextBox.Validating, TextBox.OnLeave, TextBox.OnTextChange... depends on when you want to test the matching.
Than you can test in the Eventhandler whatever you want, even RegEx would be possible for complex alphanumeric data.
PS: You should really have a look at http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx

Categories

Resources