vb.net 如何使用 OpenFileDialog

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27854443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:07:31  来源:igfitidea点击:

How to use OpenFileDialog

vb.netwinformsopenfiledialog

提问by Fvcundo

I have two forms named frmChooseDBaseand frmMain. frmChooseDBaseis for choosing a file(a database file). Once he's done choosing the database, frmMainwill load the database chosen from frmChooseDBase. How can I do dat? any help. Here's my sample codes:

我有两个名为frmChooseDBase和 的表单frmMainfrmChooseDBase用于选择文件(数据库文件)。完成frmMain数据库选择后,将加载从frmChooseDBase. 我该怎么做?任何帮助。这是我的示例代码:

Public Class frmChooseDBase
    Public sr As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            sr = OpenFileDialog1.FileName
            Me.Hide()
            FrmMain.Show()
        End If
    End Sub
End Class

Private Sub FrmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Desktop\'" & frmChooseDBase.sr & "';Extended Properties=Excel 8.0"
        con.Open()


 FillDGView("SELECT [CCCD Loading Database] AS [Transaction Date], [F2] AS [Unit Number], [F3] AS [Category], [F4] AS [Temp Required (C)], [F5] AS [Type Length], [F6] AS [T-State], [F7] AS [Position], [F8] AS [I/B Actual Visit], [F9] AS [Fright Kind] FROM [Loading$]")

    End Sub

回答by Mathemats

If you just want to load a file, you don't need to make a new form. Just have a button or menu item that says load DB. Clicking that will pop an OpenFileDialog. Drag an openFileDialog control onto the form and give it a meaningful name (openFileDialog1...)

如果你只是想加载一个文件,你不需要创建一个新的表单。只需有一个按钮或菜单项,显示加载数据库。单击它会弹出一个 OpenFileDialog。将 openFileDialog 控件拖到窗体上并为其指定一个有意义的名称 (openFileDialog1...)

openFileDialog1.Title = "Please select a DB file"
openFileDialog1.InitialDirectory = "C:\"
openFileDialog1.Filter = "DB Files|*.extensionHERE"

If openFileDialog1.ShowDialog() = DialogResult.OK then
    'Do things here, the path is stored in openFileDialog1.Filename
    'If no files were selected, openFileDialog1.Filename is ""  
End If

There are plenty of examples for using the openFileDialog control if you get stuck, or need quick help.

如果您遇到困难或需要快速帮助,有很多使用 openFileDialog 控件的示例。

回答by hericson

You don't even have to use a control:

您甚至不必使用控件:

Dim ofd As OpenFileDialog = New OpenFileDialog
ofd.DefaultExt = "txt"
ofd.FileName = "defaultname"
ofd.InitialDirectory = "c:\"
ofd.Filter ="All files|*.*|Text files|*.txt"
ofd.Title = "Select file"
If ofd.ShowDialog() <> DialogResult.Cancel Then
    MsgBox(ofd.FileName)
End If