clint 2

Hi all

can anyone help me out here....

I am totally confused regarding the above.......

I want my application to create a text file which I can then edit and save ....so I can add information as required.......

do I use notepad or do I use a multiline textbox.....of somethin else

I need to know how to create, edit, and save...........please be explicid..as I am really lost in this area.....

cheers

clint




Re: Visual Basic Express Edition create,open,edit,save....text files

Merin Gazell

You can use a large multi line text box to use as the front end. Use the StreamReader and StreamWriter classes to read and write data. They are pretty simple, msdn them up.



Re: Visual Basic Express Edition create,open,edit,save....text files

clint

thanks merin.... but I really need more than that

the examples on stream reader are beyond me.....I just dont get it..

I have a multiline textbox........I need an example please

edit.....

ok.......this opens my txt file....

if I alter the text in mytext......how do I save it......

I can not get the hang of savefiledialog........please help

Private Sub Label17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label17.Click

If My.Computer.FileSystem.FileExists("c:\myText.txt") Then

TextBox1.BringToFront()

TextBox1.Visible = True

TextBox1.Text = My.Computer.FileSystem.ReadAllText("c:\myText.txt")

Else

TextBox1.Text = "not found"

End If

End Sub






Re: Visual Basic Express Edition create,open,edit,save....text files

Whoisit

This is right from the help menu, if you play around with it you should gain an understanding of what you are trying to do. Just copy and paste the full code in to a New Projects Form.

' The following example displays an application that provides the ability to ' open rich text files (rtf) into the RichTextBox. The example demonstrates ' using the FolderBrowserDialog to set the default directory for opening files. ' The OpenFileDialog class is used to open the file. Imports System Imports System.Drawing Imports System.Windows.Forms Imports System.IO Public Class FolderBrowserDialogExampleForm Inherits Form Private folderBrowserDialog1 As FolderBrowserDialog Private openFileDialog1 As OpenFileDialog Private richTextBox1 As RichTextBox Private mainMenu1 As MainMenu Private fileMenuItem As MenuItem Private WithEvents folderMenuItem As MenuItem, _ closeMenuItem As MenuItem, _ openMenuItem As MenuItem Private openFileName As String, folderName As String Private fileOpened As Boolean = False Public Sub New() Me.mainMenu1 = New System.Windows.Forms.MainMenu() Me.fileMenuItem = New System.Windows.Forms.MenuItem() Me.openMenuItem = New System.Windows.Forms.MenuItem() Me.folderMenuItem = New System.Windows.Forms.MenuItem() Me.closeMenuItem = New System.Windows.Forms.MenuItem() Me.openFileDialog1 = New System.Windows.Forms.OpenFileDialog() Me.folderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog() Me.richTextBox1 = New System.Windows.Forms.RichTextBox() Me.mainMenu1.MenuItems.Add(Me.fileMenuItem) Me.fileMenuItem.MenuItems.AddRange( _ New System.Windows.Forms.MenuItem() {Me.openMenuItem, _ Me.closeMenuItem, _ Me.folderMenuItem}) Me.fileMenuItem.Text = "File" Me.openMenuItem.Text = "Open..." Me.folderMenuItem.Text = "Select Directory..." Me.closeMenuItem.Text = "Close" Me.closeMenuItem.Enabled = False Me.openFileDialog1.DefaultExt = "rtf" Me.openFileDialog1.Filter = "rtf files (*.rtf)|*.rtf" ' Set the Help text description for the FolderBrowserDialog. Me.folderBrowserDialog1.Description = _ "Select the directory that you want to use As the default." ' Do not allow the user to create New files via the FolderBrowserDialog. Me.folderBrowserDialog1.ShowNewFolderButton = False ' Default to the My Documents folder. Me.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Personal Me.richTextBox1.AcceptsTab = True Me.richTextBox1.Location = New System.Drawing.Point(8, 8) Me.richTextBox1.Size = New System.Drawing.Size(280, 344) Me.richTextBox1.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or _ AnchorStyles.Bottom Or AnchorStyles.Right Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(296, 360) Me.Controls.Add(Me.richTextBox1) Me.Menu = Me.mainMenu1 Me.Text = "RTF Document Browser" End Sub <STAThread()> _ Shared Sub Main() Application.Run(New FolderBrowserDialogExampleForm()) End Sub ' Bring up a dialog to open a file. Private Sub openMenuItem_Click(sender As object, e As System.EventArgs) _ Handles openMenuItem.Click ' If a file is not opened, then set the initial directory to the ' FolderBrowserDialog.SelectedPath value. If (not fileOpened) Then openFileDialog1.InitialDirectory = folderBrowserDialog1.SelectedPath openFileDialog1.FileName = nothing End If ' Display the openFile dialog. Dim result As DialogResult = openFileDialog1.ShowDialog() ' OK button was pressed. If (result = DialogResult.OK) Then openFileName = openFileDialog1.FileName Try ' Output the requested file in richTextBox1. Dim s As Stream = openFileDialog1.OpenFile() richTextBox1.LoadFile(s, RichTextBoxStreamType.RichText) s.Close() fileOpened = True Catch exp As Exception MessageBox.Show("An error occurred while attempting to load the file. The error is:" _ + System.Environment.NewLine + exp.ToString() + System.Environment.NewLine) fileOpened = False End Try Invalidate() closeMenuItem.Enabled = fileOpened ' Cancel button was pressed. ElseIf (result = DialogResult.Cancel) Then return End If End Sub ' Close the current file. Private Sub closeMenuItem_Click(sender As object, e As System.EventArgs) _ Handles closeMenuItem.Click richTextBox1.Text = "" fileOpened = False closeMenuItem.Enabled = False End Sub ' Bring up a dialog to chose a folder path in which to open or save a file. Private Sub folderMenuItem_Click(sender As object, e As System.EventArgs) _ Handles folderMenuItem.Click ' Show the FolderBrowserDialog. Dim result As DialogResult = folderBrowserDialog1.ShowDialog() If ( result = DialogResult.OK ) Then folderName = folderBrowserDialog1.SelectedPath If (not fileOpened) Then ' No file is opened, bring up openFileDialog in selected path. openFileDialog1.InitialDirectory = folderName openFileDialog1.FileName = nothing openMenuItem.PerformClick() End If End If End Sub End Class






Re: Visual Basic Express Edition create,open,edit,save....text files

clint

whatsit.....lol

scratch head........edit

that looks even more confusing than the help menus.....but ta anyway






Re: Visual Basic Express Edition create,open,edit,save....text files

Whoisit

Clint
I know as it looks on the forum it appears to be confusing, but honestly if you copy and paste it you will see readable code.
Then if you step through it you will understand the concepts of reading, editing and saving.

Honest




Re: Visual Basic Express Edition create,open,edit,save....text files

clint

ok thanks

but I tried it and its all on one line

I think I'll just forget it.....

there must be an easier way to save a file

cheers

clint






Re: Visual Basic Express Edition create,open,edit,save....text files

clint

sorry but I am still looking for help on this

clint






Re: Visual Basic Express Edition create,open,edit,save....text files

Squire James

There are several ways to read and write text files, and some of them work better for certain uses than others. So explaining your intended use for the text file will help greatly when determining which way you want to go.

I prefer to use the StreamReader and StreamWriter classes. I also prefer to open a file only for reading or only for writing... if I need to edit a file, I read the whole thing into memory, make my changes, and write it back. This will not be the way to go if the text file is huge. I normally store the data in databases instead if it gets larger than a couple of meg.

To start reading a text file, first define the reader:

Dim Reader As New System.IO.StreamReader("filename.txt")

You can read one line at a time with the ReadLine method:

Dim Line as String = Reader.ReadLine()

Or read the whole text file as one big string with the ReadToEnd method:

Dim Text as String = Reader.ReadToEnd()

When done reading, dispose of the reader so all locks on the text file are released:

Reader.Dispose()

Writing text files works in a similar fashion with the StreamWriter class:

Dim Append as Boolean ' set to True to append to exisiting file, set to false to start new file or erase current one

Dim Writer As New System.IO.StreamWriter("filename.txt", Append)

Writer.WriteLine("Write a Line of Text")

Writer.WriteLine() ' an empty line

Writer.Close() ' closes, saves, and releases lock on text file

I typically open a StreamReader and put ReadLines in a loop to make a List or array of Strings that represent the lines of the file, close the StreamReaser, add/subtract/alter lines as needed, open a StreamWriter, and write out the new file. Obviously, there are a lot more ways to do this.

Dealing with binary files is harder, and dealing with binary files where you want to write variable-length data into the middle somewhere is even harder. I usually avoid doing that sort of thing nowadays.





Re: Visual Basic Express Edition create,open,edit,save....text files

clint

thank you squire james for your very clear explanation....I will study it some more, but for now I am not quite sure if

this is the way to go for what I am trying to achieve.....so

If I open a file as in the text below....ie..display text in textbox1

the text is editable......therefore, if I make some changes in that text.....

do I need to streamread/write......or can I not simply save textbox1.text as .....myText.txt ...and do a savefiledialog......if so

how would I go about it

sorry if I am sounding a bit stupid......but I dont understand ....if I can open the file with openfiledialog.....surely I can save it with

savefiledialog.....

if I open the file into textbox1...can I not save textbox1 to the file.....please explain

Private Sub Label17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label17.Click

If My.Computer.FileSystem.FileExists("c:\myText.txt") Then

TextBox1.BringToFront()

TextBox1.Visible = True

TextBox1.Text = My.Computer.FileSystem.ReadAllText("c:\myText.txt")

Else

TextBox1.Text = "not found"

End If

End Sub






Re: Visual Basic Express Edition create,open,edit,save....text files

Tall Dude

Yes you can save it that way.

Private Sub Button1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.Click

Dim sfd As New SaveFileDialog

sfd.Filter = "Textfiles | *.txt"

sfd.InitialDirectory = "c:\"

sfd.FileName = "MyText"

If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then

My.Computer.FileSystem.WriteAllText(sfd.FileName, TextBox1.Text, False)

End If

End Sub






Re: Visual Basic Express Edition create,open,edit,save....text files

clint

Hi guys

I thank you all for the response.....and tall dude...

although I have not tried your code yet I know that it is what I was looking for....you are the tall dude

I nominate you for mvp status........hows that

I will post again when I have tried it out........

thanks again to you all

clint






Re: Visual Basic Express Edition create,open,edit,save....text files

clint

tall dude...MVP

that works like a dream.....thank you so much

clint






Re: Visual Basic Express Edition create,open,edit,save....text files

Tall Dude

Thanks for the kind words,

but my overall knowledge is a long

way from being a MVP.






Re: Visual Basic Express Edition create,open,edit,save....text files

clint

tall dude

you under estimate yourself......I always look at your answers, and they are always spot on...

when I see a response from you I know it will work,....you have come to my aid a number of times

thanks again

clint