VB.NET でファイル エクスプローラーを作成しています。1つのことを除いて、すべてがうまくいっています。動的に作成されたラベルをクリックしてフォルダーを「開く」場合、ラベルの値を取得する必要があります (ラベルに変数を設定できるようにします)。ラベルが動的に作成されたため、値を取得できません。したがって、オブジェクトは存在しません。これが私のコードです:
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
クラス終了