很多程序中左上角最常见的“打开”菜单怎么来实现?
要求点击后弹出一个窗口,可以对电脑中的文件进行浏览选中。也就是大部分程序点击“打开”按钮所实现的功能。

解决方案 »

  1.   

    添加CommonDialog控件,在打开按钮click事件中加入CommonDialog.ShowOpen代码即可
      

  2.   

    菜单编辑器 添加标题“打开”,名称open      代码中open_Click()事件
      

  3.   

    1.菜单栏加一个菜单"打开"
    2.在菜单所在的窗体上加一个commondialog控件
    3.在菜单"打开"的点击事件写commondialog.showopen
      

  4.   

    具体请参考MSDN里关于CommonDialog的介绍
      

  5.   

    '在设计时按 ctrl+e 键,打开 “菜单编辑器”。添加一个新菜单。标题“打开”、名称“open”
    '在设计时按 ctrl+ t 键, 勾选 microsoft Common Dialog control (sp3) 确定 ,向窗体添加
    '一个commondialong控件
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Private Sub open_Click()
    CommonDialog1.ShowOpen
    'ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件、打开一个目录、打印一个文件等等),并对外部程序有一定的控制。
    ShellExecute 0, "open", CommonDialog1.FileName, "", "", 5
    End Sub
      

  6.   

    用菜单编辑器编辑菜单,把"打开"的名称设为"Open",然后添加入控件"microsoft Common Dialog control (sp3)",且在程序中加入Commondialog控件,然后复制入如下代码:Private Sub Open_Click()
    On Error Goto cuowu        '设置错误处理
    Commondialog1.Filter = "*.*|*.*" '这里是文件过滤器
    Commondialog1.CancelError = True
    Commondialog1.ShowOpen
    Shell Commondialog1.FileName '这里可以处理选中的文件
    cuowu: '错误处理
    If Err.Number = 32755 then    '设置错误码过滤
    Msgbox "不能选择取消!",,"打开菜单项"
    Exit Sub
    End If
    Dim a as String
    a = Msgbox ("出现错误!错误码为:" & Err.Number & chr(13) & chr(10) & "要保存错误记录吗?",vbyesno,"错误")
    Select Case a
    Case vbyes
    Open App.Path & "\Error.log" for append as #1
    print #1,Err.Number
    Close #1
    End Select
    End Sub
      

  7.   

    用菜单编辑器编辑菜单,把"打开"的名称设为"Open",然后添加入控件"microsoft Common Dialog control (sp3)",且在程序中加入Commondialog控件,然后复制入如下代码:
    [code=VB]
    Private Sub Open_Click()
    On Error Goto cuowu        '设置错误处理
    Commondialog1.Filter = "*.*|*.*" '这里是文件过滤器
    Commondialog1.CancelError = True
    Commondialog1.ShowOpen
    Shell Commondialog1.FileName '这里可以处理选中的文件
    cuowu: '错误处理
    If Err.Number = 32755 then    '设置错误码过滤
    Msgbox "不能选择取消!",,"打开菜单项"
    Exit Sub
    End If
    Dim a as String
    a = Msgbox ("出现错误!错误码为:" & Err.Number & chr(13) & chr(10) & "要保存错误记录吗?",vbyesno,"错误")
    Select Case a
    Case vbyes
    Open App.Path & "\Error.log" for append as #1
    print #1,Err.Number
    Close #1
    End Select
    End Sub[/code]
      

  8.   

    Private Sub OpenOToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenOToolStripMenuItem.Click
            Dim openFileDlg As New OpenFileDialog()
            openFileDlg.DefaultExt = "*.txt"
            openFileDlg.Filter = "RTF文档(*.rtf)|*.rtf|文本文档(*.txt)|*.txt|所有文件(*.*)|*.*"
            openFileDlg.ShowReadOnly = True        Dim filename As String = Nothing
            If openFileDlg.ShowDialog() = Windows.Forms.DialogResult.OK And (openFileDlg.FileName.Length) > 0 Then
                filename = openFileDlg.FileName
                NewNToolStripMenuItem_Click(sender, e)
                CntChildForm -= 1
                Dim activeChild As ChildForm = Me.ActiveMdiChild
                If (Not activeChild Is Nothing) Then
                    Dim fileExt As String = Nothing
                    If filename.Trim <> String.Empty Then
                        activeChild.RichTextBox1.ResetText()
                        fileExt = System.IO.Path.GetExtension(filename).ToUpper
                        If fileExt = ".RTF" Then
                            activeChild.RichTextBox1.LoadFile(filename, RichTextBoxStreamType.RichText)
                        Else
                            activeChild.RichTextBox1.LoadFile(filename, RichTextBoxStreamType.PlainText)
                        End If
                        activeChild.Name = filename
                        activeChild.Text = filename
                    End If
                End If        End If
        End Sub