简体   繁体   中英

VB.NET File Explorer

I'm making a File Explorer in VB.NET. Everything is going fine except one thing. When you click the dynamically created label to "open" a folder, I need to get the value of the label (so I can set a variable to it.) I can't get the value because the label was dynamically create. Therefore the object doesn't exist. Here's my code:

Imports System.IO
Public Class Form1
    Dim Path As String
    Dim FolderCount = 0
    Dim FolderWidth = 128
    Dim FolderHeight = 128
    Dim WidthAndPadding = FolderWidth + 10
    Dim HeightAndPadding = FolderHeight + 10
    Dim FolderTitle As String
    Dim CombinedWidth
    Dim FolderRows = 0
    Dim OnNewLine = False
    Dim FolderTop = 0
    Dim FolderLeft = 0

Dim FullPath
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.AutoScroll = True
    Path = "C:\Program Files\"
    For Each Dir As String In Directory.GetDirectories(Path)
        CreateFolders(Dir)
    Next
End Sub
Public Sub CreateFolders(ByVal StrDirectory)
    FolderTitle = StrDirectory.Substring(Path.Length)
    Dim Folder As New Label
    FolderLeft = WidthAndPadding * FolderCount
    Folder.Left = FolderLeft
    Folder.Top = FolderTop
    Folder.Width = FolderWidth
    Folder.Height = FolderHeight
    Folder.Image = My.Resources.Folder
    Folder.TextAlign = ContentAlignment.BottomCenter
    If FolderTitle.Length > 15 Then
        FolderTitle = FolderTitle.Substring(0, 15) + "..."
    End If
    Folder.Text = FolderTitle
    Folder.Font = New Font("Arial", 9.5)
    FolderCount += 1
    CombinedWidth = FolderCount * WidthAndPadding
    If CombinedWidth >= Me.Width Then
        OnNewLine = False
        If OnNewLine = False Then
            FolderRows = FolderRows + 1
            FolderCount = 0
            CombinedWidth = 0
            Folder.Left = FolderCount * FolderWidth
            FolderTop = FolderRows * FolderHeight
            Folder.Top = FolderTop
            OnNewLine = True
            'FolderCount += 1
        End If
    End If
    Me.Controls.Add(Folder)
    AddHandler Folder.DoubleClick, AddressOf Folder_DoubleClick
    FullPath = Path + FolderTitle
End Sub

Private Sub Folder_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
    'MsgBox(Folder.Text)
End Sub

End Class

The event handler's sender parameter contains the label that generated the event.
You can cast it back to Label by writing CType(sender, Label) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM