0

我正在尝试构建一个数据库驱动的 VB.Net 应用程序,该应用程序从数据库中提取注册帐户列表并在菜单中显示 throes 帐户的用户名,因此用户可以选择一个并打开一个新表单(他们在其中使用它)。

到目前为止,我所拥有的是 MDI 父窗口的构造函数

Public Sub New()

    InitializeComponent()

    Dim tsmi As New ToolStripMenuItem("Users", Nothing, AddressOf users_mousedown)
    MenuStrip1.Items.Add(tsmi)

End Sub

用户菜单的处理程序(其中SQLite_db是一个负责数据库user_class的类,并且是一个具有两个项目(用户名和密码)作为字符串的类。

Sub users_mousedown()

    Dim submenu As New ContextMenuStrip
    Dim database As New SQLite_db

    Dim user_list As New List(Of user_class)
    user_list = database.List_Users

    For Each user As user_class In user_list
        submenu.Items.Add(user.username, Nothing, AddressOf Open_new_window(user))
    Next

    submenu.Items.Add("Add new user", Nothing, AddressOf AddNew)
    submenu.Show(Control.MousePosition)

End Sub

我想要发生的是当用户单击上下文菜单时,会创建一个新的 MDI 子表单并传递用户中的数据,但是因为 AddressOf 不喜欢传递数据,所以这不起作用......

I've looked at delegates and landa expressions but don't think either of them do what I need, the other option is to make my own subclass of the ContextMenuStrip class, which 1) handles the clicks the way I want to and 2) sounds like a nightmare.

Before I launch into what I think will be a hell of a lot of work, am I missing something? is their a simple way to do what I want to do? or if not will sub-classing ContextMenuStrip work and if not any ideas as to what will (and if it will, any ideas as to how to start learning how to do that)

4

1 回答 1

1

A simple way to encapsulate user info is with a Helper Class, where you store the context info.


Public Class Question1739163
    Class HelperUserCall
        Public userId As String

        Sub New(ByVal id As String)
            userId = id
        End Sub

        Public Sub OnClick()
            MsgBox(Me.userId)
        End Sub
    End Class
    Private Sub Question1739163_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim t As New ToolStripMenuItem("Users", Nothing, AddressOf user_mousedown)
        MenuStrip1.Items.Add(t)
    End Sub

    Public Sub user_mousedown()
        Dim s As New ContextMenuStrip
        Dim a As HelperUserCall

        a = New HelperUserCall("u1")
        s.Items.Add(a.userId, Nothing, AddressOf a.OnClick)

        a = New HelperUserCall("u2")
        s.Items.Add(a.userId, Nothing, AddressOf a.OnClick)

        s.Items.Add("New User", Nothing, AddressOf add_new)
        s.Show(Control.MousePosition)
    End Sub

    Sub add_new()
        MsgBox("add new")
    End Sub
End Class

You could improve the helper class adding the reference to database in the constructor, and retrieving the user info when the user click into the option.

于 2009-11-16T08:11:54.287 回答