简体   繁体   中英

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.

Using this input, I need to firstly validate if they have left the text box empty, 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".

I have been researching into Threading and Regex with regards to Validation. Using threading i have used the methods IsLower() and then the 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. 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 ?

   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

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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