Home > Software engineering >  How can I paste by CTRL V in WPF, C#
How can I paste by CTRL V in WPF, C#

Time:10-25

I have C# WPF project, I have 4 cells and I tried paste some code to those cells(like PIN code), but when i clicked CTRL V , its copied first digit only to first cell. How can I resolve that?

.xaml file

<StackPanel Style="{DynamicResource HorizontalPanel}" >
                    <Menu>
                        <MenuItem Command="ApplicationCommands.Paste" />
                    </Menu>
                    <Border Style="{DynamicResource DigitBorder}">
                        <TextBox Style="{DynamicResource Digit1Text}" Name="Digit1" TextChanged="Digit1_TextChanged" />
                    </Border>
                    <Border Style="{DynamicResource DigitBorder}">
                        <TextBox Style="{DynamicResource Digit2Text}" Name="Digit2" TextChanged="Digit2_TextChanged"/>
                    </Border>
                    <Border Style="{DynamicResource DigitBorder}">
                        <TextBox Style="{DynamicResource Digit3Text}" Name="Digit3" TextChanged="Digit3_TextChanged"/>
                    </Border>
                    <Border Style="{DynamicResource DigitBorder}">
                        <TextBox Style="{DynamicResource Digit4Text}" Name="Digit4" TextChanged="Digit4_TextChanged"/>
                    </Border>
</StackPanel>

.xaml.cs file:

}

        public PasswordScreen(object object1, object object2, ConnectorAction connectorAction)
        {
            InitializeComponent();
            Digit1.Focus();            
            Object1 = object1;
            Object2 = object2;
            ConnectorAction = connectorAction;          
            MenuItem pasteMenuItem = new MenuItem();          
            pasteMenuItem.Command = ApplicationCommands.Paste;                      
        }

private void Digit1_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (Digit1.Text.Length == 1)
            {
                Digit2.Focus();
            }          
        }

same for Digit2_TextChanged,Digit3_TextChanged,Digit4_TextChanged

I don't understand how I catch here the pin - code and paste the numbers each number on different cell? emphasized text

CodePudding user response:

If i understood that correctly you want to paste the 4 digit pin code and every digit in one TextBox.

One way could be to handle it in the text changed of the TextBox (example for first text box, for other textbox modification needed if all text boxes should support the splitting):

private void Digit1_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (Digit1.Text.Length <= 1)
            return;
        
        string text = Digit1.Text;
        Digit2.Text = text[1].ToString();
        if (text.Length > 2)
            Digit3.Text = text[2].ToString();
        if (text.Length > 3)
            Digit4.Text = text[3].ToString();

        Digit1.Text = text[0].ToString();
    }

Or write a own command for pasting which could be invoked through a button click or a shortcut:

private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        if (!Clipboard.ContainsText())
            return;

        string clipboardText = Clipboard.GetText();

        if (string.IsNullOrEmpty(clipboardText))
            return;

        int length = clipboardText.Length;
        if (length > 0)
            Digit1.Text = clipboardText[0].ToString();
        if (length > 1)
            Digit2.Text = clipboardText[1].ToString();
        if (length > 2)
            Digit3.Text = clipboardText[2].ToString();
        if (length > 3)
            Digit4.Text = clipboardText[3].ToString();
    }
  • Related