VLF .NET Snap-in Components - Developer's Guide | |
Create a Command Handler | |
Send comments on this topic. |
Glossary Item Box
This section shows how to create a VLFNET command handler.
The command handler will be snapped in for the Organization business object which is part of the HR Demo Application in the VLF demo framework:
1. Create a new C# or VB.NET class library project. Call it VLFHumanResources:
2. Open the Add New Item dialog box:
3. Choose VLF Command Handler, name it DepartmentDetails , and press the Add button.
A new VLF Command Handler is created and opened in the Designer:
4. Lay two labels (name them Label1 & Label2), two text boxes (name them TextBox1 & TextBox2) and a list box (name it ListBox1) on the command handler. Make Label1 Department Code: and Label2 Department Description:
5. View the events available to VLF .NET command handlers.
The avExecute event occurs after the component becomes visible. The avInitialize event occurs right after the component is instantiated and before it becomes visible.
Double click on the avExecute event to create a handler for that event.
Now you will need to write code to connect to the server, retrieve the details of the currently active department in the instance list from the server and populate the command handler with the details.
To access the details of the currently active entry in the instance list, you need to first get a reference to the list manager through the avListManager property of the command handler. Use avGetCurrentInstance method of the list manager to get the current entry.
You might have noticed by now the naming convention of the VLF specific properties and methods – they always start with the prefix ‘av’.
Use the method of your choice (LOpen.NET or ADO.NET) to fetch the required details off the server. Update the UI controls (textboxes, listboxes) with the details.
7. If you are using LOpen.NET (you need a valid license and LOpen.dll), copy this code to to your command handler, otherwise modify it as required.
The section How to get VLF.NET to notify my component when it’s shutting down will show you how to improve the code to gracefully close LOpen .NET sessions when the framework application shuts down.
You will need to modify the connection settings (in this sample they are for the listener on the iSeries).
Department Details Command Handler Source Code VB
Department Details Command Handler VB | Copy Code |
---|---|
Imports LOpen Imports System Public Class DepartmentDetails Private m_Session As Session Private m_SessionManager As SessionManager Private Sub EmployeeDetails_avExecute(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.avExecute ' Connect to a LANSA server using LOpen.NET m_SessionManager = SessionManager.GetInstance("ENG") m_Session = m_SessionManager.Session("Session1") If Not m_Session.Connected Then m_Session.ComputerName = "MyServer" m_Session.Partition = "DEM" m_Session.LanguageCode = "ENG" m_Session.PortNumber = "4545" m_Session.Connect(False, "MyUserName", "MyPassword") End If ' Get the currently selected instance Dim entry As VLF.ListEntry = Me.avListManager.avGetCurrentInstance() If entry IsNot Nothing Then Me.TextBox1.Text = entry.avVisualID2 Me.TextBox2.Text = entry.avVisualID1 ' Use LOpen.NET to retrieve associated sections Dim SectionTable As LOpen.Table = m_Session.Table("SECTAB") Dim sectionField As LOpen.Field = SectionTable.StringField("SECTION") Dim descField As LOpen.Field = SectionTable.StringField("SECDESC") Dim deptField As LOpen.Field = SectionTable.StringField("DEPTMENT") deptField.Value = entry.avVisualID2 Dim dt As DataTable = SectionTable.[Select](New LOpen.Field() {sectionField, descField}, New LOpen.Field() {deptField}, False, Nothing, False, False, _ False, False) ' Populate the section list box Me.ListBox1.BeginUpdate() Me.ListBox1.Items.Clear() For Each row As DataRow In dt.Rows Me.ListBox1.Items.Add(row("SECDESC")) Next Me.ListBox1.EndUpdate() End If End Sub End Class |
Department Details Command Handler Source Code C# (DepartmentDetails.cs)
Department Details Command Handler C# | Copy Code |
---|---|
using System; using System.Data; using LOpen; namespace VLFHumanResources { public partial class DepartmentDetails : VLF.CommandHandler { public DepartmentDetails() { InitializeComponent(); } private void DepartmentDetails_avExecute(object sender, EventArgs e) { // Connect to a LANSA server using LOpen.NET Session session = SessionManager.GetInstance("ENG").Session("Session1"); if (!session.Connected) { session.ComputerName = "MyServer"; session.Partition = "DEM"; session.LanguageCode = "ENG"; session.PortNumber = 4545; session.Connect(false, "MyUserName", "MyPassword"); } // Get the currently selected instance VLF.ListEntry entry = this.avListManager.avGetCurrentInstance(); if (entry != null) { this.TextBox1.Text = entry.avVisualID2; this.TextBox2.Text = entry.avVisualID1; // Retrieve associated sections LOpen.Table sectionTable = session.Table("SECTAB"); LOpen.Field sectionField = sectionTable.StringField("SECTION"); LOpen.Field descField = sectionTable.StringField("SECDESC"); LOpen.Field deptField = sectionTable.StringField("DEPTMENT"); deptField.Value = entry.avVisualID2; DataTable dt = sectionTable.Select( new LOpen.Field[] { sectionField, descField }, new LOpen.Field[] { deptField }, false, null, false, false, false, false); // Populate the section list box this.ListBox1.BeginUpdate(); this.ListBox1.Items.Clear(); foreach (DataRow row in dt.Rows) { this.ListBox1.Items.Add(row["SECDESC"]); } this.ListBox1.EndUpdate(); } } } } |
If this code generates error messages, ensure that you have a reference to LOpen.dll.
7. Save and build your project. Your command handler is now ready to be snapped in the Framework.
8. Copy the compiled assembly (VLHumanresources.dll) to your LANSA partition Execute directory (for example C:\Program Files\LANSA\X_WIN95\X_LANSA\x_dem\execute).