Solved

Need Help with writing to a text file

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Use Multiple Forms 4 49
VBA color chart bars 12 79
Access 2013 combo box not working 3 38
Getting warning: You are about to delete 1 row(s) 9 43
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

867 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

20 Experts available now in Live!

Get 1:1 Help Now