VB .NET 2008 Express
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.Code: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
Any help please ?
Last edited by mac_shinobi; 13th August 2009 at 10:02 AM.
bump the thread
so because its a function could I just use the addrange property so in my case would be
Just tried that and am getting the following error message as per screen grabCode:lstPrinters.items.addrange(GetPrintersCollection())
Also its a stringcollection data type so not sure if that makes any difference
Last edited by mac_shinobi; 13th August 2009 at 11:11 AM.
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\Form 1.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\Form 1.vb 11 36 Printer_Management

Right I officially hate StringCollection, use this instead
List(Of String)
Code: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 FunctionFilthy VB, I had to fire up VS 2010 for thatCode:ListBox1.Items.AddRange(GetPrintersCollection().ToArray)
Edit: killed off above posts of mine as they were reproduced in quote form anyhow and they were also wrong, for shame![]()
![]()
Last edited by SYNACK; 13th August 2009 at 12:15 PM.
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.ListViewItemCollecti on)': Value of type '1-dimensional array of String' cannot be converted to 'System.Windows.Forms.ListView.ListViewItemCollect ion'.
'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\Form 1.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
Then this codeCode: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
Code:Dim mylist as List(of String) = GetPrintersCollection() For Each s as string in mylist ListView1.Items.Add(s) Next
Last edited by mac_shinobi; 13th August 2009 at 01:24 PM.
There are currently 1 users browsing this thread. (0 members and 1 guests)