简体   繁体   中英

Input scope for textbox not working in windows 10 universal apps

Here i tried the code.

Xaml:

<TextBox Header="Telephone Number" InputScope="TelephoneNumber"/>

Cs:

TextBox phoneNumberTextBox = new TextBox();
phoneNumberTextBox.Header="Telephone Number";    
InputScope scope = new InputScope();
InputScopeName scopeName = new InputScopeName();
scopeName.NameValue = InputScopeNameValue.TelephoneNumber;
scope.Names.Add(scopeName);
phoneNumberTextBox.InputScope = scope;

but when i press any key on keyboard it showing on textbox any one please help me for this..

Read the document https://msdn.microsoft.com/library/windows/apps/hh702632

The input scope provides a hint at the type of text input expected by the control. Various elements of the system can respond to the hint provided by the input scope and provide a specialized UI for the input type. For example, the touch keyboard might show a number pad for text input when the control has its InputScope set to Number.

The control might also interpret the data being entered differently (typically for East Asian related input scopes). The input scope does not perform any validation, and does not prevent the user from providing any input through a hardware keyboard or other input device.

If you want to restrict characters from Textbox Try to add Key_Down Event inside textbox.

<TextBox Name="textbox" KeyDown="textbox_KeyDown" MaxLength="10"InputScope="Number" />

and C# code

 private void textbox_KeyDown(object sender, KeyRoutedEventArgs e)
    {
        if ((e.Key < VirtualKey.NumberPad0 || e.Key > VirtualKey.NumberPad9) & (e.Key < VirtualKey.Number0 || e.Key > VirtualKey.Number9))
        {
            e.Handled = true;
        }
    }

Here is a my solution

<TextBox Name="texbox"   TextChanging="intTextBox_TextChanging" MaxLength="10" InputScope="Number" />


private void intTextBox_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
{
    if (!Regex.IsMatch(sender.Text, "^\\d*?\\d*$") && sender.Text != "")
    {
        int pos = sender.SelectionStart - 1;
        sender.Text = sender.Text.Remove(pos, 1);
        sender.SelectionStart = pos;
    }
}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM