Solved

Need Help with writing to a text file

Posted on 2003-11-17
4
197 Views
Last Modified: 2010-05-01
Hello,

I have never done this before but I am want to write a list of 5 names, mark, john, henry, edna, katherine to a text file.  Then when my program loads I would like retrieve those five names and put them in a combo box.  I am using vb.net, any help would be appreciated.  Oh yes I would also like to be able to edit the names from run time and add more if future names are needed.  can someone please help???

Thank you
r0sk0
0
Comment
Question by:r0sk0
4 Comments
 

Expert Comment

by:aHBe
ID: 9763501
dim line as String

Open <Filename> For Output As #1 ' Open the File for writing
print #1,<line>                               ' Write to the File
Close #1                                        ' Close again

Open <Filename> For Input As #1 ' Open the File for reading
Input #1,<line>                               ' Read from the File
cbo.AddItem(line)                           ' Put line into ComboBox
Close #1                                        ' Close again

0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9763554
Hi!

Put 3 Command buttons, textbox and a combobox, and paste this code:

----------------------------------------------------
Private Sub Command1_Click()
    Open App.Path & "\test.txt" For Output As #1                'Saves combo box contents to a file
        For X = 1 To Combo1.ListCount
            Print #1, Combo1.List(X - 1)
        Next X
    Close #1
   
End Sub

Private Sub Command2_Click()
    Combo1.AddItem Text1.Text
End Sub

Private Sub Command3_Click()
    Combo1.Clear
End Sub

Private Sub Form_Load()
    On Error Resume Next
   
    Command1.Caption = "Save"
    Command2.Caption = "Add"
    Command3.Caption = "Clear"
   
    Open App.Path & "\test.txt" For Input As #1                 'Loads all lines from file to combo box
        Do Until EOF(1)
            Line Input #1, temp
            Combo1.AddItem temp
        Loop
    Close #1
        Combo1.ListIndex = 0
       
    If Err <> 0 Then MsgBox "File still doesn't exist, so it wasn't loaded"
End Sub
--------------------------------------------------------------------------------

The program loads names from a file and puts it in combobox.

Button add, adds text specified in textbox to a combobox.
Button clear, clears the combo box
Button Save, saves all items in combo box to a file.

0
 
LVL 42

Accepted Solution

by:
frodoman earned 250 total points
ID: 9763648
You might have better luck next time posting in the .Net forum - that way you won't have people giving you VB5 & VB6 solutions when you're using vb.net.

Basic writing text to a file:
--------------------------------
Dim f As New IO.FileStream("c:\" & Me.txtFile.Text & ".txt", IO.FileMode.CreateNew)
Dim SW As New System.IO.StreamWriter(f)
SW.WriteLine("mark")
SW.WriteLine("john")
...etc...
SW.Close
f.Close

For reading lines from a text file:
--------------------------------------
Dim f As New IO.FileStream("c:\" & Me.txtFile.Text & ".txt", IO.FileMode.Open)
Dim SR As New System.IO.StreamReader(f)
msgbox(SR.ReadLine)
msgbox(SR.ReadLine)
...etc...
SR.Close
f.Close

When writing, make sure you change the mode to "IO.FileMode.Append" if you want to add to an existing file.  You can probably get the rest but let me know if you need more help.

Frodoman
0
 

Author Comment

by:r0sk0
ID: 9772879
Hey thanks for the help.  Sorry for not posting in the .net forum.  I was in a hurry getting ready to take off.  But thanks again, works fine.

r0sk0
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now