Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何使用OpenFileDialog_Vb.net_Winforms_Openfiledialog - Fatal编程技术网

Vb.net 如何使用OpenFileDialog

Vb.net 如何使用OpenFileDialog,vb.net,winforms,openfiledialog,Vb.net,Winforms,Openfiledialog,我有两个表单名为frmChooseDBase和frmMainfrmChooseDBase用于选择文件(数据库文件)。选择完数据库后,frmMain将加载从frmChooseDBase中选择的数据库。 我该怎么做?任何帮助。 以下是我的示例代码: Public Class frmChooseDBase Public sr As String Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Sys

我有两个表单名为
frmChooseDBase
frmMain
frmChooseDBase
用于选择文件(数据库文件)。选择完数据库后,
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

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


如果遇到问题或需要快速帮助,可以使用openFileDialog控件

您甚至不必使用控件:

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 
Dim ofd As OpenFileDialog=新建OpenFileDialog
ofd.DefaultExt=“txt”
ofd.FileName=“defaultname”
ofd.InitialDirectory=“c:\”
ofd.Filter=“所有文件|*.|文本文件|*.txt”
ofd.Title=“选择文件”
如果ofd.ShowDialog()DialogResult.Cancel,则
MsgBox(ofd.FileName)
如果结束

我想将从OpenFileDialog中选择的内容传递到connectionString。像这样,con.ConnectionString=“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Desktop\”&[从打开的文件中选择]&“;Extended Properties=Excel 8.0”确定,而不是获取文件的da名称。也许最好是在connectionString的DataSource中替换文件路径bcoz。选中dis,con.ConnectionString=“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=”&[这是我要粘贴路径的地方]&“;Extended Properties=Excel 8.0”@Fvcundo只要弹出一个消息框,看看文件名包含什么,我想它实际上可能包含完整的路径名。@Fvcundo别担心,伙计!
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