需要帮助在循环序列中为dim添加数字? vb.net

时间:2013-09-14 01:36:20

标签: vb.net

这基本上就是我所拥有的:

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次......

2 个答案:

答案 0 :(得分:1)

这是您当前请求的For/Loopcnt变量将在此类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

答案 1 :(得分: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