Tugrul Emre Atalay
Tugrul Emre Atalay

Reputation: 938

How can I block Pasting Text in my TextBox (WinRT)?

Could you help me please about my problem? I want to block my textbox's pasting and control all characters are numeric (WinRT).

Upvotes: 1

Views: 1567

Answers (4)

Lance
Lance

Reputation: 2933

To block the pasting: 1. Block it from the paste event

    txtBox1.Paste += ADDTextBox_Paste;

    void ADDTextBox_Paste(object sender, TextControlPasteEventArgs e) 
    {e.Handled = true;return; }
  1. Block the the Keydown event for shift + insert and Ctrl + V.

    void txtBox1_KeyDown(object sender, KeyRoutedEventArgs e)
        {            
            var shiftState = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Shift);
            var ctrlState = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Control);
    
            if (((shiftState & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down && e.Key == VirtualKey.Insert)
                ||((ctrlState & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down && e.Key == VirtualKey.V))
            {
                e.Handled = true; 
                return; 
            }                       
        }
    

To Allow only numeric characters you'll have to add this code inside the Keydown event:

if ((e.Key < VirtualKey.Number0 || e.Key > VirtualKey.Number9) &&
(e.Key < VirtualKey.NumberPad0 || e.Key > VirtualKey.NumberPad9))
{ e.handled = true; return; }

Upvotes: 1

borrrden
borrrden

Reputation: 33423

As of Windows 8.1, you get a "Paste" event that you can subscribe to. Simply subscribe to it and set the event's Handled property to true to stop it from reaching the TextBox.

Upvotes: 2

user2575132
user2575132

Reputation: 1

USe Shift A to block whole passage. Shift up down or side arrow to block small parts. Right click on blocked passage to 'copy' and paste.

Upvotes: -1

Farhan Ghumra
Farhan Ghumra

Reputation: 15296

UPDATE 1

If you want to prevent ctrl + c & ctrl + v combination, then you have to check that combination in KeyDown event. If you get that combination you can clear the clipboard with static method Windows.ApplicationModel.DataTransfer.Clipboard.Clear();

If you don't watch for combination of keys rather then just ctrl, then also you can prevent the copy pasting via keyboards.

private void TextBox_KeyDown_1(object sender, KeyRoutedEventArgs e)
{
    if (e.Key == Windows.System.VirtualKey.Control)
        Windows.ApplicationModel.DataTransfer.Clipboard.Clear();
}

To allow user to enter only numeric data you can use TextBox's TextChanged event. Use numeric only regular expression to filter out the characters. Moreover to disable context menu of TextBox, ContextMenuOpening event will help you. Below is the whole code.

XAML

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <TextBox Height="50" Width="300" TextChanged="TextBox_TextChanged_1" ContextMenuOpening="TextBox_ContextMenuOpening_1" />
</Grid>

C#

private void TextBox_TextChanged_1(object sender, TextChangedEventArgs e)
{
    var tb = (TextBox)sender;
    var IsNumeric = new System.Text.RegularExpressions.Regex("^[0-9]*$");
    var text = tb.Text;
    if (!IsNumeric.IsMatch(text))
    {
        int CursorIndex = tb.SelectionStart - 1;
        tb.Text = tb.Text.Remove(CursorIndex, 1);
        tb.SelectionStart = CursorIndex;
        tb.SelectionLength = 0;
    }
}

private void TextBox_ContextMenuOpening_1(object sender, ContextMenuEventArgs e)
{
    e.Handled = true;
}

Upvotes: 1

Related Questions