locked
Change text in a textbox RRS feed

  • Question

  • I am using Visual Basic 2008

    I am trying to capitalize words in a text box.

    I am using the following code (which, in case you don't recognize it, came from an example which I modified):

    Public Sub FuncKeysModule(ByVal value As Keys)

            'Check what function key is in a pressed state, and then perform the corresponding action.
            Select Case value
    ...
                Case Keys.F2
                    'Add the code for the function key F2 here.
                    Dim chars() As Char
                    Dim rString As String
                    chars = Me.MsgBody.SelectedText.ToCharArray
                    Try
                        Me.MsgBody.ReadOnly = False
                        chars(0) = UCase(chars(0))
                        rString = CStr(chars)
                        Me.MsgBody.Text.Replace(Me.MsgBody.SelectedText, rString)

                    Catch ex As Exception
                        MessageBox.Show("You must select some text for this to work")
                    End Try
    ....
            End Select
            Me.MsgBody.Refresh()
        End Sub

    In the application, I select a word in the textbox then hit F2. The text is pulled from the textbox, I manage to change the first letter to a capital letter BUT I cannot change the textbox.

    I already set the ReadOnly property of the textbox to False when I initialized the textbox.  It looks like the Replace function does not work.

    I have fiddled with this for a while and finally decided to seek help.

    so, HELP!
    Sunday, March 7, 2010 2:03 AM

Answers

  • I think paste method might be better in your case since there might be same word in the beginning of text box and user trying to capitalize second word. The text is highlighted already so paste will replace the highlighted text

    Something like:
     chars(0) = UCase(chars(0))
                        rString = CStr(chars)
                        Me.MsgBody.Paste(rString)
    Your method did not work because replace is operating on text string returned from property of textbox, and you might have to take care of leading blank spaces also.
    Edit: when you are changing readonly textbox from code, you dont need to make it writable, it simple works

    Arjun Paudel
    • Proposed as answer by DiegoCattaruzza Sunday, March 7, 2010 10:36 AM
    • Marked as answer by Jeff Shan Friday, March 12, 2010 2:07 AM
    Sunday, March 7, 2010 2:21 AM

All replies

  • The Replace method does not change the existing text, it returns it instead. You need to set the controls text property to the changed text.

    Me.MsgBody.Text = Me.MsgBody.Text.Replace(Me.MsgBody.SelectedText, rString)

    Sunday, March 7, 2010 2:15 AM
  • I think paste method might be better in your case since there might be same word in the beginning of text box and user trying to capitalize second word. The text is highlighted already so paste will replace the highlighted text

    Something like:
     chars(0) = UCase(chars(0))
                        rString = CStr(chars)
                        Me.MsgBody.Paste(rString)
    Your method did not work because replace is operating on text string returned from property of textbox, and you might have to take care of leading blank spaces also.
    Edit: when you are changing readonly textbox from code, you dont need to make it writable, it simple works

    Arjun Paudel
    • Proposed as answer by DiegoCattaruzza Sunday, March 7, 2010 10:36 AM
    • Marked as answer by Jeff Shan Friday, March 12, 2010 2:07 AM
    Sunday, March 7, 2010 2:21 AM
  • jlab2010 , please use the insert code block button [ </>   ] located on the toolbar, to add code to a thread .
    also, do not forget to select vb.net for language from the combobox.

    With . trujade. : . Process . Start ( " mns " ) : End With

    Sunday, March 7, 2010 8:13 AM
  • I don't know if you can use this one, but it would not be right not to show this one here as answer on your question.

      MsgBody.Text = StrConv(MsgBody.Text, vbProperCase)


    Success
    Cor
    Sunday, March 7, 2010 10:17 AM