locked
Forcing upper case on text fields RRS feed

  • Question

  • Hi,

    I have a series of text fields on a simple form (inside a group box) I would like to enforce all the text be upper case upon submission to the database. I know I can use the Ucase command. The question is implementation. How can I very efficiently force all the field values to upper case vs. say doing Ucase(TextBox1), Ucase(TextBox2)....  ( I have 12 fields in this case) Is there a way to treat the text boxes as a collection and do it in one shot? Just trying to be very efficient and create clean, tight code even on such a simple thing.
    Saturday, August 22, 2009 12:39 PM

Answers

  • For Each ctl As Control In GroupBox1.Controls

    If TypeOf ctl Is TextBox Then

    Dim tbox As TextBox

    tbox = DirectCast(ctl,TextBox)

    tbox.Text = tbox.Text.ToUpper

    End If

    Next

    Asgar
    • Proposed as answer by Waleed El-Badry Saturday, August 22, 2009 12:44 PM
    • Edited by _asgar Saturday, August 22, 2009 12:45 PM corr
    • Marked as answer by gedkins Saturday, August 22, 2009 1:14 PM
    Saturday, August 22, 2009 12:43 PM

All replies

  • For Each ctl As Control In GroupBox1.Controls

    If TypeOf ctl Is TextBox Then

    Dim tbox As TextBox

    tbox = DirectCast(ctl,TextBox)

    tbox.Text = tbox.Text.ToUpper

    End If

    Next

    Asgar
    • Proposed as answer by Waleed El-Badry Saturday, August 22, 2009 12:44 PM
    • Edited by _asgar Saturday, August 22, 2009 12:45 PM corr
    • Marked as answer by gedkins Saturday, August 22, 2009 1:14 PM
    Saturday, August 22, 2009 12:43 PM
  • Also, the TextBox control has a CharacterCasing property that you can set in the designer to UpperCase that will force all characters entered to UPPER CASE regardless of how the user enters them, and they will display that way as well. This way when you pull the values from the TextBox.Text property they'll already be in caps and there will be no need to use UCase at all.

    Cheers!


    Anthony D. Green Solution Architect IRC: ##vb.net on FreeNode
    Monday, August 24, 2009 4:13 AM