繁体   English   中英

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

[英]How to include the message box within the Open file dialog in VB.net

在我之前的问题中: 如何知道我要打开的文件在VB.net中是否是.txt文件

我在这里问如何知道我是否正在打开.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文件,则输出如下:

在此处输入图片说明

如果打开不存在的文件,则输出为:

在此处输入图片说明

在第一个图像中,它仅显示错误消息框,但是在第二个图像中,错误消息框位于打开文件对话框中。

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

谢谢。

笔记:

  • 显示表单后无需检查扩展名,但您应该设置适当的过滤器,以仅选择"txt files (*.txt)|*.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

在这里,我添加了隐藏的标签。 (名称:pathlabel)按钮(打开文件),从工具箱添加openfile对话框

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

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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM