【问题标题】:How to include the message box within the Open file dialog in VB.net如何在 VB.net 的“打开文件”对话框中包含消息框
【发布时间】:2013-09-29 01:24:14
【问题描述】:

在我之前的问题中: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 文件,这里是输出:

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

在第一张图片中,它只显示错误消息框,但在第二张图片中,错误消息框在打开文件对话框中。

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

谢谢。

【问题讨论】:

  • 检查 OpenFileDialog.ShowDialog() 是否返回 OK。这意味着选择了一个有效的文件。您可以使用过滤器仅列出 .txt 文件 - msdn.microsoft.com/en-us/library/…

标签: vb.net messagebox openfiledialog


【解决方案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

【讨论】:

  • 啊,好吧,先生,我以前不知道,但是感谢您,现在我知道了,也感谢您提供的补充信息:)
  • @Matthew 很高兴我能帮上忙 :)
  • 只是补充一点,不幸的是,这并不能帮助您避免可能出现意外的用户输入,将过滤器设置为指定选项并且只允许该过滤器不会完全限制用户的文件选择,请尝试出来type "*" into the file name text box and hit enter, after that you will be able to select every type, regardless of the specified filter 好的“标准”用户会这样做,但如果你想超级安全地得到你所期望的,请记住这一点很有用
【解决方案2】:

在这里我添加了隐藏的标签。 (名称:路径标签) 按钮(打开文件) 从工具箱中添加 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"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-10
    • 2023-03-30
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 2010-12-11
    • 1970-01-01
    相关资源
    最近更新 更多