我有Richtextbox来输入具有多个字体名称,颜色,背景颜色和大小的文本。
我想保存它并使用相同的设置加载它。
Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
SaveFileDialog1.Filter = "TextFile (*.txt;*.rtf)|*.txt;*.rtf|Batch File (*.bat)|*.bat|All Files (*.*)|*.*"
SaveFileDialog1.FileName = "Untitled"
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim a() As String = SaveFileDialog1.FileName.Split("\")
Dim sw As New StreamWriter(SaveFileDialog1.FileName)
sw.Write(RichTextBox1.Text)
My.Settings.Save()
sw.Close()
End If
End Sub
答案 0 :(得分:2)
答案 1 :(得分:1)
您可以使用此代码进行阅读:
RichTextbox1.text = System.IO.File.ReadAllText(openfilepath)
这是写作:
System.IO.File.WriteAllText(Savefiledialog1.FileName, Richtextbox1.text)
希望它会有所帮助。
答案 2 :(得分:0)
“保存”和“另存为”之间有区别
保存代码
Private Sub SaveToolStripMenuItem_Click(sender As Object, e AsEventArgs) Handles SaveToolStripMenuItem.Click
If Me.Text = "Untitled" Then
Try
RichTextBox1.SaveFile(OpenFileDialog1.SafeFileName)
RichTextBox1.Modified = False
Catch ex As Exception
SaveAsToolStripMenuItem.PerformClick()
End Try
ElseIf Me.Text = OpenFileDialog1.SafeFileName Then
Try
RichTextBox1.SaveFile(OpenFileDialog1.FileName)
RichTextBox1.Modified = False
Catch ex As Exception
SaveAsToolStripMenuItem.PerformClick()
End Try
ElseIf Me.Text = SaveFileDialog1.FileName Then
Try
RichTextBox1.SaveFile(SaveFileDialog1.FileName)
RichTextBox1.Modified = False
Catch ex As Exception
SaveAsToolStripMenuItem.PerformClick()
End Try
End If
End Sub
另存为的代码
Private Sub SaveAsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveAsToolStripMenuItem.Click
Try
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
RichTextBox1.SaveFile(SaveFileDialog1.FileName)
Me.Text =SaveFileDialog1.FileName
RichTextBox1.Modified = False
End If
Catch ex As Exception
MsgBox("The file cannot be saved", MsgBoxStyle.Critical, "Save")
End Try
End Sub