Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextBoxMask cursor at beginning if empty #757

Merged
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Microsoft.Toolkit.Uwp.UI.Controls/TextBoxMask/TextBoxMask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private static void InitTextBoxMask(DependencyObject d, DependencyPropertyChange
textbox.TextChanging -= Textbox_TextChanging;
textbox.Paste -= Textbox_Paste;
textbox.Loaded -= Textbox_Loaded;
textbox.GotFocus -= Textbox_GotFocus;
textbox.Loaded += Textbox_Loaded;
}

Expand Down Expand Up @@ -119,10 +120,38 @@ private static void Textbox_Loaded(object sender, RoutedEventArgs e)
textbox.TextChanging += Textbox_TextChanging;
textbox.SelectionChanged += Textbox_SelectionChanged;
textbox.Paste += Textbox_Paste;
textbox.GotFocus += Textbox_GotFocus;
textbox.SetValue(OldTextProperty, textbox.Text);
textbox.SelectionStart = 0;
}

private static void Textbox_GotFocus(object sender, RoutedEventArgs e)
{
var textbox = sender as TextBox;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest doing a single if (textbox == null) return; after this instead of all these null checks on every reference to textbox.

Having said that, this is a private method only used with the TextBox.GotFocus event, so the sender should always be set with the current instance of the TextBox control!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Do explicit cast and remove ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I need to ask @deltakosh is there is any chance I can get a null here from the framework ?

var mask = textbox?.GetValue(MaskProperty) as string;
var placeHolderValue = textbox?.GetValue(PlaceHolderProperty) as string;
var representationDictionary = textbox?.GetValue(RepresentationDictionaryProperty) as Dictionary<char, string>;
if (string.IsNullOrWhiteSpace(mask) ||
representationDictionary == null ||
string.IsNullOrEmpty(placeHolderValue))
{
return;
}

var placeHolder = placeHolderValue[0];
var displayText = mask;
foreach (var key in representationDictionary.Keys)
{
displayText = displayText.Replace(key, placeHolder);
}

// if the textbox got focus and the textbox is empty (contains only mask) set the textbox cursor at the beginning to simulate normal TextBox behavior if it is empty.
if (string.Equals(textbox.Text, displayText))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably just always put the cursor to the end of where the user has entered text. If they have entered no text, it's at 0. If they have entered 3 of 10 characters (eg: phone number) then it's at 3.

Copy link
Contributor

@skendrot skendrot Jan 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, for the phone number example it would be at 4 to bypass the first dash

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IbraheemOsama any thoughts on this ?

{
textbox.SelectionStart = 0;
}
}

private static async void Textbox_Paste(object sender, TextControlPasteEventArgs e)
{
e.Handled = true;
Expand Down