This tutorial will show you one way of saving a text file from a Rich Text Box in a Windows Form. VB version.
Dealing with files in Visual Studio can be quite tricky. There are a number of ways to handle files, be it text or binary. There are a number of ways to open a file, create a file and edit a file.
In this tutorial, you will learn one way of saving text to a txt file. We will be using a Rich Text Box to input text, and then use a Menu Item to save to a txt file.
To begin, we will add a Menu Strip control to the form, and add a File > Save menu item (Just click to add), then we will also add a RichTextBox control. That's all we need. The Windows Form Designer generated code will look something like this:
| Private Sub InitializeComponent()
Me.MenuStrip1 = New System.Windows.Forms.MenuStrip Me.FileToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem Me.SaveToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem Me.RichTextBox1 = New System.Windows.Forms.RichTextBox Me.Label1 = New System.Windows.Forms.Label Me.MenuStrip1.SuspendLayout() Me.SuspendLayout() ' 'MenuStrip1 ' Me.MenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.FileToolStripMenuItem}) Me.MenuStrip1.Location = New System.Drawing.Point(0, 0) Me.MenuStrip1.Name = "MenuStrip1" Me.MenuStrip1.Size = New System.Drawing.Size(284, 24) Me.MenuStrip1.TabIndex = 0 Me.MenuStrip1.Text = "MenuStrip1" ' 'FileToolStripMenuItem ' Me.FileToolStripMenuItem.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.SaveToolStripMenuItem}) Me.FileToolStripMenuItem.Name = "FileToolStripMenuItem" Me.FileToolStripMenuItem.Size = New System.Drawing.Size(37, 20) Me.FileToolStripMenuItem.Text = "File" ' 'SaveToolStripMenuItem ' Me.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem" Me.SaveToolStripMenuItem.Size = New System.Drawing.Size(152, 22) Me.SaveToolStripMenuItem.Text = "Save" ' 'RichTextBox1 ' Me.RichTextBox1.Location = New System.Drawing.Point(12, 27) Me.RichTextBox1.Name = "RichTextBox1" Me.RichTextBox1.Size = New System.Drawing.Size(260, 210) Me.RichTextBox1.TabIndex = 1 Me.RichTextBox1.Text = "" ' 'Label1 ' Me.Label1.AutoSize = True Me.Label1.Location = New System.Drawing.Point(13, 244) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(262, 13) Me.Label1.TabIndex = 2 Me.Label1.Text = "For more .NET Tutorials, visit www.dotnettutorials.com" ' 'Form1 ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(284, 264) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.RichTextBox1) Me.Controls.Add(Me.MenuStrip1) Me.MainMenuStrip = Me.MenuStrip1 Me.Name = "Form1" Me.Text = "Saving to a Text File in VB" Me.MenuStrip1.ResumeLayout(False) Me.MenuStrip1.PerformLayout() Me.ResumeLayout(False) Me.PerformLayout() End Sub |
This is the code that is generated by Visual Studio when we design our form. Yours may look different.
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
Once we have added our controls to the form, we can start work on the code-behind.
To add an event handler to the menu item, simply double-click it like you would a button. We will add teh following logic:
| Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
Dim saveFile As New FileInfo("file.txt")
Dim writer As StreamWriter = saveFile.CreateText() writer.Write(RichTextBox1.Text) writer.Close() End Sub |
Try Server Intellect for Windows Server Hosting. Quality and Quantity!
In the above code, we are assigning a filename ourselves. This is not practical, and we will change this shortly. The code creates a new file with the StreamWriter, and then inputs the contents of our RichTextBox. We can improve this code by letting the user choose their own filename, and we can use an Input Box for this. We will change our code to the following:
| Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
Dim callBox As String = Microsoft.VisualBasic.Interaction.InputBox("Enter filename", "Filename", "file.txt", 200, 200)
Dim saveFile As New FileInfo(callBox)
Dim writer As StreamWriter = saveFile.CreateText() writer.Write(RichTextBox1.Text) writer.Close() End Sub |
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
This is much more practical, as it lets the user choose their own filename to save, which avoids overwriting. We still have problems, though. If Cancel is clicked on the InputBox, there is no filename for FileInfo to use, so it errors out. This isn't good, so we can use an IF statement and also TRY and CATCH for any other errors:
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
Dim callBox As String = Microsoft.VisualBasic.Interaction.InputBox("Enter filename", "Filename", "file.txt", 200, 200) Try
If callBox = "" Then
MessageBox.Show("You must enter a filename to save") Else
Dim saveFile As New FileInfo(callBox)
Dim writer As StreamWriter = saveFile.CreateText() writer.Write(RichTextBox1.Text) writer.Close() End If Catch ex As IOException
MessageBox.Show(ex.ToString()) Catch ex As Exception
MessageBox.Show(ex.ToString()) End Try End Sub |
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
This code is much more efficient, as it will catch any errors and notify the user. We also included the IF statement to let the user know that a filename is required.
The entire code-behind looks like this:
Imports System.IO Public Class Form1
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
Dim callBox As String = Microsoft.VisualBasic.Interaction.InputBox("Enter filename", "Filename", "file.txt", 200, 200) Try
If callBox = "" Then
MessageBox.Show("You must enter a filename to save") Else
Dim saveFile As New FileInfo(callBox)
Dim writer As StreamWriter = saveFile.CreateText() writer.Write(RichTextBox1.Text) writer.Close() End If Catch ex As IOException
MessageBox.Show(ex.ToString()) Catch ex As Exception
MessageBox.Show(ex.ToString()) End Try End Sub End Class |
Looking for the C#.NET 2008 Version? Click Here!
Looking for more .NET Tutorials? Click Here!