简体   繁体   English

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

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

In my previous question: How to know if the file I'm opening is a .txt file or not in VB.net 在我之前的问题中: 如何知道我要打开的文件在VB.net中是否是.txt文件

I ask here how to know if I'm opening .txt file or not. 我在这里问如何知道我是否正在打开.txt文件。

The code below is my code for opening a .txt file and prompt the user if the file is .txt of not. 下面的代码是我的代码,用于打开.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

If the file that I selected is not .txt file, here is the output: 如果我选择的文件不是.txt文件,则输出如下:

在此处输入图片说明

If I open a file that is not existing, here is the output: 如果打开不存在的文件,则输出为:

在此处输入图片说明

In the 1st image, it only show the error message box, but in the 2nd image, the error message box is within the open file dialog. 在第一个图像中,它仅显示错误消息框,但是在第二个图像中,错误消息框位于打开文件对话框中。

My question is how can I show the error message box of the 1st image with the open file dialog? 我的问题是如何在打开文件对话框中显示第一个图像的错误消息框?

Thank you. 谢谢。

Notes: 笔记:

  • No need to check the extension after you show the form, but you should instead set the appropriate filter in order to limit the selection of .txt files only "txt files (*.txt)|*.txt" 显示表单后无需检查扩展名,但您应该设置适当的过滤器,以仅选择"txt files (*.txt)|*.txt"限制.txt文件的选择。
  • You can use the OpenFileDialiog.CheckFileExists and OpenFileDialiog.CheckPathExists properties to prevent user to enter an invalid file name/path (display an error message) 您可以使用OpenFileDialiog.CheckFileExistsOpenFileDialiog.CheckPathExists属性来防止用户输入无效的文件名/路径(显示错误消息)
  • Not sure you need to check a second time if the file exists if you use CheckFileExists / CheckPathExists 如果使用CheckFileExists / CheckPathExists不确定是否需要再次检查文件是否存在
  • You should always dispose a form that you show using ShowDialog() method. 您应该始终处置使用ShowDialog()方法显示的表单。
  • You should dispose the StreamReader 您应该处置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

Here i add the label which is hidden. 在这里,我添加了隐藏的标签。 (name: pathlabel) button (open file) add openfiledialog from toolbox (名称:pathlabel)按钮(打开文件),从工具箱添加openfile对话框

this is so simple. 这很简单。 Open File button: 打开文件按钮:

openfiledialog.showdialog()

OpenFileDialog_FileOk : 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

Thank you... 谢谢...


Instead of this you must set the filter to openfiledialog button code (open file) 取而代之的是,您必须将过滤器设置为openfiledialog按钮代码(打开文件)

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

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

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