[2005] Need help with the openfiledialog & listview or listbox
I have my openfiledialog and set the code but can't get it to display the text in a listview or a listbox after i open it only thing i can get the text displayed in is a textbox hope somebody can help
Re: [2005] Need help with the openfiledialog & listview or listbox
If you've got the text into a TextBox then you've opend the file so this question ahs nothing to do with the OpenFileDialog. If you want to put strings into a ListBox then you need to add them to its Items collection. If you have a text file with multiple lines and you want each line to be an item in a ListBox then the easiest way is like this:
VB Code:
Using ofd As New OpenFileDialog
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
Me.ListBox1.Items.AddRange(IO.File.ReadAllLines(ofd.FileName))
End If
End Using
or this:
VB Code:
Using ofd As New OpenFileDialog
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
Me.ListBox1.DataSource = IO.File.ReadAllLines(ofd.FileName)
End If
End Using
The ListView is a lttle more complex because you have to create a ListViewItem object for each line:
VB Code:
Using ofd As New OpenFileDialog
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim lines As String() = IO.File.ReadAllLines(ofd.FileName)
Dim upperBound As Integer = lines.GetUpperBound(0)
Dim items(upperBound) As ListViewItem
For index As Integer = 0 To upperBound Step 1
items(index) = New ListViewItem(lines(index))
Next index
Me.ListView1.Items.AddRange(items)
End If
End Using
Re: [2005] Need help with the openfiledialog & listview or listbox
thx alot worked perfectly i would think ppl would use the listbox more since less code but seems ppl use listview more is there some advantage to listview that i'm missing?
Re: [2005] Need help with the openfiledialog & listview or listbox
The ListBox will display a list of items in a single column and is perfect for that simple case. The ListView has far more functionality than that. It provides multiple views, mulitple columns with headers, groups, etc. The right-hand side of Windows Explorer is a ListView, as is the message list in Outlook.