Working with control values in a collection

jrunowski

Member
Joined
Aug 23, 2006
Messages
15
Programming Experience
Beginner
During my form_load event I create a series of progressbars and labels for each server listed in a XML file. I figured out how to change the text property of the label controls using:

VB.NET:
For Each control In gbServerNames.Controls
'Check control name to update correct label
controlname = "lblServer" + serverarray(x) 
'Updates total count label to reflect select statement.
If control.Name = controlname.ToString Then
control.Text = TEMPcount
End If
Next control

That code works fine; it finds the correct label control and updates the text property. I have tried the same code for my progressbars except using control.value to update the progress bar value and i get " 'value' is not a member of 'System.Windows.Forms.Control' ". Does anyone know how i can access the value property of a control in this situation?
 
If you know the current Control instance is a ProgressBar you can use DirectCast:
VB.NET:
dim pbar as progressbar= directcast(ctrl, progressbar)
 
Thanks JohnH for the quick reply, im not really sure how directcast works or how i would use that code. Could you show me how i can define the directcast during my For Each loop and define the value of that progress bar?

Again thanks for the help
 
VB.NET:
Dim pb As ProgressBar
Dim index As Integer

For Each ctrl As Control In Me.Controls
    If TypeOf ctrl Is ProgressBar Then
        pb = DirectCast(ctrl, Progressbar)
        index = Array.IndexOf(myProgressBarArray, pb)

        'You now have a reference to the ProgressBar itself and its index in the array.
    End If
Next ctrl
 
Back
Top