Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need Help with writing to a text file

Posted on 2003-11-17
4
Medium Priority
?
211 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
[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
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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

715 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