C# 添加破折号'-';在Windows phone 8.1 c中存储信用卡信息的文本框中输入四个字符#

C# 添加破折号'-';在Windows phone 8.1 c中存储信用卡信息的文本框中输入四个字符#,c#,textbox,windows-phone-8.1,C#,Textbox,Windows Phone 8.1,我正在开发银行应用程序,我必须为此存储信用卡信息 输入用户的卡号 每四个字符的破折号“-”将添加到该文本框中 e、 g1234-1234-1234-1234 我尝试了KeyDown和TextChanged,但没有得到答案。 按键关闭事件 txtcardNumber.Focus(FocusState.Keyboard); string temp = string.Empty; if (txtcardNumber.Text.Length == 4)

我正在开发银行应用程序,我必须为此存储信用卡信息

  • 输入用户的卡号
  • 每四个字符的破折号
    “-”
    将添加到该文本框中
  • e、 g
    1234-1234-1234-1234

    我尝试了
    KeyDown
    TextChanged
    ,但没有得到答案。 按键关闭事件

    txtcardNumber.Focus(FocusState.Keyboard);
                string temp = string.Empty;
                if (txtcardNumber.Text.Length == 4)
                {
                    temp = txtcardNumber.Text;
    
                    txtcardNumber.Text += '-';
                    txtcardNumber.UpdateLayout();
    
    
    
                }
                else if (txtcardNumber.Text.Length == 9)
                {
                    txtcardNumber.Text = txtcardNumber.Text + "-";
                }
                else if (txtcardNumber.Text.Length == 14)
                {
                    txtcardNumber.Text = txtcardNumber.Text + "-";
                }
    
    当它在开始时自动添加“-”光标时,我不知道为什么可以在这种情况下使用。您可以按如下方式设置遮罩:

    mskCardBox.Mask = "0000-0000-0000-0000";
    

    也许这对你有帮助

    不要等到破折号在4位数之后。 获取一个文本框,双击它并编写以下代码,使该文本框autopostback=true

    一次写入所有16位数字,然后单击tab或enter,破折号将自动拆分为4位数字

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        string cardno = TextBox1.Text;
        var list = Enumerable
            .Range(0, cardno.Length / 4)
            .Select(i => cardno.Substring(i * 4, 4))
            .ToList();
        var resl = string.Join("-", list);
        TextBox1.Text = resl;
    }
    

    我找到了简单的解决办法

                   var insertText = "-";
                   var selectionIndex = creditcard.SelectionStart;
                   creditcard.Text = creditcard.Text.Insert(selectionIndex, insertText);
                   creditcard.SelectionStart = selectionIndex + insertText.Length;
    

    这是蒙面文本框的工作:)蒙面
    文本框如何?我不知道它是否适用于您的情况。请展示您的尝试。我们如何在windows phone 8.1 phone(RT)应用程序中使用MaskedTextBox?