AHM3D AHM3D - 4 months ago 13
Vb.net Question

Splitting Multi Text in 2 different text boxes from 1 text box

enter image description herehope u guys are doing fine :)
my question is that i have 1 textbox with multi text enabled, i have some text data like this
so how can i do this in Visual Basic ?
i have setup an project like this but i does for single line , but i want to make it multi line

Imports System.IO
Public Class Form1
Dim somestring2() As String

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

somestring2 = TextBox1.Text.Split(":")
For i = 0 To somestring2.Length - 1
For a = 0 To somestring2.Length - 2
TextBox3.Text = somestring2(i)
TextBox2.Text = somestring2(a)
Next
Next
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox4.Text = TextBox2.Text + ":" + TextBox3.Text
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
somestring2 = TextBox1.Text.Split(":")
For i = 0 To somestring2.Length - 1
For a = 0 To somestring2.Length - 2
TextBox2.Text = somestring2(i)
TextBox3.Text = somestring2(a)
Next
Next
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
TextBox4.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox1.Clear()

End Sub

Private Sub ListBox1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub

Private Sub Butto[`enter image description here`][1]n5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim ofd As New OpenFileDialog
Dim fl As String
ofd.ShowDialog()
fl = ofd.FileName
Dim sr As New StreamReader(fl)
TextBox1.Text = sr.ReadToEnd()
sr.Close()


strong text
End Sub
End Class

Answer

Use String.Split, for example with this LINQ query:

Dim allLineSplits = From line In TextBox1.Lines
                    Let split = line.Split({":"}, StringSplitOptions.RemoveEmptyEntries)
                    Where split.Length >= 2
                    Select split

Dim textBox2Lines = From split In allLineSplits
                    Select split(0).Trim()
Dim textBox3Lines = From split In allLineSplits
                    Select split(1).Trim()
TextBox2.Lines = textBox2Lines.ToArray()
TextBox3.Lines = textBox3Lines.ToArray()
Comments