如何在文本框中输入10个字符后自动添加空间

时间:2019-01-04 05:14:11

标签: c# wpf

<TextBox adorners:Watermark.Text="Search Number Here"  GotFocus="txtNumber_GotFocus"  Height="30" Foreground="Black" ToolTip="Number" HorizontalAlignment="Left" Margin="20,20,0,0"  Name="txtNumber" VerticalAlignment="Top" Width="220" FontSize="16" Background="#b5d2fc" TextChanged="txtNumber_TextChanged" >
                    <TextBox.CommandBindings>
                        <CommandBinding Command="Paste" Executed="txtNumber_PasteCommand" />
                    </TextBox.CommandBindings>
                    <TextBox.InputBindings>
                        <KeyBinding Key="V" Modifiers="Control" Command="Paste" />
                    </TextBox.InputBindings>
                </TextBox>

以上是我的文本框代码。

我在文本框中输入数字时不添加空格,但是我希望输入的数字中的10个字符后会自动使用空格。

例如我输入了这样的数字

70221818289511657474883015884182374321578605809268.

但是当我真正输入时,我希望数字显示在这样的文本框中

7022181828 9511657474 8830158841 8237432157 8605809268

代码

private void txtNumber_PasteCommand(object sender, ExecutedRoutedEventArgs e)
{
   string _copiedText = Clipboard.GetText();
   _copiedText = _copiedText.Replace("\n", " ").Replace("\r", "").Replace("'", "").Replace("\"", "") + " ";
   if (!string.IsNullOrEmpty(txtNumber.SelectedText))
   {
      txtNumber.SelectedText = _copiedText;
   }
   else
   {
      txtNumber.Text += _copiedText;
      txtNumber.Select(txtNumber.Text.Length, 1);
      txtNumber.ScrollToEnd();
      txtNumber.Focus();
   }
}

3 个答案:

答案 0 :(得分:0)

 Use SplitText method inside your  txtNumber_PasteCommand Command Handler.

     private  string SplitText(string textData, int splitBy)
    {
        int textLength = textData.Length - 1;
        string resultText = string.Empty;
        string triageText = textData;
        while (true)
        {
            if (triageText.Length >= splitBy)
            {
                resultText = $"{resultText} {triageText.Substring(0, splitBy)}";
                triageText = triageText.Substring(10);
            }
            else
            {
                resultText = $"{resultText} {triageText.Substring(0)}";
                break;
            }
        }
        return resultText;
    }

txtNumber_PasteCommand处理程序方法已更改为使用SplitText方法。它由2个参数组成。 1)TextData 2)按数字拆分。

private void txtNumber_PasteCommand(object sender, ExecutedRoutedEventArgs e)
{
 string _copiedText = Clipboard.GetText();
 _copiedText = _copiedText.Replace("\n", " ").Replace("\r", "").Replace("'", 

              "").Replace("\"", "") + " ";
       if (!string.IsNullOrEmpty(txtNumber.SelectedText))
       {
          txtNumber.SelectedText = SplitText(_copiedText,10);
       }
       else
       {
          txtNumber.Text += SplitText(_copiedText,10);
          txtNumber.Select(txtNumber.Text.Length, 1);
          txtNumber.ScrollToEnd();
          txtNumber.Focus();
       }
    }

更新:-   使用Property并将其与TextBox绑定。在set属性内,使用SplitText方法。

private string textData;
public string TextData { get => textData; set => textData = SplitText(value,10); }

因此,每次绑定数据文本时,都会使用空格进行更新。

答案 1 :(得分:0)

我自己解决这个问题

int pos = 0;
                int len = txtNumber.Text.Length;
                string s = txtNumber.Text;

                while (true)
                {
                    if (pos >= len) break;
                    if (space == s[pos] && (((pos + 1) % 11) != 0 || pos + 1 == s.Length))
                    {
                        s.Remove(pos, pos + 1);
                        txtNumber.Select(txtNumber.Text.Length, 1);
                        txtNumber.ScrollToEnd();
                        txtNumber.Focus();
                    }
                    else
                    {
                        pos++;
                    }
                }
                pos = 10;
                while (true)
                {
                    if (pos >= len) break;
                    char c = s[pos];
                    if (char.IsDigit(c))
                    {
                        s = s.Insert(pos, "" + space);
                        txtNumber.Text = s;
                    }
                    pos += 11;
                    txtNumber.Select(txtNumber.Text.Length, 1);
                    txtNumber.ScrollToEnd();
                    txtNumber.Focus();
                }

答案 2 :(得分:0)

private void txtNumber_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                int pos = 0;
                int len = txtNumber.Text.Length;
                string s = txtNumber.Text;

                while (true)
                {
                    if (pos >= len) break;
                    if (space == s[pos] && (((pos + 1) % 11) != 0 || pos + 1 == s.Length))
                    {
                        s.Remove(pos, pos + 1);
                        txtNumber.Select(txtNumber.Text.Length, 1);
                        txtNumber.ScrollToEnd();
                        txtNumber.Focus();
                    }
                    else
                    {
                        pos++;
                    }
                }
                pos = 10;
                while (true)
                {
                    if (pos >= len) break;
                    char c = s[pos];
                    if (char.IsDigit(c))
                    {
                        s = s.Insert(pos, "" + space);
                        txtNumber.Text = s;
                    }
                    pos += 11;
                    txtNumber.Select(txtNumber.Text.Length, 1);
                    txtNumber.ScrollToEnd();
                    txtNumber.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

我使用KeyDown事件解决此问题