Solved

listbox load txt file

Posted on 2009-05-11
6
718 Views
Last Modified: 2012-06-27
im in vb.net 2008 and I have an empty listbox, i would like to load a text file into this listbox via a dialog box.the list file is formatted with line breaks as shown below
name1
name2
name3
name4
0
Comment
Question by:j0eh4x
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Expert Comment

by:jaydee-cdgp
ID: 24360839
0
 
LVL 3

Expert Comment

by:janjerell
ID: 24360848
Hi,

This is how I do it. My listbox is named lst1, and my button is named btnLoad.

And below is my code behind:


Imports System.IO
 
Public Class Form1
 
    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        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.                    
                    LoadToListBox(myStream)
                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
 
    Private Sub LoadToListBox(ByVal myStream As Stream)
        Dim myStreamReader As StreamReader = New StreamReader(myStream)
 
        Me.lst1.Items.Clear()
 
        While myStreamReader.Peek() >= 0
            Dim str As String = myStreamReader.ReadLine
            Me.lst1.Items.Add(str)
        End While
 
    End Sub
 
End Class

Open in new window

0
 

Author Comment

by:j0eh4x
ID: 24360861
i wish do this with a dialog box that allows me to choose the file. that doesnt show how
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 15

Accepted Solution

by:
JackOfPH earned 500 total points
ID: 24360868
try this code...
Try
With OpenFileDialog1
'With statement is used to execute statements using a particular object, here,_
'OpenFileDialog1
.Filter = "Text files (*.txt)|*.txt|" & "All files|*.*"
'setting filters so that Text files and All Files choice appears in the Files of Type box
'in the dialog
If .ShowDialog() = DialogResult.OK Then
'showDialog method makes the dialog box visible at run time
FileName = .FileName
sr = New StreamReader(.OpenFile)
'using streamreader to read the opened text file
Dim textFile as string = sr.ReadToEnd()
        Dim str1() As String = textFile.Split(vbCrLf)
 
        For Each str2 As String In str1
            listbox1.items.add(str2)
        Next
 
'displaying text from streamreader in richtextbox
End If
End With
Catch es As Exception
MessageBox.Show(es.Message)
Finally
If Not (sr Is Nothing) Then
sr.Close()
End If
End Try

Open in new window

0
 
LVL 3

Expert Comment

by:janjerell
ID: 24361224
In my code, you need to click the button named btnLoad to show the File Dialog where you can pick the file you wanted.
0
 
LVL 4

Expert Comment

by:barrislb
ID: 24364462
Hi j0eh4x,

I'm a little curious regarding your question?? I have read your question over a few times already to see if there were something I was mis-reading. I say that because the experts; janjerell and JackOfPH have seemed to answer your initial question. Is there something that maybe you have not mentioned? I wanted to take shot at it myself, but the other experts have answered what you asked. Just curious? :-)
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question