Results 1 to 4 of 4

Thread: [2005] Need help with the openfiledialog & listview or listbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    115

    Question [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Using ofd As New OpenFileDialog
    2.     If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
    3.         Me.ListBox1.Items.AddRange(IO.File.ReadAllLines(ofd.FileName))
    4.     End If
    5. End Using
    or this:
    VB Code:
    1. Using ofd As New OpenFileDialog
    2.     If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
    3.         Me.ListBox1.DataSource = IO.File.ReadAllLines(ofd.FileName)
    4.     End If
    5. End Using
    The ListView is a lttle more complex because you have to create a ListViewItem object for each line:
    VB Code:
    1. Using ofd As New OpenFileDialog
    2.     If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
    3.         Dim lines As String() = IO.File.ReadAllLines(ofd.FileName)
    4.         Dim upperBound As Integer = lines.GetUpperBound(0)
    5.         Dim items(upperBound) As ListViewItem
    6.  
    7.         For index As Integer = 0 To upperBound Step 1
    8.             items(index) = New ListViewItem(lines(index))
    9.         Next index
    10.  
    11.         Me.ListView1.Items.AddRange(items)
    12.     End If
    13. End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    115

    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?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width