none
.txt file to listbox RRS feed

  • Question

  • Hello, my target is to load an txt file into listbox the problem is get behind and next the - separator. 

    The txt file contains separator like:

    A - 1

    B - 2

    C - 3

    When i load the txt into listbox i only want to show text behind the - separator but when some line is selected on listbox i want 

    Textbox1.text = behind - separator (A or B or C...) 

    Textbox2.text = next - separator (1 or 2 or 3...)

    Tuesday, October 1, 2019 1:31 PM

Answers

  • Hi

    Assuming you have to have the list as <letter><space><hyphen><space><number> then try this

    Form1 with ListBox1, TextBox1 and TextBox2

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim fn As String = "C:\Users\lesha\Desktop\testfile.txt"
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    		Dim lines() As String = IO.File.ReadAllLines(fn)
    		ListBox1.DataSource = lines
    
    	End Sub
    
    	Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    		Dim s() As String = ListBox1.SelectedItem.ToString.Split(New String() {" ", "-"}, StringSplitOptions.RemoveEmptyEntries)
    		TextBox1.Text = s(0)
    		TextBox2.Text = s(1)
    	End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, October 1, 2019 2:07 PM
    • Marked as answer by extream87 Tuesday, October 1, 2019 7:07 PM
    Tuesday, October 1, 2019 2:02 PM

All replies

  • Hi

    Does the text file haver to have the separator like <space>-<space>?

    Can it be made to be A-1, B-2, C-3 (without <spaces>?

    Is the text file one that you create yourself - are you in control of its creation?


    Regards Les, Livingston, Scotland

    Tuesday, October 1, 2019 1:44 PM
  • Hi

    Assuming you have to have the list as <letter><space><hyphen><space><number> then try this

    Form1 with ListBox1, TextBox1 and TextBox2

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim fn As String = "C:\Users\lesha\Desktop\testfile.txt"
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    		Dim lines() As String = IO.File.ReadAllLines(fn)
    		ListBox1.DataSource = lines
    
    	End Sub
    
    	Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    		Dim s() As String = ListBox1.SelectedItem.ToString.Split(New String() {" ", "-"}, StringSplitOptions.RemoveEmptyEntries)
    		TextBox1.Text = s(0)
    		TextBox2.Text = s(1)
    	End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, October 1, 2019 2:07 PM
    • Marked as answer by extream87 Tuesday, October 1, 2019 7:07 PM
    Tuesday, October 1, 2019 2:02 PM
  • Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ListBox1.Items.Clear()
            For Each item In Split(TextBox1.Text, vbCrLf)
                ListBox1.Items.Add(item)
            Next
        End Sub
    End Class
    
    


    Success
    Cor

    Tuesday, October 1, 2019 4:35 PM