0

我有一个由项目和客户两项组成的菜单条。根据用户的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当我单击项目项并单击添加项目或修改项目时,两者都将显示添加项目的形式。

4

1 回答 1

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
于 2013-05-11T13:25:19.047 回答