如何更新每个循环中每个代码创建的进度条/标签?

时间:2014-12-06 22:17:44

标签: vb.net

首先是我的代码:

 Sub festplatte()
    Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
    Dim d As DriveInfo
    Dim I As Integer
    For Each d In allDrives
        If d.IsReady = True Then
            Try
                ' Intelize controls
                Dim progressbar As ProgressBar = New ProgressBar
                Dim pbgroup As GroupBox = New GroupBox
                Dim info As Label = New Label
                Dim type As Label = New Label
                Dim format As Label = New Label
                Dim Space As Double = d.TotalFreeSpace / d.TotalSize
                ' Add to tab
                Me.Tab_M.Controls.Add(progressbar)
                Me.Tab_M.Controls.Add(pbgroup)
                Me.Tab_M.Controls.Add(info)
                Me.Tab_M.Controls.Add(type)
                Me.Tab_M.Controls.Add(format)
                ' Add to group
                pbgroup.Controls.Add(progressbar)
                pbgroup.Controls.Add(info)
                pbgroup.Controls.Add(type)
                pbgroup.Controls.Add(format)
                pbgroup.Text = d.Name & " |  Name : " & CheckName(d.VolumeLabel.ToString())
                pbgroup.Size = New System.Drawing.Size(600, 65)
                pbgroup.Location = New System.Drawing.Point(8, I * 70 + 40)
                ' format 
                format.AutoSize = True
                format.Name = "format" & I
                format.Location = New System.Drawing.Point(435, 36)
                format.Text = "Format : " & d.DriveFormat
                ' Typ
                type.AutoSize = True
                type.Name = "type" & I
                type.Location = New System.Drawing.Point(435, 16)
                type.Text = "Art    : " & GetDriverFormat(d)
                ' info
                info.Name = "info" & I
                info.Location = New System.Drawing.Point(6, 16)
                info.AutoSize = True
                ' Info Text :: 1000 MB Grenze
                If d.TotalFreeSpace > Math.Pow(1024, 3) Then
                    info.Text = "Benutzter Speicher beträgt : " & Math.Round((d.TotalSize - d.TotalFreeSpace) / btogb, 2) & " GB von " & Math.Round((d.TotalSize) / btogb, 2) & " GB (" & Math.Round(100 - Space * 100, 2) & " % )"
                Else
                    info.Text = "Benutzter Speicher beträgt : " & Math.Round((d.TotalSize - d.TotalFreeSpace) / btomb, 2) & " MB von " & Math.Round((d.TotalSize) / btomb, 2) & " MB (" & Math.Round(100 - Space * 100, 2) & " % )"
                End If
                'Progressbar
                progressbar.Size = New System.Drawing.Size(425, 23)
                progressbar.Location = New System.Drawing.Point(6, 32)
                progressbar.Name = "Memory" & I
                progressbar.Value = 100 - Space * 100

            Catch ex As Exception
                MsgBox(ex.ToString())
            End Try
            I += 1
        End If
    Next
End Sub

代码会生成一个分组框,每个分组框中都有一个进度条和3个标签,我想在其中编辑内容。

如何编辑标签的文字?我怎样才能从表单中添加/删除生成的元素?我尝试刷新但它不起作用

The Form

1 个答案:

答案 0 :(得分:2)

我尝试过这么快:

Dim LabelList As New List(Of Integer)

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim LBL As New Label
    LBL.Location = New Point(6, 23)
    LBL.AutoSize = True

    Me.Controls.Add(LBL)
    LabelList.Add(Me.Controls.Count - 1)
End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    Me.Controls(LabelList(0)).Text = "HELLO WORLD!"
End Sub

这将向表单添加标签,然后将其控件索引添加到LabelList。 LabelList(0)将获取第一个添加标签的索引。