Hello! I'm new to VB.NET Forums. Well, I've lurked for awhile, just never posted anything. Sorry
. I am a novice, so please bear with me. I'm having some difficulty with a Windows form I'm working on and was hoping someone here could point me in the right direction, as I've been unable to figure this one out via the usual searches and Googles.
I'm attempting to pull all printers on the print server into a combobox. On select changed, I would like the property "location" to show in a label box below the combo (so the user knows where the printer in question is located in the building). On button click, I'd like to set the selected printer as default. All users on the system have access to the print server, so permissions should not be a concern.
I've managed to populate the combobox with the printer names (although they're all prefixed with DC-WALLY-, eg. DC-WALLY-HP_Printer_Name_Whatever). On button click, the exception is always thrown, so I'm doing something wrong. Also I can't seem to get the selectindexchanged to work.
Can anyone experienced with System.DirectorServices and/or System.Management lend a hand? Code is below. Thanks for your time.
Form1 contains 1 combobox (ComboBox1), 1 button (Button1), and 1 label (you guessed it, Label1)
--Form1.vb--
(note, I had tried the code below before, but it didn't work either:
)
I'm attempting to pull all printers on the print server into a combobox. On select changed, I would like the property "location" to show in a label box below the combo (so the user knows where the printer in question is located in the building). On button click, I'd like to set the selected printer as default. All users on the system have access to the print server, so permissions should not be a concern.
I've managed to populate the combobox with the printer names (although they're all prefixed with DC-WALLY-, eg. DC-WALLY-HP_Printer_Name_Whatever). On button click, the exception is always thrown, so I'm doing something wrong. Also I can't seem to get the selectindexchanged to work.
Can anyone experienced with System.DirectorServices and/or System.Management lend a hand? Code is below. Thanks for your time.
Form1 contains 1 combobox (ComboBox1), 1 button (Button1), and 1 label (you guessed it, Label1)
--Form1.vb--
VB.NET:
Imports System.DirectoryServices
Imports System.Management
Imports System.Data
Public Class Form1
Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Try
Dim de As New DirectoryEntry("LDAP://CN=DC-WALLY,OU=Domain Controllers,DC=biblioteca,DC=local")
Dim src As New DirectorySearcher("(&(objectCategory=printQueue))")
src.SearchRoot = de
src.SearchScope = SearchScope.Subtree
Dim res As SearchResult
For Each res In src.FindAll()
Me.ComboBox1.Items.Add(res.Properties("Name")(0))
Next res
Catch ex As Exception
Me.Label1.Text = "No printers found."
End Try
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'Not sure how to do this part
'On select index changed, show properties("location")(0) for selected printer name
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SetDefaultPrinter(Me.ComboBox1.SelectedIndex)
End Sub
Sub SetDefaultPrinter(ByVal strPrinter As String)
strPrinter = Me.ComboBox1.SelectedIndex
Dim wmi As ManagementClass
Dim obj As ManagementObject
Dim gotit As Boolean = False
wmi = New ManagementClass("\root\cimv2:Win32_Printer")
For Each obj In wmi.GetInstances
If obj("Name") = strPrinter Then
' The SetDefaultPrinter method is new to WinXP/Win2003
obj.InvokeMethod("SetDefaultPrinter", Nothing)
gotit = True
End If
Next
If Not gotit Then
MsgBox("Error: No printer by that name found.")
End If
End Sub
VB.NET:
'Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
' Dim wsh As Object
' Dim printer = Me.ComboBox1.SelectedItem
'
' Try
' wsh = CreateObject("WScript.Network")
' wsh.SetDefaultPrinter(printer)
' wsh = Nothing
' Me.Label1.Text = "Default Printer Set To " & printer
' Catch ex As Exception
' Me.Label1.Text = "Error!!"
' End Try
' End Sub