mac_shinobi Posted August 13, 2009 Posted August 13, 2009 (edited) VB .NET 2008 Express Public Shared Function GetPrintersCollection() As StringCollection Dim printerNameCollection As New StringCollection() Dim searchQuery As String = "SELECT * FROM Win32_Printer" Dim searchPrinters As New ManagementObjectSearcher(searchQuery) Dim printerCollection As ManagementObjectCollection = searchPrinters.[Get]() For Each printer As ManagementObject In printerCollection printerNameCollection.Add(printer.Properties("Name").Value.ToString()) Next Return (printerNameCollection) End Function I want to be able to add each of the printers to a listview but need to somehow split the data but because its a stringcollection I dont understand how to do it. Any help please ? Edited August 13, 2009 by mac_shinobi
mac_shinobi Posted August 13, 2009 Author Posted August 13, 2009 (edited) Windows Control: The List Box so because its a function could I just use the addrange property so in my case would be lstPrinters.items.addrange(GetPrintersCollection()) Just tried that and am getting the following error message as per screen grab Also its a stringcollection data type so not sure if that makes any difference Edited August 13, 2009 by mac_shinobi
mac_shinobi Posted August 13, 2009 Author Posted August 13, 2009 Actually as a stringcollection this should do the trick lstPrinters.items.addrange(GetPrintersCollection().ObjectCollection) VBForums - View Single Post - ListBox.AddRange Or possibly lstPrinters.items.addrange(GetPrintersCollection().ToArray()) ListBox.Items collection neither of those worked Error for the ObjectCollection property I get Error 1 'ObjectCollection' is not a member of 'System.Collections.Specialized.StringCollection'. C:\vb 2008 current\Printer_Management\Printer_Management\Form1.vb 11 36 Printer_Management Error for the ToArray property I get Error 1 'ToArray' is not a member of 'System.Collections.Specialized.StringCollection'. C:\vb 2008 current\Printer_Management\Printer_Management\Form1.vb 11 36 Printer_Management
SYNACK Posted August 13, 2009 Posted August 13, 2009 (edited) Right I officially hate StringCollection, use this instead List(Of String) Public Shared Function GetPrintersCollection() As List(Of String) Dim printerNameCollection As New List(Of String) Dim searchQuery As String = "SELECT * FROM Win32_Printer" Dim searchPrinters As New ManagementObjectSearcher(searchQuery) Dim printerCollection As ManagementObjectCollection = searchPrinters.[Get]() For Each printer As ManagementObject In printerCollection printerNameCollection.Add(printer.Properties("Name").Value.ToString()) Next Return (printerNameCollection) End Function [size=4][font=Consolas][size=4][font=Consolas][size=2][font=Consolas][size=4][font=Consolas][size=4][font=Consolas][size=4][font=Consolas][size=2]ListBox1.Items.AddRange(GetPrintersCollection().ToArray)[/size][/font] [/size][/font][/size][/font][/size][/font][/size][/font][/size][/font][/size] Filthy VB, I had to fire up VS 2010 for that Edit: killed off above posts of mine as they were reproduced in quote form anyhow and they were also wrong, for shame Edited August 13, 2009 by SYNACK
mac_shinobi Posted August 13, 2009 Author Posted August 13, 2009 (edited) Right I officially hate StringCollection, use this instead List(Of String) Public Shared Function GetPrintersCollection() As List(Of String) Dim printerNameCollection As New List(Of String) Dim searchQuery As String = "SELECT * FROM Win32_Printer" Dim searchPrinters As New ManagementObjectSearcher(searchQuery) Dim printerCollection As ManagementObjectCollection = searchPrinters.[Get]() For Each printer As ManagementObject In printerCollection printerNameCollection.Add(printer.Properties("Name").Value.ToString()) Next Return (printerNameCollection) End Function [size=4][font=Consolas][size=4][font=Consolas][size=2][font=Consolas][size=4][font=Consolas][size=4][font=Consolas][size=4][font=Consolas][size=2]ListBox1.Items.AddRange(GetPrintersCollection().ToArray)[/size][/font] [/size][/font][/size][/font][/size][/font][/size][/font][/size][/font][/size]Filthy VB, I had to fire up VS 2010 for that Edit: killed off above posts of mine as they were reproduced in quote form anyhow and they were also wrong, for shame Its a listview I am using not a listbox Error 1 Overload resolution failed because no accessible 'AddRange' can be called with these arguments: 'Public Sub AddRange(items As System.Windows.Forms.ListView.ListViewItemCollection)': Value of type '1-dimensional array of String' cannot be converted to 'System.Windows.Forms.ListView.ListViewItemCollection'. 'Public Sub AddRange(items() As System.Windows.Forms.ListViewItem)': Value of type '1-dimensional array of String' cannot be converted to '1-dimensional array of System.Windows.Forms.ListViewItem' because 'String' is not derived from 'System.Windows.Forms.ListViewItem'. C:\vb 2008 current\Printer_Management\Printer_Management\Form1.vb 11 9 Printer_Management I've tried C# but its too hard core for me at the min lol - I aint that good. Im trying to get to grips with when Im meant to use subs, functions, classes etc Getting confused with it a bit lol Nevermind solved your function as per here Public Shared Function GetPrintersCollection() As List(Of String) Dim printerNameCollection As New List(Of String) Dim searchQuery As String = "SELECT * FROM Win32_Printer" Dim searchPrinters As New ManagementObjectSearcher(searchQuery) Dim printerCollection As ManagementObjectCollection = searchPrinters.[Get]() For Each printer As ManagementObject In printerCollection printerNameCollection.Add(printer.Properties("Name").Value.ToString()) Next Return (printerNameCollection) End Function Then this code Dim mylist as List(of String) = GetPrintersCollection() For Each s as string in mylist ListView1.Items.Add(s) Next Edited August 13, 2009 by mac_shinobi
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now