我是初学者,尝试类似地复制微软的记事本,但我收到了这条消息。目前我正在尝试编写ToolStripMenu中的“新”项。摘要:到目前为止,如果用户按下“新建”,系统会提示“是否要保存对此文档所做的更改?”,他们可以选择“否”,清除文档中的信息,或“取消”,返回他们按计划回到了原来的位置。但是,按“是”会给出错误“ArgumentException未处理 - 空路径名称不合法”
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
Dim intResult As Integer = Nothing
Dim dlg As SaveFileDialog = New SaveFileDialog
dlg.Filter = "Text File (*.txt)|*.txt"
intResult = MessageBox.Show("Do you want to save the changes made to this document?", "Warning", MessageBoxButtons.YesNoCancel)
If intResult = Windows.Forms.DialogResult.Yes Then
RichTextBox1.SaveFile(dlg.FileName, RichTextBoxStreamType.RichText)
ElseIf intResult = Windows.Forms.DialogResult.No Then
RichTextBox1.Clear()
Else
Return
End If
End Sub
答案 0 :(得分:1)
FileName
为空。 SaveFileDialog已声明,但从未使用过。你的代码没有多大意义,所以你可以显示对话框......
Dim dlg As SaveFileDialog = New SaveFileDialog
dlg.Filter = "Text File (*.txt)|*.txt"
dlg.Title = "Choose Text File"
dlg.ShowDialog()
intResult = MessageBox.Show("Do you want to save the changes made to this document?", "Warning", MessageBoxButtons.YesNoCancel)
或者可以检查他们是否指定了FileName
,然后运行你的逻辑。
If dlg.FileName <> "" then
' do save logic '
End If