If you have never heard about Genesis32 SCADA package from Iconics, this is the right time to learn more about it before we continue. I shall wait right here...
Good, now you know what I am talking about.
We'll talk about GraphWorX32, this is HMI graphical display application. One of the control available in GraphWorX32 is drop-down list (Statefield). This is pretty cool drop-down menu, but values on the list have to be hardcoded during screen design and can't be updated at runtime.
At the same time GraphWorX32 supports VBA scripting language. So it's totally possible to update drop-down list from VBA code. This is how I do it:
' In order to update drop-down values we have to delete stateField object and re-create it again
Dim strStateConfiguration As String
Dim objFieldToUpdate As Object
Dim L, T, W, H
Dim Fnt As IFontDisp
Dim objFieldToUpdateText As GwxText
Dim objNewText As GwxText
Dim objNewStateField As Object
' StateField object attaches to existing text object, so we have to find text object first, assuming it has "StateField_txt" name
Set objFieldToUpdate = ThisDisplay.GetVisibleObjectFromName("StateField_txt")
' Get text objects position/size, we have to create new object with the same size
objFieldToUpdate.GetObjectDimensions2 L, T, W, H
' Cast text object to the GwxText type
Set objFieldToUpdateText = objFieldToUpdate
' Get text object font
Set Fnt = objFieldToUpdateText.Font
' Delete text object
' Create new text object, set the same position/size and font as original text object.
' We didn't store original text align, colors etc, so we just hardcode it
Set objNewText = ThisDisplay.CreateText(0, 0, "", TextAlignLeft, False, False, RGB(128, 128, 128), RGB(0, 0, 0), 0, LineNone, False, RGB(0, 0, 0), EdgeNone, False, "StateField_txt")
Set objNewText .Font = Fnt
objNewText.SetObjectDimensions L, T, W, H
' Here we have to create new drop-down list. Just assign couple values
strStateConfiguration = ""
strStateConfiguration = strStateConfiguration + "value1" & Chr(9) & "Drop-Down-Text1" & Chr(13) & Chr(10)
strStateConfiguration = strStateConfiguration + "value2" & Chr(9) & "Drop-Down-Text2" & Chr(13) & Chr(10)
' Creating StateField and attach it to text object. OPC tag string is hardcoded in this example
Set objNewStateField = ThisDisplay.CreateStateField("StateField_txt", "StateField_stat", True, True, False, 0, strStateConfiguration, "")
objNewStateField.dataSource = "<Path_to_my_OPC_Tag>"
There are a couple things to keep in mind:
- This code needs to be run in PreRuntimeStart event
- You can't run this code during runtime, only at design mode
- PreRuntimeStart event called only during transition from design mode to runtime mode
- If screen opens from another screen which already in runtime mode - PreRuntimeStart event won't be called
- Depending of the application it could be several scenarios how to force drop-down list update. For example: you could open screen in design mode and in DisplayLoad event switch it to runtime mode