在使用事件变量时从另一个类调用子过程

时间:2018-04-19 16:17:23

标签: vb.net

我有TreeView扩展事件的代码。我想将所有代码放到另一个具有公共共享子方法的类中并将其调回。我试过但是有一个变量" e"我不能将这个变量移动到另一个类,因为它属于它自己的事件参数。

如果你帮我解决我怎么办,我会很高兴。

我的代码:

@ManyToOne
@JoinColumn(name = "id_Student", referencedColumnName = "id", nullable = false)
private Student student;

我想将它移到另一个类(myOtherClass.vb),如;

   Private Sub treDir_BeforeExpand(sender As Object, e As TreeViewCancelEventArgs) Handles treDir.BeforeExpand
        Dim DrvIsReady As Boolean = (From d As DriveInfo In DriveInfo.GetDrives Where d.Name = e.Node.ImageKey Select d.IsReady).FirstOrDefault
        If (e.Node.ImageKey <> "Desktop" AndAlso Not e.Node.ImageKey.Contains(":\")) OrElse DrvIsReady OrElse Directory.Exists(e.Node.ImageKey) Then
            e.Node.Nodes.Clear()
            __subMain.AddChildNodes(e.Node, e.Node.Tag.ToString)
        ElseIf e.Node.ImageKey = "Desktop" Then
            e.Node.Nodes.Clear()
            Dim PublicDesktopFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)
            Dim CurrentUserDesktopFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            __subMain.AddChildNodes(e.Node, CurrentUserDesktopFolder)
            __subMain.AddChildNodes(e.Node, PublicDesktopFolder)
        Else
            e.Cancel = True
            MessageBox.Show("The CD or DVD drive is empty.", "Drive Info...", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

从我的第一个活动中调用它: (我想要的结构)

Public Shared Sub treDirExpand()
       Dim DrvIsReady As Boolean = (From d As DriveInfo In DriveInfo.GetDrives Where d.Name = e.Node.ImageKey Select d.IsReady).FirstOrDefault
        If (e.Node.ImageKey <> "Desktop" AndAlso Not e.Node.ImageKey.Contains(":\")) OrElse DrvIsReady OrElse Directory.Exists(e.Node.ImageKey) Then
            e.Node.Nodes.Clear()
            __subMain.AddChildNodes(e.Node, e.Node.Tag.ToString)
        ElseIf e.Node.ImageKey = "Desktop" Then
            e.Node.Nodes.Clear()
            Dim PublicDesktopFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)
            Dim CurrentUserDesktopFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            __subMain.AddChildNodes(e.Node, CurrentUserDesktopFolder)
            __subMain.AddChildNodes(e.Node, PublicDesktopFolder)
        Else
            e.Cancel = True
            MessageBox.Show("The CD or DVD drive is empty.", "Drive Info...", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
End Sub

1 个答案:

答案 0 :(得分:0)

只需将e作为其他类中的参数:

Public Shared Sub treDirExpand(e As TreeViewCancelEventArgs)

您可能必须添加引用(如果在另一个项目中)和/或使用Imports指令,以便编译器知道TreeViewCancelEventArgs的命名空间。