Chase Ernst
Chase Ernst

Reputation: 1155

Deleting text from text box

I am trying to delete text from my text box once it gets to a certain amount of characters. I am using the following code:

Private Sub MainTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MainTextBox.TextChanged

    If MainTextBox.Text.Length >= 50 Then
        MainTextBox.Text.Remove(1, 10)
    End If

End Sub

When I execute the code, it does not give me any error messages or anything the code runs perfectly, but it does not delete the text like I want it to. Any help?

Upvotes: 2

Views: 140

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460268

Strings are immutable, that means you cannot modify them without creating a new string.

MainTextBox.Text = MainTextBox.Text.Remove(1, 10)

However, your code makes little sense. Do you want to shorten the text to a certain amount of characters?

If MainTextBox.Text.Length >= 50 Then
    MainTextBox.Text = MainTextBox.Text.Substring(0, 50))    
End If 

Upvotes: 6

Related Questions