VB.NET中的Menustrip子项控件

时间:2013-05-11 12:27:54

标签: vb.net user-interface interface

我有一个menustrip,包含Project和Customer两个项目。根据用户的ID,每个项目将具有不同的子项目。但是,我面临的问题是如何将每个子项与特定表单连接起来。在我的搜索过程中,我做了以下工作:

Dim userid = Textbox1.text
if userid = "1001" then
                Dim p1 As New ToolStripMenuItem
                p1 = Form1.ProjectToolStripMenuItem
                p1.DropDownItems.Add("Add Project")
                AddHandler p1.DropDownItemClicked, AddressOf add_project_click
                Dim p2 As ToolStripMenuItem
                p2 = Form1.ProjectToolStripMenuItem
                p2.DropDownItems.Add("Modify Project")
else
                    Dim p2 As ToolStripMenuItem
                    p2 = Form1.ProjectToolStripMenuItem
                    p2.DropDownItems.Add("Modify Project")
end if
    Private Sub add_project_click(ByVal sender As Object, ByVal e As EventArgs)
        AddProjectForm.Show()

    End Sub

AddHandler p1.DropDownItemClicked, AddressOf add_project_click现在出现问题,因为当我单击“项目”项目并单击“添加项目”或“修改项目”时,两者都将显示“添加项目”的形式。

1 个答案:

答案 0 :(得分:1)

更改add_project_click方法的定义,以便e是ToolStripItemClickedEventArgs而不是EventArgs类型。然后,您可以使用e.SelectedItem.Text来确定单击了哪个项目。

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim userid = "1001"

    Dim p1 As New ToolStripMenuItem

    p1.Text = "Menu Item"

    If userid = "1001" Then

        p1.DropDownItems.Add("Add Project")

    End If

    p1.DropDownItems.Add("Modify Project")

    MyMenuStrip.Items.Add(p1)

    AddHandler p1.DropDownItemClicked, AddressOf add_project_click

End Sub


Private Sub add_project_click(ByVal sender As Object, ByVal e As ToolStripItemClickedEventArgs)

    If e.ClickedItem.Text = "Add Project" Then

        'AddProjectForm.Show()

    Else

        'ModifyProjectForm.Show()

    End If

End Sub