0

这基本上是我所拥有的:

Public checkprogresstime_p1 As String = ""
Public checkprogresstime_p2 As String = ""

'P1 Progress bar updater
checkprogresstime_p1 = (time_total.Text - time_p1_hour.Value)
If checkprogresstime_p1 >= 60 Then
    checkprogresstime_p1 = 60
    time_p1_progress.ForeColor = Color.LimeGreen
ElseIf checkprogresstime_p1 <= 0 Then
    checkprogresstime_p1 = 1
End If
If time_p1_progress.Value < 60 Then
    time_p1_progress.ForeColor = Color.Red
End If
time_p1_progress.Value = checkprogresstime_p1

这基本上是我需要的:

Dim cnt As Integer = 1     

Do
    'P1 Progress bar updater
    checkprogresstime_p(cnt) = (time_total.Text - time_p(cnt)_hour.Value)
    If checkprogresstime_p(cnt) >= 60 Then
        checkprogresstime_p(cnt) = 60
        time_p(cnt)_progress.ForeColor = Color.LimeGreen
    ElseIf checkprogresstime_p(cnt) <= 0 Then
        checkprogresstime_p(cnt) = 1
    End If
    If time_p(cnt)_progress.Value < 60 Then
        time_p(cnt)_progress.ForeColor = Color.Red
    End If
    time_p(cnt)_progress.Value = checkprogresstime_p(cnt)
Loop While cnt <= 25

我不知道该怎么做......我需要它循环并添加 +1、25 次。我现在基本上已经写了25次了......

4

2 回答 2

1

这是For/Loop您当前的要求。cnt变量将在这种类型的Loop.

For cnt As Integer = 1 To 25
 'P1 Progress bar updater
 checkprogresstime_p(cnt) = (time_total.Text - time_p(cnt)_hour.Value)
 If checkprogresstime_p(cnt) >= 60 Then
    checkprogresstime_p(cnt) = 60
    time_p(cnt)_progress.ForeColor = Color.LimeGreen
 ElseIf checkprogresstime_p(cnt) <= 0 Then
    checkprogresstime_p(cnt) = 1
 End If
 If time_p(cnt)_progress.Value < 60 Then
    time_p(cnt)_progress.ForeColor = Color.Red
 End If
 time_p(cnt)_progress.Value = checkprogresstime_p(cnt)
Next
于 2013-09-14T02:16:56.267 回答
0

我相信你想要做的更多的是在你的表单上有 25 个进度条,每个进度条都被命名,进度条的编号是time_p#_progress哪里#。话虽如此,有两种方法可以更新进度条,而无需复制和粘贴代码 25 次...

1.Me.Controls用于获取进度条的引用

For j = 1 To 25
    Dim pbar As ProgressBar = Me.Controls("time_p" & j & "_progress")
    Dim ph As NumericUpDown = Me.Controls("time_p" & j & "_hour")
    Dim checkprogresstime As Long = (time_total.Text - ph.Value)
    If checkprogresstime >= 60 Then
        checkprogresstime = 60
        pbar.ForeColor = Color.LimeGreen
    ElseIf checkprogresstime <= 0 Then
        checkprogresstime = 1
    End If
    If time_p1_progress.Value < 60 Then
        pbar.Value = checkprogresstime
    End If
    pbar.Value = checkprogresstime
    Application.DoEvents()
Next

注意:你没有告诉我们是什么类型的控制time_p1_hour。我认为这是一个NumericUpDown向下控制。因此,如果不是,则需要将其替换为控件类型time_p1_hour

2.动态创建控件作为控件数组

在 Form1_Load 方法 (MyBase.Load) 中初始化进度条

Private pbars(24) As ProgressBar

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For i = LBound(pbars) To UBound(pbars)
        pbars(i) = New ProgressBar()
        pbars(i).Parent = Me
        pbars(i).Top = i * pbars(i).Height
        pbars(i).Left = 0
        pbars(i).Visible = True
    Next
End Sub

像这样将您的代码放入循环中

For cnt = 0 To 24
    checkprogresstime_p(cnt) = (time_total.Text - time_hour(cnt).Value)
    If checkprogresstime_p(cnt) >= 60 Then
        checkprogresstime_p(cnt) = 60
        time_p_progress(cnt).ForeColor = Color.LimeGreen
    ElseIf checkprogresstime_p(cnt) <= 0 Then
        checkprogresstime_p(cnt) = 1
    End If
    If time_p_progress(cnt).Value < 60 Then
        time_p_progress(cnt).ForeColor = Color.Red
    End If
    time_p_progress(cnt).Value = checkprogresstime_p(cnt)
Next
于 2013-09-14T02:35:59.220 回答