简体   繁体   English

c#验证输入字符串并使用线程替换为TitleCase

[英]c# Validating input string and replace to TitleCase using Threading

I want to check what the user is writing in a textbox and take this input string from the textBox which is contained on a winform, this then needs to converted to have every letter of a word Capitalised. 我想检查用户在文本框中写的内容,并从winform中包含的文本框中获取此输入字符串,然后将其转换为将单词的每个字母都大写。

Using this input, I need to firstly validate if they have left the text box empty, Null. 使用此输入,我首先需要验证他们是否将文本框留空,为Null。 Second step was to check if they have formed a coherent sentence using Title case "in other words, making sure they haven't been typing with caps lock on or vise versa". 第二步是使用Title大小写检查他们是否形成了连贯的句子“换句话说,确保他们没有大写锁定或反之亦然”。

I have been researching into Threading and Regex with regards to Validation. 我一直在研究有关验证的Threading和Regex。 Using threading i have used the methods IsLower() and then the ToLower(). 使用线程我已经使用了IsLower()方法,然后使用了ToLower()方法。 This may be a rather long way of converting the input, at a bare minimum I could just convert all of the first letter of every word to upper case and all the middle to lower. 这可能是转换输入的相当长的方法,至少可以将每个单词的所有首字母都转换为大写,而将所有中位数转换为小写。

If i havent explained my issue well enough just ask and will gladly fire over any extra info please find my source code below: 如果我还没有很好地解释我的问题,请问一下,并乐意为您提供任何其他信息,请在下面找到我的源代码:

        string myText = tbProductId.Text;

        //// Check for null values
        if (myText.Equals(""))
        {    //// display Error prompt
            MessageBox.Show("Please enter somthing");
            //// Get length of entered string
        }

        else
        {

            for (int i = 0; i < myText.Length; i++)
            {
                //// check if lower case
                if (char.IsLower(myText[i]))
                {
                    string asTitleCase = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.
                    ToTitleCase(myText.ToLower());
                    MessageBox.Show("Please enter non Captilised strings");

                }
            }
            for (int i = 0; i < myText.Length; i++)
            {
                if (char.IsUpper(myText[i]))
                {
                    string asTitleCase = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.
                   ToTitleCase(myText.ToUpper());
                    MessageBox.Show("Please enter strings in Title Case");
                }
            }

        }
    }

Thanks for all of the feedback and help, please find the solution attached below provided by @ Wiktor Stribiżew. 感谢您提供所有反馈和帮助,请在下面找到@ WiktorStribiżew提供的解决方案。 However there is one more issue, when shift is held it is inactive and doesnt work any ideas why it would be working with caps lock on but not with shift ? 但是,还有一个问题,当按住shift键时,它是不活动的,并且不起作用,为什么在大写锁定时不起作用,而对shift键却不起作用?

   string myText = tbProductId.Text;
        if (myText.Equals(""))
        {
            MessageBox.Show("Please enter somthing");
        }
        else
        {
            tbProductId.Text = System.Threading.Thread.CurrentThread.CurrentCulture.TextInf‌​o.ToTitleCase(tbProd‌​uctId.Text);

            tbProductId.Focus();
            tbProductId.Select(tbProductId.Text.Length, 0);
            //Move Cursor to location of where error

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM