This is a flow issue.

Ok, going try to explain this as best I can.

Right now what my program does is takes items that are checked in a Checked List Box and compares them to a specific registry key. Currently everything is working great however there are times that a driver might be installed on a system and there are no printer objects associated with it in which case I want to state so in the txtLog. However if you follow the logic below you will see what I am running into (Yes I am a beginner VB programmer). What happens is in the For Each statement is that the txtLog is overrun with the text from the Else statement because it is doing it For Each.

IE: In otherwords if I have 1 item selected in the listbox it checks all 3 of the printers installed against the currently installed drivers and comes back with:

Not in Use
Not in Use
Found It!

If I take out the else statement then all is well however there are times when someone might select an item in the listbox that does not have a printer associated with it in which case I would like for them to know what is going on.
Thanks.

Jim

VB Code:
  1. Dim prnCount As Integer
  2.         Dim strPrinter As String
  3.         Dim strlbText
  4.         Dim strDriver
  5.         envstrOS = Shell.ExpandEnvironmentStrings("%COMPUTERNAME%")
  6.         ' LISTS ALL OF THE PRINTER OBJECTS INSTALLED ON THE SYSTEM.
  7.         Dim RegKey As RegistryKey = Registry.LocalMachine.CreateSubKey( _
  8.         "SYSTEM\CurrentControlSet\Control\Print\Printers")
  9.         Dim SubKeys() As String = RegKey.GetSubKeyNames()
  10.         ' GOES THROUGH THE LIST OF PRINTERS ONE AT A TIME
  11.         If SubKeys.Length > 0 Then
  12.             For prnCount = 0 To SubKeys.Length - 1
  13.                 strPrinter = (SubKeys(prnCount))
  14.                 ' QUERIES THE REGISTRY KEY "PRINTER DRIVER" TO DETERMINE WHAT DRIVER A PRINTER OBJECT USERS.
  15.                 Dim DrvRegKey As RegistryKey = Registry.LocalMachine.OpenSubKey( _
  16.                 "SYSTEM\CurrentControlSet\Control\Print\Printers\" & strPrinter)
  17.                 Dim objPrinterDriver As Object = DrvRegKey.GetValue("Printer Driver")
  18.                 ' CHECKS AGAINST A CHECKED LISTBOX TO SEE IF ANY OF THE ITEMS CHECKED MATCH THE PRINTER DRIVER FOUND ABOVE
  19.                 For Each strDriver In lbDrivers.CheckedItems
  20.                     If strDriver = objPrinterDriver Then
  21.                         txtLog.AppendText(strPrinter & " uses " & strDriver)
  22.                         txtCombined.AppendText("\\" & envstrOS & "\" & strPrinter & "," & "\\" _
  23.                         & envstrOS & "\" & strPrinter & ControlChars.CrLf)
  24.                     Else
  25.                         ' --- HERE IS THE PROBLEM, WITH THE ELSE STATEMENT ---
  26.                         txtLog.AppendText("Driver " & strDriver & " is not in use by any printer object")
  27.                     End If
  28.                 Next
  29.             Next prnCount
  30.  
  31.         End If