⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 如何动态增加和删除菜单项
💻 FRM
字号:
VERSION 4.00
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   1455
   ClientLeft      =   2280
   ClientTop       =   3060
   ClientWidth     =   3165
   Height          =   2145
   Left            =   2220
   LinkTopic       =   "Form1"
   ScaleHeight     =   1455
   ScaleWidth      =   3165
   Top             =   2430
   Width           =   3285
   Begin VB.CommandButton CmdRemove 
      Cancel          =   -1  'True
      Caption         =   "Remove"
      Height          =   495
      Left            =   1800
      TabIndex        =   3
      Top             =   720
      Width           =   1215
   End
   Begin VB.CommandButton CmdAdd 
      Caption         =   "Add"
      Default         =   -1  'True
      Height          =   495
      Left            =   240
      TabIndex        =   2
      Top             =   720
      Width           =   1215
   End
   Begin VB.TextBox CaptionText 
      Height          =   285
      Left            =   840
      TabIndex        =   1
      Top             =   240
      Width           =   2175
   End
   Begin VB.Label Label1 
      Caption         =   "Caption"
      Height          =   255
      Left            =   240
      TabIndex        =   0
      Top             =   240
      Width           =   615
   End
   Begin VB.Menu mnuFile 
      Caption         =   "&File"
      Begin VB.Menu mnuFileList 
         Caption         =   "mnuFileList"
         Index           =   0
         Visible         =   0   'False
      End
      Begin VB.Menu mnuFileSep 
         Caption         =   "-"
         Visible         =   0   'False
      End
      Begin VB.Menu mnuFileExit 
         Caption         =   "E&xit"
      End
   End
End
Attribute VB_Name = "Form1"
Attribute VB_Creatable = False
Attribute VB_Exposed = False
Option Explicit

Private max_index As Integer
Private Sub CmdAdd_Click()
    ' Load the new menu item.
    max_index = max_index + 1
    Load mnuFileList(max_index)
    mnuFileList(max_index).Caption = CaptionText.Text
    mnuFileList(max_index).Visible = True
    CaptionText.Text = ""
    
    ' Make the separator visible.
    mnuFileSep.Visible = True
End Sub

Private Sub CmdRemove_Click()
Dim txt As String
Dim ctl As Menu

    ' Find the menu item with this caption.
    txt = CaptionText.Text
    CaptionText.Text = ""
    For Each ctl In mnuFileList
        If ctl.Caption = txt Then Unload ctl
    Next ctl
    
    ' Make the separator visible.
    If mnuFileList.Count < 2 Then _
        mnuFileSep.Visible = False
End Sub


Private Sub mnuFileExit_Click()
    Unload Me
End Sub


Private Sub mnuFileList_Click(Index As Integer)
    MsgBox "Selected item" & Str$(Index)
End Sub


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -