Contributed by: Bert Warren, Paper Free Technology

If you find that it takes some time to process a drop-down combo box with all attached repositories as you type in (change) the server name, you may solve this issue by using the following code to configure the background worker.

Creating a good usable login dialog was an early challenge for me. Without the background worker, every key press would start the process of checking for server and repository lists. Typing in the server name or adding ports to the found server name added a lot of time

I will show you how to use this method in a simple application that will only log in and out. Once you complete this project, you will be able to change the server text in the server combo box and populate the repository drop-down list without long delays between each key press or text change.

Beginning Steps

To start:

  • Open a new project in VB 2010.
  • Add the LFSO82 reference.
  • Add the following objects to the form:
  • Object Type – Name
  • Group Box – gbxLFLogin
  • ComboBox – cmbServ
  • ComboBox – cmbRepo
  • CheckBox – ckbWinAuth
  • Panel – pnlUser
  • TextBox – tbxUser
  • TextBox – tbxPassword
  • CheckBox – ckbScan
  • Button – btnLogIn
  • Button – btnLogOut

Here is what my form looks like (I have added labels):

Laserfiche Login Demo

After the form is built, we’ll start working on the code. First we need to add our Import Statements.

' needed for Laserfiche connection
Imports LFSO82Lib
' needed for BackGroundWorker
Imports System.ComponentModel

Laserfiche Login Demo Import

Then we will add our Global Objects.

' Global LFDatabase object set to nothing
' This object gets filled when logged in and
' reset to nothing when logged out.
Dim LFDB As LFDatabase = Nothing

Background Worker Code

Here is the commented code I use for the background worker to get the repository list attached to a given server:

' BackgroundWorker to get repository list
Private Sub bgwRepo_DoWork(ByVal sender As Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs)
' Create String array to hold list of repositories
Dim RepoList() As String = Nothing
Try
' Create new LFApplication object
Dim app As New LFApplication
' Create LFServer object for server in cmbServ text
Dim serv As LFServer = app.GetServerByName(e.Argument)
' Get collection of LF repositories
Dim Repos As ILFCollection = serv.GetAllDatabases
' Process the collection of repositories
If Repos.Count > 0 Then
For Each Repo As LFDatabase In Repos
' Resize string array to add new repository name
If RepoList Is Nothing Then
' Array is nothing, so create first slot
ReDim RepoList(0)
Else
' Add space to end of array while preserving its content
ReDim Preserve RepoList(RepoList.Count)
End If
' Add Repository name to last slot in array
RepoList(RepoList.Count - 1) = Repo.Name
Next
Else
' Server found with no repositories
If RepoList Is Nothing Then
' Array is nothing, so create first slot
ReDim RepoList(0)
End If
End If
Catch
RepoList = Nothing
End Try
' Return an array object containing the server name and the repository list
Dim objResult(1) As Object
' Load the Server name into the first slot of the returned array
objResult(0) = e.Argument
' load the repository list object into the 2nd slot of the returned array
objResult(1) = RepoList
' return the results array
e.Result = objResult
End Sub

Here is the code that allows the completed background worker to populate the drop-down combo box with the repository list.

' BackGroundWorker to update repository ComboBox DropDown
Private Sub bgwRepo_RunWorkerCompleted(ByVal sender As Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
Dim objResult() As Object = e.Result
' Get server name from objResult
Dim svr As String = DirectCast(objResult(0), String)
' Get Repository list from objResult
Dim ret() As String = DirectCast(objResult(1), String())
' Only proceed if Server name still matches
If svr = cmbServ.Text Then
' Only process if reurned list is not empty
If Not ret Is Nothing Then
' Clear repository ComboBox DropDown
cmbRepo.Items.Clear()
' Process each Repository name in list
For Each sRep As String In ret
' Add repository name to ComboBox DropDown
cmbRepo.Items.Add(sRep)
Next
' If ComboBox text is not in ComboBox DropDown change ComboBox text
If Not cmbRepo.Items.Contains(cmbRepo.Text) Then
' Set ComboBox text to first item in ComboBox DropDown
cmbRepo.Text = cmbRepo.Items.Item(0)
End If
Else
' Server has no availible repositories
' Clear repository ComboBox DropDown
cmbRepo.Items.Clear()
' Clear repository ComboBox text
cmbRepo.Text = ""
End If
End If
End Sub

To populate the server combo box list with all available servers, I used this code:

' Clear then repopulate Server list on ComboBox DropDown
Private Sub cmbServ_DropDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbServ.DropDown
' Remove all items from list
cmbServ.Items.Clear()
' Create new LFApplication object
Dim app As New LFApplication
' Get collection of LF servers
Dim Servers As ILFCollection = app.GetAllServers(ckbScan.Checked)
' Process the collection of servers
For Each Serv As LFServer In Servers
' Add each unique server name to the ComboBox DropDown
If Not cmbServ.Items.Contains(Serv.Name) Then cmbServ.Items.Add(Serv.Name)
Next
End Sub

Logging In and Out

Now, we’ll create some methods to log in and out of the repository. We’ll start with logging out:

Private Sub LFLogout()
' Only process if LFDatabase object is not nothing
If Not LFDB Is Nothing Then
Try
' Create LFConnection object from CurrentConnection property of LFDatabase object
Dim CON As LFConnection = LFDB.CurrentConnection
' Log Out
CON.Terminate()
Catch ex As Exception
' Log Errors Here
End Try
' Ensure LFDatabse object is nothing
LFDB = Nothing
End If
End Sub

My log-in method:

Private Sub LFLogin(ByVal sServer As String, ByVal sRepo As String, _
ByVal bWinAuth As Boolean, Optional ByVal sUser As String = _
"admin", Optional ByVal sPW As String = "")
' Only process if LFDatabase object is nothing
If LFDB Is Nothing Then
Try
' Create new LFApplication object
Dim app As New LFApplication
' Create LFServer object for server
Dim lfserv As LFServer = app.GetServerByName(sServer)
' Create LFConnection object
Dim CON As New LFConnection
' Set LFDatabase object
LFDB = lfserv.GetDatabaseByName(sRepo)
' Process Authentication
If bWinAuth Then
' Use blank User and Password for WinAuth
CON.UserName = ""
CON.Password = ""
Else
'Do not use empty user name for LF Auth (if that is what was passed)
If String.IsNullOrEmpty(sUser) Then
' Try using admin with blank password
sUser = "admin"
sPW = ""
End If
' Set LF Auth user and password
CON.UserName = sUser
CON.Password = sPW
End If
' Connect to the repository
CON.Create(LFDB)
Catch ex As Exception
' Log Errors Here
MessageBox.Show(ex.Message)
' Ensure LFDatabse object is nothing
LFDB = Nothing
End Try
End If
End Sub

A log-in Helper:

Private Sub LoginHelper()
Dim sUser, sPW As String
' Server name can not be blank
If String.IsNullOrEmpty(cmbServ.Text) Then
MessageBox.Show("Please provide LF Server Name or IP.")
' Exit the sub
Return
End If
' Repository Name can not be blank
If String.IsNullOrEmpty(cmbRepo.Text) Then
MessageBox.Show("Please provide LF Repository Name.")
' Exit the sub
Return
End If
' Process Authentication
If ckbWinAuth.Checked Then
' Use blank User and Password for WinAuth
sUser = ""
sPW = ""
Else
' Use provide User and Password for LFAuth
sUser = tbxUser.Text
sPW = tbxPassword.Text
End If
' Log into repository
LFLogin(cmbServ.Text, cmbRepo.Text, ckbWinAuth.Checked, sUser, sPW)
End Sub

Checkbox and Button Clicks

You can use this code to handle checkbox and button clicks:

Private Sub ckbWinAuth_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ckbWinAuth.CheckedChanged
If sender.Checked Then
pnlUser.Enabled = False
Else
pnlUser.Enabled = True
End If
End Sub
Private Sub btnLogIn_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnLogIn.Click
If LFDB Is Nothing Then
LoginHelper()
If Not LFDB Is Nothing Then
MessageBox.Show("Logged In Successfull!", "Log In", _
MessageBoxButtons.OK)
End If
Else
MessageBox.Show("Connection already exists.", "Log In", _
MessageBoxButtons.OK)
End If
End Sub
Private Sub btnLogOut_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnLogOut.Click
If LFDB Is Nothing Then
MessageBox.Show("Connection not found.", "Log Out", _
MessageBoxButtons.OK)
Else
LFLogout()
MessageBox.Show("Logged Out Successfull!", "Log Out", _
MessageBoxButtons.OK)
End If
End Sub

Form Events Code

And finally, included is code to handle form events:

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' You can preset login values if desired
Dim bScan As Boolean = True
Dim svr As String = "" ' Add server name here to prepopulate
Dim repo As String = "" ' Add repo name here to prepopulate
Dim bWinAuth As Boolean = False ' Use to prepopulate Win Auth
Dim usr As String = "" ' Add user name here to prepopulate
Dim pw As String = "" ' Add password here to prepopulate
' Process preset login values
ckbScan.Checked = bScan
If Not String.IsNullOrEmpty(svr) Then cmbServ.Text = svr
If Not String.IsNullOrEmpty(repo) Then cmbRepo.Text = repo
ckbWinAuth.Checked = bWinAuth
If Not String.IsNullOrEmpty(usr) Then tbxUser.Text = usr
If Not String.IsNullOrEmpty(pw) Then tbxPassword.Text = pw
End Sub
Private Sub Form1_FormClosing(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles MyBase.FormClosing
' Ensure no connection to Repository is left open
LFLogout()
End Sub

Some of the products and services listed on the Laserfiche Solution Exchange were not developed by Laserfiche. The recommendations and opinions expressed on the Laserfiche Solution Exchange are those of the person or persons posting the recommendations only, and they do not necessarily represent Laserfiche's opinion or recommendation of the product or service being reviewed. Laserfiche disclaims all liability resulting from your purchase or use of any non-Laserfiche software product or service listed on the site.