如何在VB.net中的“打开文件”对话框中包含消息框

时间:2013-09-24 00:51:14

标签: vb.net messagebox openfiledialog

在我之前的问题中:How to know if the file I'm opening is a .txt file or not in VB.net

我在这里问如何知道我是否打开.txt文件。

下面的代码是我打开.txt文件的代码,并提示用户该文件是否为.txt。

Dim filename As String = String.Empty
Dim TextLine As String = ""
Dim SplitLine() As String


Dim ofd1 As New OpenFileDialog()

ofd1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
ofd1.FilterIndex = 2
ofd1.RestoreDirectory = True
ofd1.Title = "Open Text File"

'get the filename of the txt file
If ofd1.ShowDialog() = DialogResult.OK Then
'if the file is not .txt file
        If (Path.GetExtension(filename).ToLower() <> ".txt") Then
            MessageBox.Show("Please select text Files only", _
                            "RMI", _
                             MessageBoxButtons.OK, _
                             MessageBoxIcon.Warning)

            'show the open file dialog
            ofd1.ShowDialog()

            'if the file is .txt file
        Else
            filename = ofd1.FileName
 End If

'if the filename is existing
If System.IO.File.Exists(filename) = True Then

    Dim objReader As New System.IO.StreamReader(filename)

    'read the text file and populate the datagridview
    Do While objReader.Peek() <> -1
        TextLine = objReader.ReadLine()
        TextLine = TextLine.Replace(" ", "")
        SplitLine = Split(TextLine, ",")
        dvList.Rows.Add(SplitLine)
    Loop

End If

如果我选择的文件不是.txt文件,则输出为:

enter image description here

如果我打开一个不存在的文件,这里是输出:

enter image description here

在第1张图片中,它只显示错误信息框,但在第2张图片中,错误信息框位于打开的文件对话框中。

我的问题是如何使用打开文件对话框显示第一张图像的错误消息框?

谢谢。

2 个答案:

答案 0 :(得分:1)

注意:

  • 在您显示表单后无需检查扩展名,但您应该设置相应的过滤器,以便仅限制.txt文件的选择"txt files (*.txt)|*.txt"
  • 您可以使用OpenFileDialiog.CheckFileExistsOpenFileDialiog.CheckPathExists属性阻止用户输入无效的文件名/路径(显示错误消息)
  • 如果您使用CheckFileExists / CheckPathExists
  • ,请确定您是否需要再次检查文件是否存在
  • 您应该始终使用ShowDialog()方法处理您显示的表单。
  • 您应该处置StreamReader

Dim filename As String = String.Empty
Dim TextLine As String = ""
Dim SplitLine() As String

Using ofd1 As New OpenFileDialog()
        ofd1.Filter = "txt files (*.txt)|*.txt"
        ofd1.FilterIndex = 2
        ofd1.CheckPathExists = True
        ofd1.CheckPathExists = True
        ofd1.RestoreDirectory = True
        ofd1.Title = "Open Text File"

        'get the filename of the txt file
        If ofd1.ShowDialog() = DialogResult.OK Then
            filename = ofd1.FileName

            Using objReader As New System.IO.StreamReader(filename)

                'read the text file and populate the datagridview
                Do While objReader.Peek() <> -1
                    TextLine = objReader.ReadLine()
                    TextLine = TextLine.Replace(" ", "")
                    SplitLine = Split(TextLine, ",")
                    dvList.Rows.Add(SplitLine)
                Loop
            End Using
        End If
End Using

答案 1 :(得分:0)

这里我添加了隐藏的标签。 (姓名:pathlabel) 按钮(打开文件) 从工具箱中添加openfiledialog

这很简单。 打开文件按钮:

openfiledialog.showdialog()

OpenFileDialog_FileOk:

PathLabel.Text = System.IO.Path.GetExtension(OpenFileDialog.FileName)
    If PathLabel.Text = ".txt" Then
        Dim Objectreader As New System.IO.StreamReader(OpenFileDialog.FileName)
        TextBox1.Text = Objectreader.ReadToEnd
        Objectreader.Close()
    Else
        MsgBox("please select only Text Document (.txt)", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error")
    End If

谢谢...


而不是这个,你必须将过滤器设置为openfiledialog 按钮代码(打开文件)

Openfiledialog.showdialog()
openfiledialog.filter = "Text Document|*.txt"