以编程方式将MS Access DB文件中的子菜单项添加到menustrip

时间:2017-12-06 10:35:09

标签: vb.net

我想以编程方式从 MS Access DB文件中加载一些存储的标题,并将它们作为子项目添加到MenuStrip到现有子菜单。

我使用这种方式加载我的数据库文件,我只是不知道如何将这些标题添加到我的Menustrip控件中作为子项...

Imports System.Data.OleDb

Public Class MainForm
    Public Provider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
    Public DataFile As String = "database.accdb"
    Public ConnString As String = Provider & DataFile
    Public MyConnection As OleDbConnection = New OleDbConnection
    Public DataReader As OleDbDataReader

    Public Sub ApplicationsMenu_Load()
        MyConnection.ConnectionString = ConnString
        MyConnection.Open()

        Dim str As String = "SELECT * FROM ApplicationsPaths"

        Dim cmd As OleDbCommand = New OleDbCommand(str, MyConnection)
        DataReader = cmd.ExecuteReader

        Try
            While DataReader.Read()
                'Add submenu items code here!!!
            End While
            MyConnection.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

进入另一个项目,我在下面使用这种方式将一些labels添加到FlowLayoutPanel控件中。我想做类似的事情...我只是错过了语法!!!

While DataReader.Read()
            Dim lbl As New Label()
            Dim count As Integer = StandardPaths_HiddenFlowLayoutPanel.Controls.OfType(Of Label)().ToList().Count
            lbl.Name = "Path_" & DataReader("TagNum").ToString
            lbl.Tag = DataReader("TagNum").ToString
            lbl.Text = DataReader("Path").ToString
            lbl.AutoEllipsis = True
            lbl.AutoSize = True
            lbl.TextAlign = ContentAlignment.MiddleRight
            StandardPaths_HiddenFlowLayoutPanel.Controls.Add(lbl)
        End While

修改

我在下面找到了将DB文件中的标题添加到MenuStrip的方法,但它将它们作为顶级菜单加载。

While DataReader.Read()
            Dim SubItems As New ToolStripMenuItem
            SubItems.Text = DataReader("AppName").ToString
            MainForm_MenuStrip.Items.Add(SubItems)
        End While

如何将它们作为项目添加到现有子菜单?我正在尝试MainForm_MenuStrip.DropDownItems.Add(SubItems),我得到'DropDownItems' is not a member of 'MenuStrip'

2 个答案:

答案 0 :(得分:0)

将text / menuitems /数据库文件中的任何内容加载到文本框,列表框或simillar控件中。使其不可见。然后在表单加载时,使用:

 For each item in listbox1.items
 'this is untested
 'for example, if your menustrip's item name is FILE then
 FileToolStripMenuItem.items.add(item)
 NEXT

这就是全部

答案 1 :(得分:0)

好的,我找到了!!!首先,我必须将子菜单的名称放在我要添加这些项目的位置,而不是MenuStrip控件的名称。然后,我不得不放DropDownItems.Add而不是Items.Add,因为我想将标题添加到下拉项中。

While DataReader.Read()
        Dim SubItems As New ToolStripMenuItem
        SubItems.Text = DataReader("AppName").ToString
        ApplicationsActions_MenuItem.DropDownItems.Add(SubItems)
    End While