Coding Thread, Split into array ? in Coding and Web Development; VB .NET 2008 Express
Code:
Public Shared Function GetPrintersCollection() As StringCollection
Dim printerNameCollection As New StringCollection()
Dim searchQuery As String ...
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 ?
Last edited by mac_shinobi; 13th August 2009 at 10:02 AM.
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 Function
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 Function
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.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
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 Function
Then this code
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.