Question Change Control Properties

bpeirson

Member
Joined
Dec 21, 2010
Messages
7
Location
United States
Programming Experience
1-3
I am just starting to get back into VB programming, and I'm looking for a more elegant solution to an application I'm developing for a production line.

I have a series of labels with manufacturing steps. The background color of the steps needs to change to green as the process steps through. Right now it is working as it should, it just seems a little too much like brute force programming.

Right now I'm using a Select statement to change the color of EACH label. The first portion of the Select statement is below. With this method I have to do this same thing for each case...

' Select Case ScreenStep.Value
' Case 0
' lblStep1.BackColor = Color.Gray
' lblStep1Text.BackColor = Color.Gray
' lblStep2.BackColor = Color.Gray
' lblStep2Text.BackColor = Color.Gray
' lblStep3.BackColor = Color.Gray
' lblStep3Text.BackColor = Color.Gray
' lblStep4.BackColor = Color.Gray
' lblStep4Text.BackColor = Color.Gray
' lblStep5.BackColor = Color.Gray
' lblStep5Text.BackColor = Color.Gray
' lblStep6.BackColor = Color.Gray
' lblStep6Text.BackColor = Color.Gray
' lblStep7.BackColor = Color.Gray
' lblStep7Text.BackColor = Color.Gray
' lblStep21.BackColor = Color.Gray
' lblStep21Text.BackColor = Color.Gray

Does anyone have a suggestion for a "cleaner" way to do this?
 
You should use arrays, for both your Colors and controls. Then you can do something like this:
VB.NET:
Array.ForEach(Me.stepControls, Sub(c) c.BackColor = Me.stepColours(ScreenStep.Value))
That basically says, for each control in the 'stepControls' array, take the Color from the 'stepColors' array at the index contained in ScreenStep.Value and assign it to that control.
 
You can iterate each control on the form and set the color within a simple loop. Don't quote me on the syntax, I'm not setting in front of my development pc, but it will look like this:

For Each ctrl As Control In Me.Controls
If TypOf(ctrl) Is Label Then
ctrl.BackgoundColor = Colors.Gray
End If
Next

You could further enhance the ability by placing the labels in a Groupbox where you could then have "For Each ctrl As Control In Me.GroupboxName.Controls".
That would limit the code to just the controls within the Groupbox.


If you are using any type of threading you will want to use a delegate to make the actual updates to the form controls. Something like ....

Public Delegate Sub delFormatControl(ctrl As Control, c As Color)
Public Sub FormatControl(ctrl As Control, c As Color)
If ctrl.InvokeRequired Then
Me.Invoke(New delFormatControl(AddressOf FormatControl), ctrl, c)
Else
ctrl.BackgroundColor = c
End If
End Sub


The For Each loop would then become.

For Each ctrl As Control In Me.GroupboxName.Controls 'GroupboxName is whatever name you give the groupbox
If TypOf(ctrl) Is Label Then
FormatControl(ctrl, Colors.Gray)
End If
Next

...... Delegate may be new to you, but if you are going to do much programming you need to know it. Big guess here, but you mentioned Production Line, so I'm guessing ME or Controls Engineer. Maybe even a PLC is involved. If that is all true OPC is going to be your best friend.
 
Manufacturing Engineer is right. Due to our demand I haven't been able to do any major development changes lately, but next week is extremely slow. I will try out these methods and post back what I end up with.

As far as OPC, I looked into tutorials and guides for how to implement in VB.Net. We do have SEVERAL OPC servers currently running on the server. The main control environment for the line HMIs is GE's Proficy iFix. I don't have a problem with the software, but the documentation for our setup is horrible and there is no one left here that really understands what is being done in the VBA. I'm slowly inching towards replacing the entire SCADA system with just VB.Net. One of our other MEs directed me to a software company called InGear which can interface directly with Allen Bradley hardware without the need for OPC. So far it's working as advertised and I didn't have to learn how to code an OPC client in VB.Net.
 
Back
Top