if (!(char.IsDigit(e.KeyChar)))
{
e.Handled = true;
}
The above code is not working properly
Below is the image error :
The problem space is "Clipboard"
If this is for WinForms, my suggestion would be to use a MaskedTextBox instead. This is a purpose-built control for allowing only certain kinds of user-input.
You can set the mask through the designer or in code.
For example, for a 5-digit numeric:
maskedTextBox1.Mask = "00000";
maskedTextBox1.ValidatingType = typeof(int);
Yes, this is the typical nemesis for keyboard filtering. The TextBox control doesn't have any built-in events to intercept a paste from the clipboard. You'll have to detect the Ctrl+V keypress yourself and screen Clipboard.GetText().
The logic is tricky to get right. Here's a class that can make all this a little easier. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto a form. Double click it and write the ValidateChar event handler. Like this one, only allowing entering digits:
private void validatingTextBox1_ValidateChar(object sender, ValidateCharArgs e) {
if (!"0123456789".Contains(e.KeyChar)) e.Cancel = true;
}
The code:
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text;
[DefaultEvent("ValidateChar")]
class ValidatingTextBox : TextBox {
public event EventHandler<ValidateCharArgs> ValidateChar;
protected virtual void OnValidateChar(ValidateCharArgs e) {
var handler = ValidateChar;
if (handler != null) handler(this, e);
}
protected override void OnKeyPress(KeyPressEventArgs e) {
if (e.KeyChar >= ' ') { // Allow the control keys to work as normal
var args = new ValidateCharArgs(e.KeyChar);
OnValidateChar(args);
if (args.Cancel) {
e.Handled = true;
return;
}
}
base.OnKeyPress(e);
}
private void HandlePaste() {
if (!Clipboard.ContainsText()) return;
string text = Clipboard.GetText();
var toPaste = new StringBuilder(text.Length);
foreach (char ch in text.ToCharArray()) {
var args = new ValidateCharArgs(ch);
OnValidateChar(args);
if (!args.Cancel) toPaste.Append(ch);
}
if (toPaste.Length != 0) {
Clipboard.SetText(toPaste.ToString());
this.Paste();
}
}
bool pasting;
protected override void WndProc(ref Message m) {
if (m.Msg == 0x302 && !pasting) {
pasting = true;
HandlePaste();
pasting = false;
}
else base.WndProc(ref m);
}
}
class ValidateCharArgs : EventArgs {
public ValidateCharArgs(char ch) { Cancel = false; KeyChar = ch; }
public bool Cancel { get; set; }
public char KeyChar { get; set; }
}
Handle TextChanged event or use a MaskedTextBox.
if (textBox1.Text.Count(a => !char.IsDigit(a)) > 0)
{
textBox1.Text = new string(textBox1.Text.Where(a => char.IsDigit(a)).ToArray());
}
I answered a similar question on StackOverflow once.
Here's the link to the question: Best way to limit textbox decimal input in c#
Essentially, you'll have to put my class in your code and apply it to all textboxes you want to restrict data entered.
The TextBoxFilter class I wrote allows you to limit entry to Alphabet, Numerics, AlphaNumerics, Currency and UserSpecified input.
control.TextChanged += (s, a) => {
string value = string.Empty;
foreach (char ch in control.Text.ToCharArray())
{
if (char.IsDigit(ch))
{
value += ch.ToString();
}
}
control.Text = value;
};
Related
Entry.unfocus/Entry.completed hides keyboard, how to cancel it?
I have a page with some entries and when I press keyboard enter key, I want the keyboard not hides. How to do that with PCL project (Android e iOS)?
Just to point out another solution for Android. In case you want to keep always visible the keyboard for a specific Editor Renderer, you need to override the following methods in the MainActivity class:
private bool _lieAboutCurrentFocus;
public override bool DispatchTouchEvent(MotionEvent ev)
{
var focused = CurrentFocus;
bool customEntryRendererFocused = focused != null && focused.Parent is YourCustomEditorRenderer;
_lieAboutCurrentFocus = customEntryRendererFocused;
var result = base.DispatchTouchEvent(ev);
_lieAboutCurrentFocus = false;
return result;
}
public override Android.Views.View CurrentFocus
{
get
{
if (_lieAboutCurrentFocus)
{
return null;
}
return base.CurrentFocus;
}
}
You can find a more detail explanation here
Hope this helps.
Regards
If you want to do that from the PCL there's a nice and easy way to navigate through your entries and keep them focused one after the other (If this is what you're looking for, and not just keep keyboard open)
Let's say you have around 5 entries in your page, and you want to cycle through them when user presses the done or enter key.
CurrentPage.FindByName<Entry>("FirstEntry").Completed += (o, args) =>
{
CurrentPage.FindByName<Entry>("SecondEntry").Focus();
};
CurrentPage.FindByName<Entry>("SecondEntry").Completed += (o, args) =>
{
CurrentPage.FindByName<Entry>("ThirdEntry").Focus();
};
CurrentPage.FindByName<Entry>("ThirdEntry").Completed += (o, args) =>
{
CurrentPage.FindByName<Entry>("ForthEntry").Focus();
};
CurrentPage.FindByName<Entry>("ForthEntry").Completed += (o, args) =>
{
CurrentPage.FindByName<Entry>("FifthEntry").Focus();
};
CurrentPage.FindByName<Entry>("FifthEntry").Completed += (o, args) =>
{
//Keep going or execute your command, you got the drill..
};
You can add this to your ViewIsAppearing or Init method.
Recently i did something similar. I want to keep keyboard always open in a page and not to hide when a button clicked. To accomplish this, i followed different ways both on iOS and Android.
iOS
In iOS, i created a custom editor renderer
public class CustomEditorRenderer : EditorRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
base.OnElementChanged(e);
var element = this.Element as CustomEditor;
Control.InputAccessoryView = null;
Control.ShouldEndEditing += DisableHidingKeyboard;
MessagingCenter.Subscribe<ReportEventDetailPage>(this, "FocusKeyboardStatus", (sender) =>
{
if (Control != null)
{
Control.ShouldEndEditing += EnableHidingKeyboard;
}
MessagingCenter.Unsubscribe<ReportEventDetailPage>(this, "FocusKeyboardStatus");
});
}
private bool DisableHidingKeyboard(UITextView textView)
{
return false;
}
private bool EnableHidingKeyboard(UITextView textView)
{
return true;
}
}
In this piece of code:
Control.ShouldEndEditing += DisableHidingKeyboard; makes keyboard always opened after focusing custom editor. However, the keyboard does not hide when changing current page to another page. To solve this problem i used MessagingCenter and when dissapering of the current page i send a message to hide keyboard.
Android
For Android, i created a keyboard helper interface and implemented it.
Here is my interface:
public interface IKeyboardHelper
{
void ShowKeyboard();
void HideKeyboard();
}
Keyboard Helper class for Android:
public class KeyboardHelper : IKeyboardHelper
{
public void ShowKeyboard()
{
var context = Forms.Context;
var inputMethodManager = context.GetSystemService(Context.InputMethodService) as InputMethodManager;
if (inputMethodManager != null && context is Activity)
{
var activity = context as Activity;
var token = activity.CurrentFocus?.WindowToken;
inputMethodManager.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
}
}
public void HideKeyboard()
{
var context = Forms.Context;
var inputMethodManager = context.GetSystemService(Context.InputMethodService) as InputMethodManager;
if (inputMethodManager != null && context is Activity)
{
var activity = context as Activity;
var token = activity.CurrentFocus?.WindowToken;
inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);
activity.Window.DecorView.ClearFocus();
}
}
in Constructor of the current page:
else if (Device.OS == TargetPlatform.Android)
{
MessagingCenter.Send(this, "AndroidFocusEditor");
}
and OnAppearing method of the current page:
protected override void OnAppearing()
{
base.OnAppearing();
if (Device.OS == TargetPlatform.Android)
{
DependencyService.Get<IKeyboardHelper>().ShowKeyboard();
//EventEditor.Focus();
MessagingCenter.Subscribe<ReportEventDetailPage>(this, "AndroidFocusEditor", (sender) => {
Device.BeginInvokeOnMainThread(async () => {
await Task.Run(() => Task.Delay(1));
EventEditor.Focus();
MessagingCenter.Unsubscribe<ReportEventDetailPage>(this, "AndroidFocusEditor");
});
});
}
else if (Device.OS == TargetPlatform.iOS)
{
EventEditor.Focus();
}
}
One last thing: if user clicks another button on the page, keyboard is hiding. To prevent this i followed this link and it really helped me a lot
Keep Keyboard Open For Android
In case you have a custom Keyboard, you can implement a "show" and a "hide" method on android renderer.
Then on your page, show keyboard on your custom control without hiding it. You can hide it when changing page, by overriding OnBackButtonPressed.
In OnBackButtonPressed, send a message using MessagingCenter. Then subscribe to it on your custom control constructor.
Declare an EventHandler that you invoke in the callback method.
Subscribe to this event on your android custom entry renderer and hide the keyboard there.
I had a similar problem and handled it like below:
using System.Runtime.CompilerServices;
using Xamarin.Forms;
public class CustomEntry: Entry
{
public static readonly BindableProperty KeyboardAliveProperty =
BindableProperty.Create(nameof(KeyboardAliveType), typeof(KeyboardAliveType),
typeof(CustomEntry), KeyboardAliveType.Default);
public KeyboardAliveType KeyboardAliveType
{
get { return (KeyboardAliveType)GetValue(KeyboardAliveProperty); }
set { SetValue( KeyboardAliveProperty, value);}
}
}
public enum KeyboardAliveType
{
Default =0,
OnCompleted = 1,
OnButtonClicked = 2,
OnCompletedAndButtonClicked = 3
}
Renderer for Android:
using System;
using Android.Content;
using Android.OS;
using Android.Views;
using Android.Views.InputMethods;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Entry = Xamarin.Forms.Entry;
[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
/// <summary>
/// Allow and support changes to Border styling and Keyboard with Custom Entry.
/// </summary>
public class CustomEntryRenderer: EntryRenderer, TextView.IOnEditorActionListener
{
private ImeAction _currentInputImeFlag;
public CustomEntryRenderer(Context context) : base(context)
{
//do nothiing
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (e.NewElement!=null)
{
}
}
bool TextView.IOnEditorActionListener.OnEditorAction(TextView v, ImeAction actionId, KeyEvent e)
{
// Fire Completed and dismiss keyboard for hardware / physical keyboards
if (actionId == ImeAction.Done || actionId == _currentInputImeFlag ||
(actionId == ImeAction.ImeNull && e.KeyCode == Keycode.Enter && e.Action == KeyEventActions.Up))
{
global::Android.Views.View nextFocus = null;
if (_currentInputImeFlag == ImeAction.Next)
{
nextFocus = FocusSearch(v, FocusSearchDirection.Forward);
}
if (nextFocus != null)
{
nextFocus.RequestFocus();
if (!nextFocus.OnCheckIsTextEditor())
{
if (Element is CustomEntry cE)
{
if (cE.KeyboardAliveType != KeyboardAliveType.OnCompleted &&
cE.KeyboardAliveType != KeyboardAliveType.OnCompletedAndButtonClicked)
{
v.HideKeyboard();
}
}
}
}
else
{
EditText.ClearFocus();
if (Element is CustomEntry cE)
{
if (cE.KeyboardAliveType != KeyboardAliveType.OnCompleted &&
cE.KeyboardAliveType != KeyboardAliveType.OnCompletedAndButtonClicked)
{
v.HideKeyboard();
}
}
}
((IEntryController)Element).SendCompleted();
}
return true;
}
}
internal static class CustomEntryRendererExtensions
{
internal static void HideKeyboard(this Android.Views.View inputView, bool overrideValidation = false)
{
if (inputView == null)
throw new ArgumentNullException(nameof(inputView) + " must be set before the keyboard can be hidden.");
using (var inputMethodManager = (InputMethodManager)inputView.Context?.GetSystemService(Context.InputMethodService))
{
if (!overrideValidation && !(inputView is EditText || inputView is TextView || inputView is SearchView))
throw new ArgumentException("inputView should be of type EditText, SearchView, or TextView");
IBinder windowToken = inputView.WindowToken;
if (windowToken != null && inputMethodManager != null)
inputMethodManager.HideSoftInputFromWindow(windowToken, HideSoftInputFlags.None);
}
}
}
In MainActivity.cs
private bool _lieAboutCurrentFocus;
public override bool DispatchTouchEvent(MotionEvent ev)
{
var focused = CurrentFocus;
if (focused?.Parent is CustomEntryRenderer cer)
{
if (cer.Element is CustomEntry cEntry)
{
if (cEntry.KeyboardAliveType == KeyboardAliveType.OnButtonClicked ||
cEntry.KeyboardAliveType == KeyboardAliveType.OnCompletedAndButtonClicked)
{
_lieAboutCurrentFocus = true;
}
}
}
var result = base.DispatchTouchEvent(ev);
_lieAboutCurrentFocus = false;
return result;
}
public override Android.Views.View CurrentFocus
{
get
{
if (_lieAboutCurrentFocus)
{
return null;
}
return base.CurrentFocus;
}
}
Renderer for UWP:
using System;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.System;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Xamarin.Forms;
using Xamarin.Forms.Platform.UWP;
[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
public class CustomEntryRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
// Remove the EventHandler set for KeyUp, and add my custom EventHandler.
// Had to do it this way (using WindowsRuntimeMarshal) because the Delegate that
// I want to remove from the KeyUp event is marked private in a different assembly, so no way to access it directly.
// This way I can customize how the keyboard behaves when the Enter key is pressed.
/*Done the best I can for UWP.*/
var keyUpRuntimeEvent = this.Control.GetType().GetRuntimeEvent("KeyUp");
Action<EventRegistrationToken> removeEventHandlerAction =
(Action<EventRegistrationToken>)Delegate.CreateDelegate(typeof(Action<EventRegistrationToken>),
this.Control,
keyUpRuntimeEvent.RemoveMethod);
WindowsRuntimeMarshal.RemoveAllEventHandlers(removeEventHandlerAction);
this.Control.KeyUp += TextBoxOnKeyUp;
this.Control.PreventKeyboardDisplayOnProgrammaticFocus = false;
// Just to make sure that keyboard is up when the Entry is focused.
Control.GotFocus += (sender, args) =>
{
AttemptToForceKeyboardToShow(Control);
};
Control.TextChanged += (sender, args) =>
{
if (Control.FocusState != FocusState.Unfocused)
{
AttemptToForceKeyboardToShow(Control);
}
};
}
}
protected override void Dispose(bool disposing)
{
if (disposing && Control != null)
{
Control.KeyUp -= TextBoxOnKeyUp;
}
base.Dispose(disposing);
}
private void TextBoxOnKeyUp(object sender, KeyRoutedEventArgs args)
{
if (args?.Key != VirtualKey.Enter)
{
return;
}
if (Element.ReturnType == ReturnType.Next)
{
FocusManager.TryMoveFocus(FocusNavigationDirection.Next);
}
else
{
/*Done the best I can for UWP.*/
if (Element is CustomEntry cE)
{
if (cE.KeyboardAliveType != KeyboardAliveType.OnCompleted &&
cE.KeyboardAliveType != KeyboardAliveType.OnCompletedAndButtonClicked)
{
//Hide the soft keyboard; this matches the behavior of Forms on Android/iOS
Windows.UI.ViewManagement.InputPane.GetForCurrentView().TryHide();
}
}
}
((IEntryController)Element).SendCompleted();
}
private void AttemptToForceKeyboardToShow(FormsTextBox control)
{
try
{
var inputPane = InputPane.GetForUIContext(control.UIContext);
var keyboardShowSuccess = inputPane?.TryShow();
if (keyboardShowSuccess == null || !keyboardShowSuccess.Value)
{
Console.WriteLine("Attempt to force Keyboard to show failed on Windows.");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
See here.
I have a RootPage as root view. In my RootPage I've added this to support the back button:
SystemNavigationManager.GetForCurrentView().BackRequested += SystemNavigationManager_BackRequested;
private void SystemNavigationManager_BackRequested(object sender, BackRequestedEventArgs e) {
bool handled = e.Handled;
if (this.AppFrame == null)
return;
if (this.AppFrame.CanGoBack && !handled) {
// If not, set the event to handled and go back to the previous page in the app.
handled = true;
this.AppFrame.GoBack();
}
e.Handled = handled;
}
This works as expected (the user goes to the previous page if there is one). But when I enable caching for one page it doesn't work anymore:
this.NavigationCacheMode = NavigationCacheMode.Enabled;
I've also tried Required but that gives the same result.
When I press the back button now it just closes the app instead of going back. The code for going back is executed though, so I don't know what I'm doing wrong.
Update
I've located the exception. I have a custom TextBox which fills the Text with a currency symbol and a space. This is essentially what creates the exception:
this.Loaded += (sender, args) => {
Text = "$ ";
};
Note: it is not the currency symbol that breaks the app, but the fact that I set the Text in Loaded when returning to this Page (page cache should handle this now, so that's conflicting I guess)
Update 2
Code sample to reproduce problem. Custom TextBox:
public class RegexTextBox : TextBox {
private string regex = "";
private string previousText = "";
private int previousSelectionStart = 0;
public RegexTextBox() {
this.Loaded += (sender, args) => {
switch(RegexType) {
case RegexTextBoxTypes.Number:
regex = "^([0-9]+|)$";
break;
case RegexTextBoxTypes.Currency:
regex = "^€ ([0-9]+|)$";
Text = "€ ";
break;
}
this.TextChanging += input_TextChanging;
this.SelectionChanged += input_SelectionChanged;
previousText = Text;
previousSelectionStart = SelectionStart;
};
}
private void input_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args) {
if (regex.Length > 0) {
if (!Regex.IsMatch(Text, regex)) {
Text = previousText;
SelectionStart = previousSelectionStart;
}
previousText = Text;
}
}
private void input_SelectionChanged(object sender, RoutedEventArgs e) {
previousSelectionStart = SelectionStart;
}
public bool isEmpty() {
if(RegexType == RegexTextBoxTypes.Currency) {
return !Regex.IsMatch(Text, "^€ ([0-9]+)$");
}
return Text.Length == 0;
}
public void ClearText() {
if(RegexType == RegexTextBoxTypes.Currency) {
Text = "€ ";
} else {
Text = "";
}
}
public string GetPlainText() {
if (RegexType == RegexTextBoxTypes.Currency) {
return Text.Substring(2, Text.Length - 2);
}
return Text;
}
public RegexTextBoxTypes RegexType {
get { return (RegexTextBoxTypes)GetValue(RegexTypeProperty); }
set { SetValue(RegexTypeProperty, value); }
}
public static readonly DependencyProperty RegexTypeProperty =
DependencyProperty.Register("RegexType", typeof(RegexTextBoxTypes), typeof(RegexTextBox), new PropertyMetadata(RegexTextBoxTypes.Normal));
}
public enum RegexTextBoxTypes {
Normal,
Number,
Currency
}
In your xaml put this in your grid (controls namespace points to the location of the custom TextBox):
<controls:RegexTextBox
InputScope="Number"
RegexType="Currency"/>
Then in the constructor of the page put this:
this.NavigationCacheMode = NavigationCacheMode.Required;
Make sure the custom TextBox is filled with some numbers. Then navigate to a new page and back, when navigating back it should close the app due to an uncaught exception (though visual studio won't show there has been an exception).
I want create a custom control for Numeric Text box with dependency property in WPF , in my solution , I add one WPF application and custom control (WPF) ,then in public class , I create dependency property ....
Now I don't know how can i write my rule for text box and which event is true?
Another question : What is my rule for numeric text box , that this text box must be give number and . and Separating .this custom Text box is for accounting system.
public static readonly DependencyProperty NumberTextbox;
static Numeric()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Numeric), new FrameworkPropertyMetadata(typeof(Numeric)));
FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata("Enter Your Text", OnKeyDown);
NumberTextbox =DependencyProperty.Register("Text", typeof(TextBox), typeof(FrameworkElement), metadata);
}
public string NumberTXT
{
get { return (string)GetValue(NumberTextbox); }
set { SetValue(NumberTextbox, value); }
}
I recommend to you add another Dependency Property in example code below I named it Value
Also format your number by comma or NumberFormatInfo.CurrentInfo.NumberDecimalSeparator
and control caret location changes by two property SelectionLength and SelectionStart.
Finally for more detail and complete code WPF Maskable Number Entry TextBox
region Value property
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(double), typeof(NumericTextBox), new PropertyMetadata(new Double(), OnValueChanged));
private static void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
//var numericBoxControl = (NumericTextBox)sender;
}
public double Value
{
get { return (double)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); Text = value.ToString("###,###,###"); }
}
endregion
protected override void OnPreviewTextInput(TextCompositionEventArgs e)
{
base.OnPreviewTextInput(e);
var txt = e.Text.Replace(",", "");
e.Handled = !IsTextAllowed(txt);
if (IsTextAllowed(txt))
{
if (Text.Length == 3)
{
Text = Text.Insert(1,",");
SelectionLength = 1;
SelectionStart += Text.Length;
}
}
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
base.OnPreviewKeyDown(e);
if (e.Key == Key.Back)
{
if (Text.Length == 5)
{
Text = Text.Replace(",", "");
SelectionLength = 1;
SelectionStart += Text.Length;
}
}
}
protected override void OnTextChanged(TextChangedEventArgs e)
{
var txt = Text.Replace(",", "");
SetValue(ValueProperty, txt.Length==0?0:double.Parse(txt));
base.OnTextChanged(e);
}
private static bool IsTextAllowed(string text)
{
try
{
double.Parse(text);
return true;
}
catch (FormatException)
{
return false;
}
}
I don't understand your question exactly and why you need dependency proerties to make a numeric text box custom control. What you can do is to inherit from textbox and handle the PreviewTextInput, like it is solved in this question by Ray:
then you get:
public class NumericTextBox : TextBox
{
public NumericTextBox()
{
PreviewTextInput += NumericTextBox_PreviewTextInput;
}
void NumericTextBox_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
e.Handled = !isTextAllowed(e.Text);
}
private static bool isTextAllowed(string text)
{
var regex = new Regex("[^0-9]+");
return !regex.IsMatch(text);
}
}
And you can use it like that:
<myNameSpace:NumericTextBox />
And now you can add any other validation you want.
I would also implement a solution for the pasting issue, something like (see also in the link):
private void textBoxPasting(object sender, DataObjectPastingEventArgs e)
{
if (e.DataObject.GetDataPresent(typeof(String)))
{
var text = (String)e.DataObject.GetData(typeof(String));
if (!isTextAllowed(text))
{
e.CancelCommand();
}
}
else
{
e.CancelCommand();
}
}
Good job, but let me do the following task with a UserControl in C #:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace NumericBox
{
public partial class NumericBox : TextBox
{
public NumericBox
{
this.TextAlign = HorizontalAlignment.Right;
this.Text = "0,00";
this.KeyPress += NumericBox_KeyPress;
}
public double NumericResult
{
get{
double d = Convert.ToDouble(this.Text.Replace('.', ','));
return d;
}
}
private void NumericBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
e.Handled = true;
if ((e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1))
e.Handled = true;
if (e.KeyChar == 13)
{
e.Handled = true;
SendKeys.Send("{TAB}");
}
}
}
}
I have an input box that should allow numbers/currency only. For this purpose I use the InputScope "CurrencyAmount".
When I run the code a numeric keyboard will pop up but the user is allowed to enter many decimal point instead of just one.
Example:
Inputs like "12.50" should be allowed in the textbox, but the user is able to enter value like "12....50", "..12.5....0" etc. instead.
How can I restrict the allowed textbox values to match my criterium?
I would go with a behavior and a regex. Then you can easily reuse your code for other textboxes as well.
public class RegexValidationBehavior : Behavior<TextBox>
{
public static readonly DependencyProperty RegexStringProperty =
DependencyProperty.Register("RegexString", typeof(string), typeof(RegexValidationBehavior), new PropertyMetadata(string.Empty));
public string RegexString
{
get { return GetValue(RegexStringProperty) as string; }
set { SetValue(RegexStringProperty, value); }
}
protected override void OnAttached()
{
base.OnAttached();
if (AssociatedObject != null)
{
AssociatedObject.TextChanged += OnTextChanged;
}
Validate();
}
protected override void OnDetaching()
{
base.OnDetaching();
if (AssociatedObject != null)
{
AssociatedObject.TextChanged -= OnTextChanged;
}
}
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
Validate();
}
private void Validate()
{
var value = AssociatedObject.Text;
if (value.IsNotEmpty() && RegexString.IsNotEmpty())
{
MatchAgainstRegex(value);
}
}
private void MatchAgainstRegex(string value)
{
var match = Regex.Match(value, RegexString);
if (!match.Success)
{
AssociatedObject.Text = value.Remove(value.Length - 1);
AssociatedObject.Select(AssociatedObject.Text.Length, 0);
}
}
}
Then in your XAML write something like.
<TextBox InputScope="Number" Text="{Binding Amount, Mode=TwoWay}">
<i:Interaction.Behaviors>
<Control:RegexValidationBehavior RegexString="{Binding OnlyTwoDecimalsRegex}"/>
</i:Interaction.Behaviors>
</TextBox>
In your ViewModel you specify a Regex, for example
public string OnlyTwoDecimalsRegex { get { return #"^([0-9]+)?([,|\.])?([0-9]{1,2})?$"; } }
you can try subscribing to TextChanged event for textbox and run below validation - works well for other locales too apart from en-US.
string decimalsep = CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
int decimalSepCount = text1.Text.Count(f => f == decimalsep[0]);
if (decimalSepCount > 1)
{
MessageBox.Show("Invalid input");
}
I would append a key down event handler to your textbox and validate if your input matches your predicate.
Pseudocode:
//...
//register event handler
yourTextBox.KeyDown += new KeyEventHandler(yourTextBox_KeyDown);
//...
//the keydown event
public void yourTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(yourTextBox.Text,"<enter a regular expression here>"))
e.Handled = true;
else e.Handled = false;
}
Visual C#.NET:
private void tbAddress_GotFocus()
{
tbAddress.Text = "";
}
private void tbAddress_LostFocus()
{
if (tbAddress.Text == "") { tbAddress.Text = "Email Address"; }
}
So, I'm trying to use that code to make it so there is text in a (Windows Forms) textbox, and then when the textbox gets focus (when the user clicks inside of it), the text disappears. That way it looks like a form with the label inside the textbox.
So, why doesn't that code work, or is there a better way to do it?
Here's a tutorial on how to do watermarks in textboxes.
http://vidmar.net/weblog/archive/2008/11/05/watermarked-textbox-in-windows-forms-on-.net.aspx
Use the textBox1.GotFocus event
textBox1.GotFocus += textBox1_GotFocus;//at the designer, constructor or form load...
private void textBox1_GotFocus(object sender, EventArgs e)
{
textBox1.Clear();//clear the text.
}
//Update:
If you already did that, then the problem must be in somewhere else, this code should be running without any problem.
+1 to #RexM for the pointer to the simplest, and probably best answer.
There's a more complex approach to this which is loosely based on the idea from this answer. It allows for customizing the color of the displayed text, but it probably has some other problems.
I'd really recommend using RexM's answer. Anyhow, for reference, here's the code for the other approach:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace MyControls
{
public class HintedTextBox : TextBox
{
public HintedTextBox() : base() { ResetHintColor(); }
[Description("The color of the hint text to display"),
Category("Appearance")]
public Color HintColor { get; set; }
// Default value handling for HintColor
private Color DefaultHintColor { get { return Color.LightGray; } }
public void ResetHintColor() { HintColor = Color.LightGray; }
public bool ShouldSerializeHintColor() { return !HintColor.Equals(DefaultHintColor); }
[Description("The textual hint to display in the textbox"),
Category("Behavior"),
Localizable(true)]
public string HintText
{
get { return m_hintText; }
set
{
if (m_hintText != value)
{
m_hintText = value;
UpdateHintTextState(true);
}
}
}
private string m_hintText = "";
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
HasFocus = true;
UpdateHintTextState();
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
HasFocus = false;
UpdateHintTextState();
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
UpdateHintTextState();
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 15) // WM_PAINT
{
PaintHintText();
}
}
private bool DisplayHintText { get; set; }
private bool HasFocus { get; set; }
private void PaintHintText()
{
if (DisplayHintText)
{
using (Graphics g = Graphics.FromHwnd(this.Handle))
using (SolidBrush b = new SolidBrush(HintColor))
{
StringFormat sf = new StringFormat();
switch (this.TextAlign)
{
case HorizontalAlignment.Center:
sf.Alignment = StringAlignment.Center;
break;
case HorizontalAlignment.Right:
sf.Alignment = StringAlignment.Far;
break;
default:
sf.Alignment = StringAlignment.Near;
break;
}
g.DrawString(HintText, Font, b, ClientRectangle, sf);
}
}
}
private void UpdateHintTextState() { UpdateHintTextState(false); }
private void UpdateHintTextState(bool forceInvalidate)
{
bool prevState = DisplayHintText;
if (HintText.Length == 0)
DisplayHintText = false;
else if (Text.Length != 0)
DisplayHintText = false;
else
DisplayHintText = !HasFocus;
if (DisplayHintText != prevState || forceInvalidate)
Invalidate();
}
}
}
It doesn't work because by default the textbox does not trigger a postback, and without a postback, server side events do not process.
Your best option here is to use client side javascript to handle this functionality - because round tripping to to the server on each textbox event is wasteful and annoying for the user.