Add controls dynamically and set events

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hey all. I´m trying to take some files and organize them in a FlowLayoutPanel
Imagine this folder "C:Example"
In this folder exists many subfolders. For each subfolder in C:Example i want to add a FlowLayoutPanel in a principal FlowLayoutPanel. In each secondary FlowLayoutPanel I want to add a button for each file in SubFolder.
I´m using this method to get the files to don´t have troubles with exceptions.
Post# Private Sub LoadFiles()
Dim mainFolder As String
Dim driver = Path.GetPathRoot(Environment.SystemDirectory)
Dim dir As New System.IO.DirectoryInfo(driver + "Example")
Dim subdirs() As System.IO.DirectoryInfo = dir.GetDirectories
For Each item As System.IO.DirectoryInfo In subdirs
Here i need to create a backgoundworker

Dim flowpanel as new windows.forms.flowlayoutpanel
flowpanel.AutoSize = True
flowpanel.Size = New Size(100, 200)
flowpanel.BackColor = Color.Transparent
flowpanel.Visible = True
FlowLayoutPanel1.Controls.Add(flowpanel)
FlowLayoutPanel1 is the principal Panel and flowpanel is for the subfolders
Dim label As New Label
label.Text = item.Name
flowpanel.Controls.Add(label)
mainFolder = item.FullName
If mainFolder <> "" Then
Here will run the backgroundworker. As I said I need 1 for each subfolder
bgworker.RunWorkerAsync(mainFolder)

End If
Next
End Sub
This is the code I used for BackGroundWorker Events if i just want all files in the principal FlowLayoutPanel:Private Sub LoadFiles()
Dim mainFolder As String
Dim driver = Path.GetPathRoot(Environment.SystemDirectory)

mainFolder = driver + "Example"
If mainFolder <> "" Then
If bgw.IsBusy = True Then
Else
bgw.RunWorkerAsync(mainFolder)
End If

End If
Next


End Sub

#Region "Common Methods And Functions"

Private Sub GenerateSafeFolderList(ByVal folder As String)

If Not safeFullPaths.Contains(folder) Then
safeFullPaths.Add(folder)
End If

Dim Dirs As New Stack(Of String)
Dirs.Push(folder)

While Dirs.Count > 0
Dim Dir As String = Dirs.Pop

Try
For Each D As String In Directory.GetDirectories(Dir)

Dim dirInfo As New DirectoryInfo(D)
If (((dirInfo.Attributes And FileAttributes.Hidden) = 0) AndAlso _
((dirInfo.Attributes And FileAttributes.System) = 0)) Then
If Not safeFullPaths.Contains(D) Then
safeFullPaths.Add(D)
End If
End If
Dirs.Push(D)
Next

Catch ex As Exception
If safeFullPaths.Contains(Dir) Then
Dim indexToRemove As Integer = 0
For i As Integer = 0 To safeFullPaths.Count - 1
If safeFullPaths(i) = Dir Then
indexToRemove = i
Exit For
End If
Next

safeFullPaths.RemoveAt(indexToRemove)
End If
Continue While
End Try
End While

End Sub


Private Delegate Sub GenerateSafeFolderListDelegate(ByVal folder As String)


Private Function ReturnShortString(ByVal str As String, _
ByVal maxLength As Integer) _
As String

If str.Length <= maxLength Then
Return str
Else
Dim leftEndPosition As Integer = CInt((maxLength / 2) - 2.5)
Dim rightStartPosition As Integer = str.Length - (CInt((maxLength / 2) - 2.5))

Dim sb As New System.Text.StringBuilder

sb.Append(str.Substring(0, leftEndPosition) & " ... " & str.Substring(rightStartPosition))

Return sb.ToString
End If

End Function


Private Delegate Function ReturnShortStringDelegate(ByVal str As String, _
ByVal maxLength As Integer) _
As String

#End Region

Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) _
Handles bgw.DoWork

If e.Argument IsNot Nothing Then
Dim mainFolder As String = DirectCast(e.Argument, String)

With safeFullPaths
.Clear()
.Add(mainFolder)
End With

bgw.ReportProgress(0, "Getting Directory Information...")
GenerateSafeFolderList(mainFolder)

Dim totalCount As Integer = safeFullPaths.Count
Dim loopCount As Integer = 0
Dim mp3FilePathList As New List(Of String)

For i As Integer = 0 To totalCount - 1
For Each fi As String In My.Computer.FileSystem.GetFiles(safeFullPaths(i), _
FileIO.SearchOption.SearchTopLevelOnly, "*.lnk")
System.IO.Path.GetFileName(listbox1.Items(i).ToString)

mp3FilePathList.Add(fi)


mp3FilePathList.Add(GetTargetPath(fi))
Next



loopCount += 1

If loopCount Mod 7 = 0 Then
Dim pctDone As Double = (loopCount / totalCount) * 100

bgw.ReportProgress(CInt(pctDone), "Looking Through: " & _
ReturnShortString(safeFullPaths(i), 50))
End If
Next

mp3FilePathList.Sort()
e.Result = mp3FilePathList

End If


End Sub


Private Sub bgw_ProgressChanged(ByVal sender As Object, _
ByVal e As System.ComponentModel.ProgressChangedEventArgs) _
Handles bgw.ProgressChanged

If e.UserState IsNot Nothing Then

End If

End Sub


Private Sub bgw_RunWorkerCompleted(ByVal sender As Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
Handles bgw.RunWorkerCompleted



If e.Error IsNot Nothing Then
Exception was raised - handle it

ElseIf e.Result IsNot Nothing Then
Dim fileList As New List(Of String)
fileList = DirectCast(e.Result, List(Of String))

If fileList.Count > 0 Then
ListBox1.BeginUpdate()

For Each fi As String In fileList
listbox1.Items.Add(fi)
Next

ListBox1.EndUpdate()

listbox1.Enabled = True
Dim i As Integer = 0
For i = 0 To listbox1.Items.Count - 1
btn = New System.Windows.Forms.Button
btn.Width = 150
btn.Height = 60
Dim extension = System.IO.Path.GetExtension(listbox1.Items(i).ToString)
listbox1.Items.Add(extension)

Dim dir = GetTargetPath(listbox1.Items(i))
btn.Text = System.IO.Path.GetFileNameWithoutExtension(listbox1.Items(i).ToString)
btn.Name = dir
btn.Visible = True
btn.TextAlign = ContentAlignment.MiddleCenter
btn.FlatStyle = FlatStyle.Flat
btn.FlatAppearance.BorderSize = 0
btn.BackColor = Color.FromArgb(200, 200, 200)
btn.Font = New System.Drawing.Font("Segoe UI", 8)
btn.ForeColor = Color.Black
btn.TextImageRelation = TextImageRelation.ImageBeforeText
Try
btn.Image = Drawing.Icon.ExtractAssociatedIcon(dir).ToBitmap
Catch ex As Exception
Continue For
End Try

FlowLayoutPanel1.Controls.Add(btn)



Next

End If
End If

MsgBox("done")
End Sub






So, what I need is to get all subfolders like i showed in first CodeBlock and for each one create a backgroundworker and set 3 events. ProgressChanged, DoWork and finally RunWorkerCompleted. Then I think I will be able to do the rest.
Thanks for read.
Meu novo programa (Bloco de Notas) http://hyrokumata-app.blogspot.pt/2012/07/notepad-3-beta.html

View the full article
 
Back
Top