Reputation: 23
It is writing to txt file file but only 1 field per line. I need a comma between each field in the text file so each record is on 1 line. How do I do that?
Here's this portion of my code.
Imports System.IO
Public Class ContactInfoForm
' Declare module-level variable.
Private ContactInfoStreamWriter As StreamWriter
Private Sub SaveButton_Click(sender As System.Object, e As System.EventArgs) Handles SaveButton.Click
' Save the users contact information to the end of the file.
' Make sure name field and at least 1 number field is not empty.
If NameTextBox.Text <> "" And PhoneNumberTextBox.Text <> "" Or PagerNumberTextBox.Text <> "" Or
CellPhoneNumberTextBox.Text <> "" Or VoiceMailNumberTextBox.Text <> "" Then
If ContactInfoStreamWriter IsNot Nothing Then ' Check if the file is open
ContactInfoStreamWriter.WriteLine(NameTextBox.Text)
ContactInfoStreamWriter.WriteLine(PhoneNumberTextBox.Text)
ContactInfoStreamWriter.WriteLine(PagerNumberTextBox.Text)
ContactInfoStreamWriter.WriteLine(CellPhoneNumberTextBox.Text)
ContactInfoStreamWriter.WriteLine(VoiceMailNumberTextBox.Text)
ContactInfoStreamWriter.WriteLine(EmailAddressTextBox.Text)
With NameTextBox
.Clear()
.Focus()
End With
PhoneNumberTextBox.Clear()
PagerNumberTextBox.Clear()
CellPhoneNumberTextBox.Clear()
VoiceMailNumberTextBox.Clear()
EmailAddressTextBox.Clear()
Else ' File is not open
MessageBox.Show("You must open the file before you can save your contact information.", "File is Not Open",
MessageBoxButtons.OK, MessageBoxIcon.Information)
' Display the File Open dialog box.
OpenToolStripMenuItem_Click(sender, e)
End If
Else
MessageBox.Show("Please enter your name and at least 1 number where you can be reached.", "Data Entry Error",
MessageBoxButtons.OK)
NameTextBox.Focus()
End If
End Sub
Upvotes: 1
Views: 1502
Reputation: 149
It is because you're using WriteLine where it writes its parameter into a single line. You can either use .Write() or use String.Format(), example:
Dim info As String = ""
info = String.Format("{0},{1},{2},{3},{4},{5}", _
NameTextBox.Text, PhoneNumberTextBox.Text, PagerNumberTextBox.Text, CellPhoneNumberTextBox.Text, VoiceMailNumberTextBox.Text, EmailAddressTextBox.Text)
ContactInfoStreamWriter.WriteLine(info)
Upvotes: 1