Solved

Need Help with writing to a text file

Posted on 2003-11-17
4
199 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

770 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