如果TextBox具有8个字符,则将其清除xaml C#

问题描述 投票:0回答:1

我想使textBox组件具有4个元素(带空格的8个字符)后变得清晰

Text box

c# xaml textbox
1个回答
0
投票

处理文本框的KeyDown事件并检查textBox.Text.Length == 8。您也可以将其他所有字符都强制为空格。

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (textBox1.Text.Length == 8)
    {
        // do something with the text

        textBox1.Text = "";     // clear the textbox
    }
    // optional else if to only allow spaces
    else if (textBox1.Text.Length % 2 == 1  // odd index characters
        && e.KeyCode != Keys.Space)         // must be spaces
    {
        e.Handled = true;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.