Greg
Greg

Reputation: 55

tOGGLE cASE For Textbox?

How can I add tOGGLE cASE to textboxes, for example, I click a button and it changes the text in a textbox to tOGGLE cASE (hello -> hELLO), basically it takes first letter and lower cases it and the rest upper cases it.

Upvotes: 0

Views: 288

Answers (2)

Moon
Moon

Reputation: 1151

Here is a method using .NET Culture functions to first convert to Title Case and then invert the case to your "tOGGLE cASE"

Private Sub btn_ConvertTotOGGLEcASE_Click(sender As Object, e As EventArgs) Handles btn_ConvertTotOGGLEcASE.Click

    'Get the current value of the textbox
    Dim MyText As String = MyTextBox.Text

    'Convert it to Title Case using built in .NET tools
    Dim MyTextInfo As System.Globalization.TextInfo = New System.Globalization.CultureInfo("en-US", False).TextInfo
    MyText = MyTextInfo.ToTitleCase(MyText)

    'Then invert the case of all the characters
    Dim InvertedText As Char() = MyText.Select(Function(c) If(Char.IsLetter(c), If(Char.IsUpper(c), Char.ToLower(c), Char.ToUpper(c)), c)).ToArray()

    'Finally convert it back to a string
    MyTextBox.Text = New String(InvertedText)

End Sub

Upvotes: 2

Luc Berthiaume
Luc Berthiaume

Reputation: 316

You can split you string in an array, iterate in the array with lcase(mid(string,1,1) & ucase(mid(string,2, len(string)-1)), and recompose your array in a string

Public function ToogleText(myStr as string) as string

  dim str() as string  
  str = split(myStr," ")
  dim toogleStr as string
  toogleStr = ""

  for each substr as string in str
       toogleStr = toogleStr & lcase(mid(substr,1,1)) & ucase(mid(substr, 2,len(substr)-1)) & "  "
   next substr
    if len(toogleStr) > 0 then
        ToogleText =  mid(toogleStr,1,len(toogleStr)-1)
   else
        ToogleText =""

   end if

end function

Upvotes: 0

Related Questions