Developing a wizard

Previous chapterNext chapter Show allShow all    Hide allHide all

This tutorial develops a wizard creating variables for a defined driver.

Start the VBA environment from the zenon Editor and change to folder ZWorkspace/Forms. This file contains the basics for developing a wizard. Change the name of the UserForm.

If the folder mentioned above is not available, you can import it via the command Import file.

 

info Info

For developing a wizard knowledge about the object model of zenon and VBA are required. These topics are not part of this tutorial.

Create the surface displayed above. Then switch to the code module of the UserForm and scroll to the end of the file. There you will find the following methods.

 

These methods provide the information about the wizard, which is requested by the control system. Keep in mind that the wizard is only displayed in the wizard selection if the method IsZenOnWizard returns True.

 

Switch to the event Initialize of the UserForm and change the contents of the string array m_strCaption. As oru wizard only consits of two steps, you can delete the other allocations.

 

Add the following definitions to the top area of the code module:

Private m_obDriver As Driver
Private m_obVarType As VarType
Private m_nChannelType As Integer

 

Create a method for initializing the driver combobox. The task of this routine is to display all the loaded drivers of the current project in a combobox.

cbDriver.Clear
Dim nIndex As Long
For nIndex = 0 To MyWorkspace.ActiveDocument.Drivers.Count - 1
   Dim obDriver As Driver
   Set obDriver = MyWorkspace.ActiveDocument.Drivers.Item(nIndex)
   If (Not obDriver Is Nothing) Then
      cbDriver.AddItem
      obDriver.Name
   End If
Next nIndex
If (cbDriver.ListCount > 0) Then
   cbDriver.ListIndex = 0
End If

 

Additionally we need a routine displaying all defined variable types of the project in a combobox.

If (Not m_obDriver Is Nothing) Then
cbVarType.Clear
Dim nIndex As Long , nSelect As Integer
For nIndex = 0 To MyWorkspace.ActiveDocument.VarTypes.Count - 1
Dim obVarType As VarType
Set obVarType = MyWorkspace.ActiveDocument.VarTypes.Item(nIndex)
If ( Not obVarType Is Nothing And obVarType.IsSimple = True) Then
cbVarType.AddItem
obVarType.Name
If (obVarType.Name = INT) Then
   nSelect = nIndex
End If
End If
Next nIndex
cbVarType.ListIndex = nSelect
End If

 

On opening the wizard all existing variables are checked to find a free start offset for the the new variables to be created. This is done with the following method.

Private Function FindHighestOffsetVar() As Long
On Error GoTo Error
Dim nIndex As Long , nOffset As Long
For nIndex = 0 To MyWorkspace.ActiveDocument.Variables.Count - 1

Dim obVar As Variable
   Set obVar = MyWorkspace.ActiveDocument.Variables.Item(nIndex)
   If ( Not obVar Is Nothing) Then
   If (obVar.Offset > nOffset) Then
         nOffset = obVar.Offset
      End If
   End If
Next nIndex
FindHighestOffsetVar = nOffset
Exit Function
Error : MsgBox
Error occurs: + Err.Description + Source + Err.Source
End Function

 

Switch to the event Initialize of the UserForm and add the following lines to this method:

txtStart.Value = CStr(FindHighestOffsetVar + 1)

InitializeDriver

 

The allocation to txtStart sets the proposed start offset for the variables to be created. The method InitializeDriver fills the combobox with the existing drivers.

Create an event Change for the driver combobox and add the following code. After having selected a driver the variable types are acquired. The selected driver object is stored in the variable m_obDriver for later use.

Private Sub cbDriver_Change()
cmdNext.Enabled = True
Set m_obDriver = MyWorkspace.ActiveDocument.Drivers.Item(cbDriver.Value)
If ( Not m_obDriver Is Nothing) Then
   InitializeVarType
End If
End Sub

 

Create an event Change for the variable type combobox and add the following code. The selected variable type is stored in the variable m_obVarType for later use.

Private Sub cbVarType_Change()
Set m_obVarType = MyWorkspace.ActiveDocument.VarTypes.Item(cbVarType.Value)
End Sub

 

Now the only thing left is to create the event routine for creating the variables with the defined settings. This is done with the button Finish.

Private Sub cmdFinish_Click()
On Error GoTo Error
If (cbVarType.ListIndex = -1) Then
MsgBox 'Please select a variable type'
cbVarType.SetFocus
Exit Sub
End If
If (txtStart.Value = Or txtCount.Value = Or txtStep.Value = ) Then
MsgBox 'Please enter Start-Offset', 'count of creating variables and the step'
txtStart.SetFocus
End If
If (m_obVarType Is Nothing) Then
MsgBox 'Variable type + cbVarType.Name + doesnt exist!'
Exit Sub
End If
Dim nPrvMousePtr As Integer
nPrvMousePtr = MousePointer
MousePointer = fmMousePointerHourGlass
DoEvents
Dim strName As String
Dim nIndex As Long , nVarIndex As Integer
Dim nStartOff As Long , nStep As Integer
nVarIndex = 1
nStartOff = CLng (txtStart.Value)
nStep = CLng (txtStep.Value)
For nIndex = 0 To CLng (txtCount.Value - 1)
Dim obVar As Variable
strName = txtName.Value + _ + CStr (nIndex + 1)
'*** Guaranteeing uniqueness of the variable name
Dim bResult As Boolean
bResult = False
Do
Set obVar = MyWorkspace.ActiveDocument.Variables.Item(strName)
If (obVar Is Nothing) Then
bResult = True
Else
nVarIndex = nVarIndex + 1
strName = txtName.Value + _ + CStr (nVarIndex)
End If
Loop While
bResult = False
'*** Create variable
Set obVar = MyWorkspace.ActiveDocument.Variables.CreateVar (strName, m_obDriver, tpSPSMerker, m_obVarType)
If ( Not obVar Is Nothing) Then
obVar.Offset = nStartOff
nStartOff = nStartOff + nStep
End If
Next nIndex
MousePointer = nPrvMousePtr
Unload Me
Exit Sub
Error :
MousePointer = nPrvMousePtr
MsgBox Error occurs: + Err.Description + Source + Err.Source
End Sub

 

On finishing the wizard it is checked, if the defined settings are valid. If this is not the case, a messages is displayed and the user is demanded to correct the entries.

If the defined settings are valid, the variables are created. The variables are named with a name and an index. If a variables with the same name already exists in the project, the next free index is acquired. In our code example always a variable with the channel type PLC marker is created. With each cycle the offset of the variable is increased.