[RESOLVED] Load Textfile into two listboxes with substring in one
I am working on a program that opens a textfile into two listboxes using this code for each listbox (under form1_load event):
Dim lines() As String = IO.File.ReadAllLines(Application.StartupPath & "\Tasks Config Files\Run Commands.txt")
ListBox1.Items.AddRange(lines)
However, the problem is that every line in the text has two segments and each half gets loaded individually into each listbox. For example, the text file looks like:
Command Prompt***cmd.exe
Task Manager***taskmgr.exe
System Configuration***msconfig.exe
Then when it loads into listbox1, it needs to be everything before the three aesteriks. Example:
Listbox1_Items()
Command Prompt
Task Manager
System Configuration
& for listbox2, everything after the three asteriks. Example:
Listbox2_Items()
cmd.exe
taskmgr.exe
msconfig.exe
I started working on this using this code:
Dim phrase As String = ListBox1.Items.Item(count).Split("***")(1)
and then I created a loop. However, I have not had any success. Again, I need to be able to load the substrings and afterstrings into the corresponding listboxes. Please help? Suggestions?
Re: Load Textfile into two listboxes with substring in one
You're on the right track with the use of Split but, like every problem, you should work out the logic you need to implement before trying to write code to implement it. You don't need any programming experience to write an algorithm because it is something that can be implemented with pen and paper, so anyone can do that. So, what's your algorithm? Once you have a working algorithm, i.e. one that you can step through manually and get the desired result, then you actually have something to compare your code to as you write it. Not being able to write code when you don't know what it actually has to do is no real surprise. Work out what it has to do first.
Re: Load Textfile into two listboxes with substring in one
I can see a problem with your strategy. Presumably you're going to want to synchronize your scrolling with your 2 listboxes?
Re: Load Textfile into two listboxes with substring in one
JMcilhinney gave you good advice on how to approach programming generally. To answer this question...
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lines() As String = IO.File.ReadAllLines("E:\Desktop\Run Commands.txt")
Dim first() As String = Array.ConvertAll(lines, Function(l As String) l.Split(New String() {"***"}, StringSplitOptions.RemoveEmptyEntries)(0))
Dim second() As String = Array.ConvertAll(lines, Function(l As String) l.Split(New String() {"***"}, StringSplitOptions.RemoveEmptyEntries)(1))
ListBox1.DataSource = first
ListBox2.DataSource = second
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.Items.Count = 0 Or ListBox2.Items.Count = 0 Then Return
ListBox2.SelectedIndex = ListBox1.SelectedIndex
End Sub
Private Sub ListBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
If ListBox1.Items.Count = 0 Or ListBox2.Items.Count = 0 Then Return
ListBox1.SelectedIndex = ListBox2.SelectedIndex
End Sub
End Class
Re: Load Textfile into two listboxes with substring in one
Quote:
Originally Posted by
.paul.
JMcilhinney gave you good advice on how to approach programming generally. To answer this question...
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lines() As String = IO.File.ReadAllLines("E:\Desktop\Run Commands.txt")
Dim first() As String = Array.ConvertAll(lines, Function(l As String) l.Split(New String() {"***"}, StringSplitOptions.RemoveEmptyEntries)(0))
Dim second() As String = Array.ConvertAll(lines, Function(l As String) l.Split(New String() {"***"}, StringSplitOptions.RemoveEmptyEntries)(1))
ListBox1.DataSource = first
ListBox2.DataSource = second
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.Items.Count = 0 Or ListBox2.Items.Count = 0 Then Return
ListBox2.SelectedIndex = ListBox1.SelectedIndex
End Sub
Private Sub ListBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
If ListBox1.Items.Count = 0 Or ListBox2.Items.Count = 0 Then Return
ListBox1.SelectedIndex = ListBox2.SelectedIndex
End Sub
End Class
Awesome!!!! You are awesome, thank you. Your code is very nicely written too.