Solved

Need Help with writing to a text file

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…
Suggested Courses
Course of the Month5 days, 18 hours left to enroll

627 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