Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,250 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,218 people online right now. Registration is fast and FREE... Join Now!




Open Dialog

 
Reply to this topicStart new topic

Open Dialog

camckee316
13 Oct, 2008 - 08:49 PM
Post #1

New D.I.C Head
*

Joined: 29 Aug, 2008
Posts: 44


My Contributions
I am trying to open a file and read it into a list box, using the open dialog box. I get as far as selecting a file but then nothing happens. Can anyone show me my error(s)?
Attached is the sample file that I would like to read in. It is just a test file. Thanks.
CODE

    Private Sub EmpFile_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles EmpFile.FileOk
        Dim inputFile As System.IO.StreamReader
        If EmpFile.ShowDialog() = Windows.Forms.DialogResult.OK Then
            MessageBox.Show(EmpFile.FileName)
            With EmpFile
                .Filter = "Text files (*.txt)|*.txt|All Files(*.*)|*.*"
                .InitialDirectory = "C:\Documents and Settings\Me\Desktop\WITCC\3rd Quarter Classes\VB\EmployeeData Part 3\EmployeeData Part 3\bin\Debug\Employee_Data2.txt"
                .Title = "Select file to open"
                If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                    inputFile = File.OpenText(.FileName)
                    ListBox1.Items.Add(inputFile.ReadLine())
                    inputFile.Close()
                End If
            End With

        Else
            MessageBox.Show("You Selected No File", "File Selection")
        End If



Attached File(s)
Attached File  Employee_Data2.txt ( 7bytes ) Number of downloads: 11
User is offlineProfile CardPM
+Quote Post

thava
RE: Open Dialog
14 Oct, 2008 - 12:46 AM
Post #2

D.I.C Regular
Group Icon

Joined: 17 Apr, 2007
Posts: 442



Thanked: 18 times
Dream Kudos: 50
My Contributions
hope you get a answer in .net section
User is offlineProfile CardPM
+Quote Post

magicmonkey
RE: Open Dialog
14 Oct, 2008 - 08:28 AM
Post #3

D.I.C Regular
***

Joined: 12 Sep, 2008
Posts: 413



Thanked: 68 times
My Contributions
You are only reading the first line of the text file, you need to loop through the file until you hit the endofstream.

vb

do until inputfile.endofstream
ListBox1.Items.Add(inputFile.ReadLine())
loop

User is offlineProfile CardPM
+Quote Post

camckee316
RE: Open Dialog
14 Oct, 2008 - 10:14 AM
Post #4

New D.I.C Head
*

Joined: 29 Aug, 2008
Posts: 44


My Contributions
ok I tried that it may work, I am not sure. Once I get to the part where I select the file and click open to open that file nothing happens. I am missing some type of code and the books that I am using only have a paragraph on this, and part of the paragraph is the code to use.
User is offlineProfile CardPM
+Quote Post

magicmonkey
RE: Open Dialog
14 Oct, 2008 - 11:25 AM
Post #5

D.I.C Regular
***

Joined: 12 Sep, 2008
Posts: 413



Thanked: 68 times
My Contributions
After reading your code again I noticed that you have place this code in the FileOk event of your OpenFileDialog. I am not sure if that is what you intended, however if it is then you should not be showing the dialog again within this event. Infact you call ShowDialog twice in that event handler.

Let me try to simplify this for you in a button click event instead of the FileOk event.

vb

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim EmpFile As New OpenFileDialog

With EmpFile
.Filter = "Text files (*.txt)|*.txt|All Files(*.*)|*.*"
'.InitialDirectory = "C:\Documents and Settings\Me\Desktop\WITCC\3rd Quarter Classes\VB\EmployeeData Part 3\EmployeeData Part 3\bin\Debug\Employee_Data2.txt"
.InitialDirectory = Application.StartupPath 'Don't hardcode paths like above
.Title = "Select file to open"
End With

Select Case EmpFile.ShowDialog()
Case Windows.Forms.DialogResult.OK
Using inputFile As IO.StreamReader = My.Computer.FileSystem.OpenTextFileReader(EmpFile.FileName)
ListBox1.BeginUpdate()
Do Until inputFile.EndOfStream
ListBox1.Items.Add(inputFile.ReadLine)
Loop
ListBox1.EndUpdate()
End Using
End Select
End Sub





User is offlineProfile CardPM
+Quote Post

camckee316
RE: Open Dialog
14 Oct, 2008 - 12:06 PM
Post #6

New D.I.C Head
*

Joined: 29 Aug, 2008
Posts: 44


My Contributions
Thank you. That works like a dream.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 04:11AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month