Example 4 - Setting variable values and displaying them in time format in zenon

Previous chapterNext chapter Show allShow all    Hide allHide all

Requirement:

A time should be saved in a doubleword process variable.

e.g.: 1230 (hours, minutes)

First we have to create a macro, which opens frmTimeSet.

With the object obElem we search for the variable linked to the clicked element.

Public Sub LeftClickUp_TimeSet (obElem As Element)
Dim i As Integer
For i = 0 To obElem.CountVariable - 1
frmTimeSet.cmbVariables.AddItem obElem.ItemVariable(i). Name
Next i
frmTimeSet.cmbVariables.Text = frmTimeSet.cmbVariables.List ( 0 )
frmTimeSet.Show
End Sub

The variable name now is written to a combobox of frmTimeSet, so the change event of the combobox cmbVariables is triggered.

info Info

The value (e.g. 0130) stands for the time (01:30), but in the variable only the value 130 is saved.

Therefore as many zeros are added to the value, until the value has four digits. (0130)

Now the four digit value is split (01, 30) and sent to the text fields.

Private Sub cmbVariables_Change()
Dim obVariable As Variable
Dim strValue As String
Set obVariable = thisProject.Variables.Item(cmbVariables.Text)

'variable exists
If obVariable Is Nothing Then
Exit Sub
End If
strValue = obVariable.Value
Do Until Len (strValue) = 4
strValue = '0' + strValue
Loop
tbHour.Value = Left$(strValue, 2)
tbMinute.Value = Right$(strValue, 2)
tbHour.SetFocus
tbHour.SelStart = 0
tbHour.SelLength = Len (tbHour.Text)
End Sub

The following procedure closes the window.

Private Sub cmdExit_Click()
Unload Me
End Sub

By clicking the set button the entered time value is written to the variable.

Private Sub cmdSet_Click()
Dim obVariable As Variable
Set obVariable = thisProject.Variables.Item(cmbVariables.Text)
'variable exists
If obVariable Is Nothing Then
Exit Sub
End If
If tbHour.Text = '' Or tbMinute.Text = '' Then
MsgBox ( 'Bitte geben Sie eine Uhrzeit ein!')
tbHour.SetFocus
tbHour.SelStart = 0
tbHour.SelLength = Len (tbHour.Text)
Exit Sub
End If
obVariable.Value = tbHour.Text + tbMinute.Text

Unload Me
End Sub

In the text field tbHour only values between 0 and 23 can be entered.

Private Sub tbHour_Change()
Dim bIsNum As Boolean
Dim obVariable As Variable
Dim strValue As String
Set obVariable = thisProject.Variables.Item(cmbVariables.Text)strValue = obVariable.Value
If tbHour.Text Like '[0-9]*' Then
bIsNum = True
Else bIsNum = False
If bIsNum = False Then
tbHour.Text = ''
If tbHour.Value = '' Then
Exit Sub
If tbHour.Value > 23 Or tbHour.Value < 0 Then
MsgBox ( 'Bitte nur Werte zwischen 0 u. 23 eingeben')
tbHour.Value = Left$(strValue, 2)
tbHour.SetFocustbHour.SelStart = 0
tbHour.SelLength = Len (tbHour.Text)
Exit Sub
End If
If Len (tbHour.Text) = 2 Then
tbMinute.SetFocustbMinute.SetFocus
tbMinute.SelStart = 0
tbMinute.SelLength = Len (tbMinute.Text)
End If
End Sub

On leaving the text field a leading 0 is added, if the entry has only one digit. (e.g.: 5 becomes 05)

Private Sub tbHour_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len (tbHour.Value) = 1 Then
tbHour.Value = '0' + tbHour.Value
End If
End Sub

In the text field 'tbMinute' only values between 0 and 59 can be entered.

Private Sub tbMinute_Change()
Dim obVariable As Variable
Dim bIsNum As Boolean
Dim strValue As String
Set obVariable = thisProject.Variables.Item(cmbVariables.Text)
strValue = obVariable.Value
If tbMinute.Text Like '[0-9]*' Then
bIsNum = True
Else
bIsNum = False
End If
If bIsNum = False Then tbMinute.Text = ''
If tbMinute.Value = '' Then Exit Sub
If tbMinute.Value > 59 Or tbMinute.Value < 0 Then
MsgBox ( 'Bitte nur Werte zwischen 0 u. 59 eingeben')
tbMinute.Value = Right$(strValue, 2)
tbMinute.SetFocus
tbMinute.SelStart = 0
tbMinute.SelLength = Len (tbMinute.Text)
Exit Sub
End If
If Len (tbMinute.Text) = 2 Then
cmdSet.SetFocus
End Sub

Also on leaving the text field minute a zero is added in front of a one digit value.

Private Sub tbMinute_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len (tbMinute.Value) = 1 Then
tbMinute.Value = '0' + tbMinute.Value
End If
End Sub

Now that the input of a time is possible, the value of the variable has to be displayed as a time (HH:MM) in the according element.

For this we create a new macro 'Draw_Time'.

'Draw_Time' is executed on each 'redraw' of the element.

First the according variable is determined from the selected element.

The background and text color of the element is determined.

Then a rectangle is drawn over the element with the draw object. (i.e. the original value no longer is visible)

Now the value of the variable is split and newly combined with a separator (:).

e.g.: 1230 = 12:30

The TextOut function writes the value to the rectangle.

Public Sub Draw_Time(obElem As Element, ByVal hdc As OLE_HANDLE)
Dim obDraw As Draw
Dim obVariable As Variable
Dim intCenter As Integer
Dim strValue As String
On Error Resume Next
obElem.Draw hdc
'variables from element
Set obVariable = obElem.ItemVariable(0)
Set obDraw = obElem.DrawApi
'set backcolor
obDraw.SetBkColor hdc, obElem.BackColor
'set textcolor
obDraw.SetTextColor hdc, obElem.ForeColor
'Rectangle
obDraw.FillSolidRect hdc, obElem.Left + 10, obElem.Top + 10,_
(obElem.Right - obElem.Left) - 20, (obElem.Bottom - obElem.Top) - 20,_
obElem.BackColor
intCenter = obElem.Right - obElem.Left
intCenter = intCenter / 2 - 20
strValue = obVariable.Value
Do Until Len (strValue) = 4
strValue = '0' + strValue
Loop
strValue = Left$(strValue, 2) + ':' + Right$(strValue, 2)
'write time to element
obDraw.TextOut hdc, obElem.Left + intCenter, obElem.Top + 10, strValue
End Sub

So the variable value (1230) is always displayed in time format (12:30).