Load from file into listbox?
So in my program i am trying to make it so that when you press a button it opens a "choose file" popup(where the user can choose a text file) then after the user chooses it the program will automatically load each line of the textfile into a listbox.
But i have been researching it and the only thing i have been able to find is a file>open thing. So how cold i make it open a "open" dialogue on the press of a button
and since i haven't been able to find anything on the open dialogue i haven't looked for anythi开发者_Python百科ng on the loading each line of it into a listbox, so if anyone wants ot help me with that it would be great.
and i do not have any code to show you as the rest of my program has no relevance to this
Using FD As New OpenFileDialog()
FD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
If FD.ShowDialog = Windows.Forms.DialogResult.OK Then
Listbox1.Items.Clear
ListBox1.Items.AddRange(IO.File.ReadAllLines(FD.FileName))
End If
End Using
EDIT: Answer on the comment:
If you can use LINQ, then its a one row of code to read all lines from the listbox and write it to a file:
Save using SaveFileDialog and LINQ
Using FD As New SaveFileDialog()
FD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
If FD.ShowDialog = Windows.Forms.DialogResult.OK Then
IO.File.WriteAllLines(fd.filename, (From p As String In ListBox1.Items Select p).ToArray)
End If
End Using
If you can't use LINQ, then you can do this instead:
Save using SaveFileDialog and FOR/EACH
Using FD As New SaveFileDialog()
FD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
If FD.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim FileContent As String = ""
For Each i As String In ListBox1.Items
FileContent &= i & vbCrLf
Next
IO.File.WriteAllText(FD.FileName, FileContent)
End If
End Using
Basically, there are a couple parts here. First, you want to create an Open File Dialog box that prompts the user for where the file is. Here is how you do that:
http://www.homeandlearn.co.uk/net/nets4p6.html
Next, you want to read the text file in line by line into your listbox. Here is how you read your text file (you will need to modify the code to have it add the lines to the listbox instead of doing a Console.WriteLine:
http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx
OpenFileDialog from MSDN:
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myStream As Stream = Nothing
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = openFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
' Insert code to read the stream here.
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
' Check this again, since we need to make sure we didn't throw an exception on open.
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
End If
End Sub
As you can see from the comments, you can read a stream after the user has opened the file. You can then read this stream using, for example, a StreamReader. That will give you the data within the file the user chose. Depending on what you want, you can then parse that data and add it to your listbox.
精彩评论