Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 单击保存按钮后如何显示消息框错误/成功_Vb.net_Listbox_Save_Messagebox - Fatal编程技术网

Vb.net 单击保存按钮后如何显示消息框错误/成功

Vb.net 单击保存按钮后如何显示消息框错误/成功,vb.net,listbox,save,messagebox,Vb.net,Listbox,Save,Messagebox,我一直在网上搜索解决方案,但由于某种原因似乎找不到 我想在单击“保存”按钮时显示一个消息框,显示“保存成功”或错误消息 以下是迄今为止按钮的代码: Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveKeywords.Click IO.Directory.CreateDirectory("C:\TESTING") Dim w As

我一直在网上搜索解决方案,但由于某种原因似乎找不到

我想在单击“保存”按钮时显示一个消息框,显示“保存成功”或错误消息

以下是迄今为止按钮的代码:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveKeywords.Click

    IO.Directory.CreateDirectory("C:\TESTING")
    Dim w As New IO.StreamWriter("C:\TESTING\Keywords.txt")
    Dim i As Integer

    For i = 0 To ListBox1.Items.Count - 1
        w.WriteLine(ListBox1.Items.Item(i))
    Next
    w.Close()


End Sub

任何帮助都将不胜感激。谢谢。

您可以使用Try-Catch来检测程序中的错误,因此如果出现错误,它将发送错误消息。例如:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveKeywords.Click

Try 

IO.Directory.CreateDirectory("C:\TESTING")
Dim w As New IO.StreamWriter("C:\TESTING\Keywords.txt")
Dim i As Integer

For i = 0 To ListBox1.Items.Count - 1
    w.WriteLine(ListBox1.Items.Item(i))
Next

MessageBox.Show("Saved Successfully")

Catch ex as exception

Messagebox.show("Error when saving. " & ex.tostring, "Data error ")

Finally
w.Close()

End Try

End Sub
即使保存成功或出现错误,finally也将始终关闭streamwriter流

另一方面,我会以这种方式亲自写信给文件

Dim FileLocation as string = "C:\TESTING\Keywords.txt"

System.IO.File.WriteAllLines(FileLocation, ListBox1.ToArray, System.Text.Encoding.Default)

(上面一行未经测试,因此ListBox1.ToArray可能无法工作,但如果无法工作,则很容易修复)

理想情况下,单击按钮时应包含尽可能少的代码,并调用包含大部分代码的子函数或函数,如下图所示,但这不是问题所在

您应该尽可能多地检查代码中的错误-在您的情况下,检查目录是否已经存在

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveKeywords.Click
    SaveFile("C:\TESTING\Keywords.txt")
End Sub

Private Sub SaveFile(fileName As String)
    If Not Directory.Exists(Path.GetDirectoryName(fileName)) Then
        IO.Directory.CreateDirectory(path.GetDirectoryName(fileName))
    End If
    Dim w As New IO.StreamWriter()
    Dim i As Integer
    Try
        For i = 0 To ListBox1.Items.Count - 1
            w.WriteLine(ListBox1.Items.Item(i))

        Next
    Catch ex As Exception
        MessageBox.Show("Error saving File: " & fileName & vbCrLf & "Exception Details" & vbCrLf & ex)
    Finally
        w.Close()
End Sub

步骤1:实现一种检测问题的方法-添加一个Try-Catch;步骤2:处置StreamWriter;步骤3:显示catch中设置的错误消息,或者您怀疑的默认“保存成功”,ListBox1.ToArray将不会编译。但是,
ListBox1.Items.OfType(Of String).ToArray
将从列表框中的项创建一个字符串数组
WriteAllines
IEnumerable(字符串的)
很满意,因此出于您的目的,您可以使用
ListBox1.Items.Of(字符串的)