Automatic Capitalisation Of Words In A Text Box

A question arose recently on the newsgroup of how to automatically capitalise the first letter of a word in a text box, e.g. by activating the shift key on the soft keyboard. Since there is no API for the SIP to do this the workaround is to simulate a mouse click over the position of the Shift key in the soft keyboard.


An alternative method is to handle the TextChanged event on the TextBox and add some logic to capitalise words as the text is entered. The advantage here is that it is not dependent on a specific SIP being in use:-


private void textBox1_TextChanged(object sender, System.EventArgs e)


{


  //start with true as the first character should always be upper case


  bool makeUpper = true;


  //get the current text as a char array


  char[] oldText = textBox1.Text.ToCharArray();


  //create a new char array the same size for the new text


  char[] newText = new char[oldText.Length];



  //for each char


  for(int iChar = 0; iChar < oldText.Length; iChar++)


  {


    if(makeUpper)


    {


      //make the character upper-case and reset the flag


      newText[iChar] = Char.ToUpper(oldText[iChar]);


      makeUpper = false;


    }


    else


    {


      //copy the character as-is


      newText[iChar] = oldText[iChar];


    }


    //if it’s a space the next character should be upper-case


    if(oldText[iChar] == ‘ ‘)


    {


      makeUpper = true;


    }


  }


  //assign the new value


  textBox1.Text = new String(newText);


  //position the cursor at the end of the text


  textBox1.SelectionStart = textBox1.Text.Length;


}

Advertisements

2 thoughts on “Automatic Capitalisation Of Words In A Text Box

  1. Peter et al,

    the current code is clear and well structured.

    Two possible improvements which might make the code a bit more performant (not verified), but perhaps a bit less clear:

    – Two arrays for text
    Currently there are two copies of the Textbox contents: oldText and newText. Think one copy should do, too.

    – Uppercased space char
    Currently, ToUpper is called for space chars, too. By first checking for space char and then checking the ‘makeUpper’ flag, this could be avoided.

    The code below should do the same job. Hope the formatting turns out properly.

    Regards,

    Helmut

    private static string ToUpperWords(string oldText) {

    bool makeUpper = true;

    char[] newText = oldText.ToCharArray();

    for(int iChar = 0; iChar < oldText.Length; iChar++) {
    //if it’s a space there’s no need for uppercasing, but next character should be upper-case
    if(oldText[iChar] == ‘ ‘) {
    makeUpper = true;
    } else {
    if(makeUpper) {
    //make the character upper-case
    newText[iChar] = Char.ToUpper(oldText[iChar]);
    }
    // Reset the flag (current char is no space)
    makeUpper = false;
    }
    }
    //return the new value
    return new String(newText);
    }

  2. Same code as in my previous post, but with most of Peter’s original comments.

    Regards,

    Helmut

    private static string CapitalizeWords(string oldText) {
    //start with true as the first character should always be upper case
    bool makeUpper = true;

    //get the current text as a char array
    char[] newText = oldText.ToCharArray();

    //for each char
    for(int iChar = 0; iChar < oldText.Length; iChar++) {
    //if it’s a space there’s no need for uppercasing, but next character should be upper-case
    if(oldText[iChar] == ‘ ‘) {
    makeUpper = true;
    } else {
    if(makeUpper) {
    //make the character upper-case
    newText[iChar] = Char.ToUpper(oldText[iChar]);
    }
    // Reset the flag (current char is no space)
    makeUpper = false;
    }
    }
    //return the new value
    return new String(newText);
    }

Comments are closed.