C# WPF-只需使用特定字符串更改文本框的字符

C# WPF-只需使用特定字符串更改文本框的字符,c#,wpf,textbox,C#,Wpf,Textbox,我试图创建一个工具来支持我网络上的用户,基本上我有一个文本框,你可以输入一个主机名。我想知道是否只有在文本框中输入了6位数字时,才可以在文本框中添加字符。如果是别的,别管它 基本上,如果数字是123456,则在“C123456”开头加上“C” 但是如果有人已经输入了C,不要做任何事情 还有一些我不想更改的字符(对于其他主机名)。仅当输入6位数字时,在前面加一个“c” 示例代码: 这就是我到目前为止所做的: private void IPfind(object sender, Ke

我试图创建一个工具来支持我网络上的用户,基本上我有一个文本框,你可以输入一个主机名。我想知道是否只有在文本框中输入了6位数字时,才可以在文本框中添加字符。如果是别的,别管它

基本上,如果数字是123456,则在“C123456”开头加上“C” 但是如果有人已经输入了C,不要做任何事情

还有一些我不想更改的字符(对于其他主机名)。仅当输入6位数字时,在前面加一个“c”

示例代码:

这就是我到目前为止所做的:

        private void IPfind(object sender, KeyEventArgs e)
        {

            if (e.Key == Key.Enter)
            {
                TextBox tb = (TextBox)sender;
                string text = tb.Text.ToUpper();
                int num;
                if (int.TryParse(text, out num))
                {
                    // it is an integer. Simply add a C at the begining if it has enough characters

                    if (text.Length == 6)
                    {
                        tb.Text = 'C' + text;
                        tb.CaretIndex = tb.Text.Length;
                    }
                    else
                    {

                    }
                    Ipbox.Clear();
                    try
                    {
                        // Host Name resolution to IP
                        IPHostEntry host = Dns.GetHostEntry(Assetbox.Text.Trim());
                        IPAddress[] ipaddr = host.AddressList;
                        // Loop through the IP Address array and add the IP address
                        foreach (IPAddress addr in ipaddr)
                        {
                            // Finds the IP V4 address
                            if (addr.AddressFamily == AddressFamily.InterNetwork)
                                Ipbox.Text = (addr.ToString());
                        }
                    }

所以我认为正则表达式在这里非常有用

制作文本框,然后在XAMl中制作文本更改事件

 <TextBox x:Name="textBox" TextChanged="textBox_TextChanged"/>
编辑

如果您只想匹配p或C字符加上数字,请创建两个正则表达式模式

var pattern = new Regex(@"(c{1}[0-9]{6})");
var pattern2 = new Regex(@"(p{1}[0-9]{6})");

然后选中文本框中的匹配项

使用
if
块检查所有内容。我在评论中解释了不同部分:

    bool _changing = false; // since you are going to change Text you need this to stop a loop or other errors
    private void Tb_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (_changing)
            return;
        _changing = true;
        TextBox tb = (TextBox)sender;
        string text = tb.Text.ToUpper();

        if (text == null || text.Length == 0)
        {
            // The user has not enter enough characters yet
            return;
        } 

        int num;
        if (int.TryParse(text, out num))
        {
            // it is an integer. Simply add a C at the begining if it has enough characters
            if (text.Length == 6)
            {
                tb.Text = 'C' + text;
                tb.CaretIndex = tb.Text.Length;
            }
            else
            {
                // let use to continue typing
            }
        }
        else
        {
            // it is not an integer
            //check if it starts with P or C
            if (text[0] == 'C' || text[0] == 'P')
            {
                string textrest = text.Remove(0, 1);
                if (textrest.Length == 0)
                {
                    // it is just a C or P
                    return;
                }

                if (int.TryParse(textrest, out num))
                {
                    // it became an integer after removing the first char. It is OK then.
                }
                else
                {
                    // it is not a number and removing the first C or P did not solve the problem
                    // throw new FormatException();
                    // or
                    MessageBox.Show("Wrong Format. Enter ###### or C###### or P#######");
                }
            }
            else
            {
                // it is not a number and the reason is not because it starts with C or P
                // throw new FormatException();
                // or
                MessageBox.Show("Wrong Format. Enter ###### or C###### or P#######");
            }
        }
        _changing = false;
    }

非常感谢@JohnChris和@Ramin

我使用了两种解决方案来解决这个问题

        TextBox tb = (TextBox)sender;
        string text = tb.Text;
        if (Regex.IsMatch(text, @"^\d+$") && text.Length == 6)
            {
                tb.Text = 'C' + text;
                tb.CaretIndex = tb.Text.Length;
            }

这很容易做到,所以我给你一个很好的解决方案,你能澄清一下吗,你想让他们输入一个文本框“CXXXXXX”,其中X是一个数字,如果这是正确的,你将字母存储在前面或整个字符串存储在另一个变量中?是的-但问题是。。。6位数字表示库存编号,“C”表示计算机,“P”表示打印机。。。它们都有6个数字。。。因此,如果有人要输入CXXXXXX或PXXXXXX,我不希望它在6位数字前添加另一个C来保留主机名。我希望我更清楚,如果不清楚,我会再试一次。感谢您的帮助:)**我们还有其他主机名用于大型机网络。。。比如KICX1234,也需要单独使用。在问这个问题之前,您尝试了什么?给我们看看你的代码?这一半对我来说很有用谢谢,唯一的问题是它只接受数字-因为它是一个整数。。。如果else语句以P或C开头,则它接受输入。。。基本上我需要任何东西。如果我输入www.google.ca,它将运行而不更改。唯一的例外是,如果它是完全数字和6位数字,在它之前加上一个C。我用代码更新了原来的帖子(到目前为止我已经有了)
        TextBox tb = (TextBox)sender;
        string text = tb.Text;
        if (Regex.IsMatch(text, @"^\d+$") && text.Length == 6)
            {
                tb.Text = 'C' + text;
                tb.CaretIndex = tb.Text.Length;
            }