0

The following method needs to successful take a user input and update their input to title case format e.g capital at the beginning of every word as they are typing, this is entered into a text box on a win form project. I have an issue with this method as it converts properly until I press caps lock or shift. It will also work if i hold both the buttons down not to sure if this just cancels each other out. I have been looking into Regex but not to sure how to implement it in this class. please find the code below for the function thanks in advance.

// User input stored in Temp Var
string myText = tbProductId.Text;

//
if (myText.Equals(null))
{
    // validation, check if the user has entered anything, if Null.
    MessageBox.Show("Please enter somthing");
}
else
{
    // convert to Title Case
    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 was found
}
caner
  • 721
  • 5
  • 21
  • There are several approaches to this. Two such ones: monitor their typing on a character-by-character basis and convert letters into upper or lower case depending on if it's the first or if it follows a space; or wait until the end and run the whole string through a [converter](http://stackoverflow.com/questions/913090/how-to-capitalize-the-first-character-of-each-word-or-the-first-character-of-a) to change it into the correct format. – Abion47 May 15 '17 at 07:51
  • You may also be interested in the [`Keyboard`](https://msdn.microsoft.com/en-us/library/system.windows.input.keyboard(v=vs.110).aspx) class. – Abion47 May 15 '17 at 07:53
  • The simplest one: `tbProductId.Text = System.Threading.Thread.CurrentThread.CurrentCulture.TextInf‌​o.ToTitleCase(tbProd‌​uctId.Text.ToLower());`. It converts all to lowercase first then returning title case as result. – Tetsuya Yamamoto May 15 '17 at 07:54
  • @TetsuyaYamamoto Thank you works perfectly thanks a bunch – whatdoyouNeedFromMe May 15 '17 at 08:04

3 Answers3

0

You can simply use :

tbProd‌​uctId.CharacterCasing = CharacterCasing.Lower

The behaviours you explained is standard per the docs is correct and this is the simply way around it.

I also agree with the comment above from @Tetsuya Yamamoto

Digvijay
  • 774
  • 5
  • 10
0

final result and answer thanks to the people who posted above

        // User input stored in Temp Var
        string myText = tbProductId.Text;
        var regex = new Regex(@"[^a-zA-Z0-9\s]");

        if (myText.Equals("") ||(regex.IsMatch(myText.ToString())))
        {
            MessageBox.Show("Please enter a Valid value no special chars or leaving this blank!!!!");
        }

        else 
        {
            tbProductId.Text = System.Threading.Thread.CurrentThread.CurrentCulture.TextInf‌​‌​o.ToTitleCase(tbPr‌​od‌​uctId.Text.ToLow‌​er());
            tbProductId.Focus();
            tbProductId.Select(tbProductId.Text.Length, 0);
            //Move Cursor to location of where error

}

0

Char for char ...

    public void textBox1_Click(Object ob, EventArgs eventArgs){
        if (textBox1.Text.Length > 0)
        {
            string text = textBox1.Text;
            string tmpText = "";

            if (textBox1.Text.Length == 1)
            {
                tmpText = text.ToUpper();
            }
            else
            {
                for (int i = 0; i < text.Length; i++)
                {
                    if (i < text.Length - 1)
                        tmpText += text[i];
                    else if (text[text.Length - 2] == ' ')
                        tmpText += text[text.Length - 1].ToString().ToUpper();
                    else
                        tmpText += text[i];
                }
            }
            textBox1.Text = tmpText;
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }
    }
Tom Henn
  • 125
  • 6