vb.net copy word into textboxes based on their position in sentence

90 Views Asked by At

I have a textbox (tInto1) that I have copied a sentence into. I have 10 textboxes to copy the words into, based on their position (that is 1st word goes into textbox1, 2nd word goes into textbox2, 3rd into textbox 3 till the last word is copied into the last textbox). The code below gives me the number of words in the sentence but not the word, based on its position.

`Dim miLine As String
 Dim i As Integer
 Dim MiArray() As String
 miLine = tInto1.Text
 MiArray = DisLine.Split(" ")

 For i = 0 To UBound(MiArray)
        if i=0 then 
        txtbox1.text = i

        elseif i=2 then
        textbox2.text = i

        elseif i=3 then
        textbox3 = i
        .
        .
        . 'for the other 6 elseif statements.
        else 
        textbox10 = i
 Next`

Help will be greatly appreciated.

1

There are 1 best solutions below

5
On

If your textboxes are named by number, you can try something like this:

Private Sub Btn_SplitToTextBoxes_Click(sender As Object, e As EventArgs) Handles Btn_SplitToTextBoxes.Click
    Dim Sentence As String = "This is a Test sentence"
    Dim Words() As String = Sentence.Split(CChar(" "))
    'You need to set a limit to operate only on TextBoxes 1 to 10
    Dim Limit As Integer = If(Words.Count <= 10, Words.Count -1, 9)
    For i As Integer = 0 To Limit
        Try
            Me.Controls("TextBox" & (i + 1).ToString).Text = Words(i)
        Catch ex As Exception
            MessageBox.Show("Error while inserting text into 'TextBox" & (i + 1).ToString & "'", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit For
        End Try
    Next
End Sub