Question Deleting text from multiple texboxes

wasimking

Member
Joined
May 21, 2012
Messages
5
Programming Experience
1-3
Hello I want To remove one character from from one textbox with backspace and it will remove more than one character from other textbox

if i remove "b" from textbox1 it should automaticaly remove "king" from second texbox
Capture2.PNG

and error is "Index and count must refer to a location within the string.Parameter name: count"
the world length is same means if enter "c" it will generate 4 character word with one space in textbox2
this is code

Private Sub txtInput_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtInput.KeyPress

Dim len2 As Integer

len2 = txtCode.Text.Length

If (Asc(e.KeyChar) = 65 Or Asc(e.KeyChar) = 97) Then

txtCode.Text += "Acer" & " "

ElseIf (Asc(e.KeyChar) = 66 Or Asc(e.KeyChar) = 98) Then
txtCode.Text += "King" & " "

ElseIf (Asc(e.KeyChar) = 8) Then
Try
txtCode.Text = txtCode.Text.Remove(len2 - 4, len2)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If

End Sub
 
What you have provided is a single example but you haven't provided the general case. There's no point our providing code that will work for one example because it likely won't work with any other example. Please provide a FULL and CLEAR description of the general rules you want to apply.
 
i have two text boxes in 1st text box i am entering one alpha bate and it is generating more than one character in second text box,,,for example i am entering "a" in first textbox it giving me "apple" "ball" for "b" in 2nd textbox(the length of words in second textbox is always same i.e.4 and one space after each word) and if a mistake occur during typing in 1st text box and i removed character "b" from 1st textbox it should automatically remove "ball" from second text box this i want to achieve,my main project is diferent from this but same i want to achieve over there
 
i have two text boxes in 1st text box i am entering one alpha bate and it is generating more than one character in second text box,,,for example i am entering "a" in first textbox it giving me "apple" "ball" for "b" in 2nd textbox(the length of words in second textbox is always same i.e.4 and one space after each word) and if a mistake occur during typing in 1st text box and i removed character "b" from 1st textbox it should automatically remove "ball" from second text box this i want to achieve,my main project is diferent from this but same i want to achieve over there

Are you thinking of a Childrens flash card style app, where each letter of the alphabet generates a word a=apple, b=ball, c=car ..etc?
Entering the letter into textbox1 changes the word in textbox2?

In which case, you could you the textbox1's textchanged event to call a method to re-populate textbox2 with the correct word, in that method , you could specifiy if blank then textbox2.text = "" ?

If I have the wrong end of the stick, can you explain further?
 
So you're saying that there is a specific 1:1 relationship between letters and words, i.e. every time you enter a specific letter in the first TextBox the same word will appear in the second TextBox, correct? I would tend to store the words in a Dictionary keyed by letter. You will also need to be a bit clever to allow for the fact that the user may not delete or backspace at the end of the text. Here's my example:
Private wordsByLetter As New Dictionary(Of String, String) From {{"a", "apple"},
                                                                 {"b", "ball"},
                                                                 {"c", "cat"}} 'etc

Private oldText As String = String.Empty

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    Dim currentText = TextBox1.Text

    If currentText = String.Empty Then
        'Clear the text.
        TextBox2.Clear()
    ElseIf currentText.StartsWith(oldText) Then
        'The user has appended a new character so append a new word.
        Dim lastLetter = currentText.Substring(currentText.Length - 1)

        If wordsByLetter.ContainsKey(lastLetter) Then
            'Add a space before all but the first word.
            If TextBox2.TextLength > 0 Then
                TextBox2.AppendText(" ")
            End If

            TextBox2.AppendText(wordsByLetter(lastLetter))
        End If
    Else
        'Completely rebuild the text from scratch.
        TextBox2.Text = String.Join(" ", From letter In currentText
                                         Where wordsByLetter.ContainsKey(letter)
                                         Select wordsByLetter(letter))
    End If

    oldText = currentText
End Sub
Note that that doesn't account for special situations like the user pasting multiple characters into the TextBox at once but it will simply ignore characters that are not in the Dictionary.
 
I just noticed that you're suing .NET 3.5 so the use of From to initialise the Dictionary wouldn't work. You'd have to Add the items individually in the Load event handler.
 
jmcilhinney sir,
i tried your code in new project which have only two textboxes replacing this code "From {{"a", "apple"},{"b", "ball"},{"c", "cat"}}"
by "wordsByLetter.Add("a", "apple")
wordsByLetter.Add("b", "ball")
wordsByLetter.Add("c", "cat")"
this and getting following error if i enter more than one character in that textbox or if i delete current character
"An item with the same key has already been added." on this line "wordsByLetter.Add("a", "apple")"
 
Then you are trying to add the Dictionary items in the TextChanged event handler. The only things you put in the TextChanged event handler are the things you want done every time the Text changes. Think about it. Do you really want to add all the items to the Dictionary every time the Text changes? Of course not. My code does it once only, when the form is first created. How would you usually do something once only when the form is created? You'd put it in the Load event handler.
 
Back
Top