+ Post New Thread
Results 1 to 6 of 6
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 ...
  1. #1

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,677
    Thank Post
    3,209
    Thanked 1,030 Times in 955 Posts
    Rep Power
    361

    Split into array ?

    VB .NET 2008 Express

    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
    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.

  2. #2

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,677
    Thank Post
    3,209
    Thanked 1,030 Times in 955 Posts
    Rep Power
    361
    bump the thread

  3. #3

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,677
    Thank Post
    3,209
    Thanked 1,030 Times in 955 Posts
    Rep Power
    361
    Quote Originally Posted by SYNACK View Post
    so because its a function could I just use the addrange property so in my case would be


    Code:
    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
    Attached Images Attached Images
    Last edited by mac_shinobi; 13th August 2009 at 11:11 AM.

  4. #4

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,677
    Thank Post
    3,209
    Thanked 1,030 Times in 955 Posts
    Rep Power
    361
    Quote Originally Posted by SYNACK View Post
    Actually as a stringcollection this should do the trick

    Code:
    lstPrinters.items.addrange(GetPrintersCollection().ObjectCollection)

    VBForums - View Single Post - ListBox.AddRange

    Or possibly
    Code:
    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\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

  5. #5

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    10,986
    Thank Post
    850
    Thanked 2,653 Times in 2,253 Posts
    Blog Entries
    9
    Rep Power
    764
    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
    Code:
    ListBox1.Items.AddRange(GetPrintersCollection().ToArray)
    
    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
    Last edited by SYNACK; 13th August 2009 at 12:15 PM.

  6. #6

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,677
    Thank Post
    3,209
    Thanked 1,030 Times in 955 Posts
    Rep Power
    361
    Quote Originally Posted by SYNACK View Post
    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
    Code:
    ListBox1.Items.AddRange(GetPrintersCollection().ToArray)
    
    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.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.

SHARE:
+ Post New Thread

Similar Threads

  1. distributed storage array
    By browolf in forum Group Project
    Replies: 21
    Last Post: 4th August 2009, 06:28 PM
  2. Moving to a RAID array
    By binky in forum Hardware
    Replies: 7
    Last Post: 28th February 2009, 03:48 PM
  3. HELP - RAID Array Failed
    By sidewinder in forum Hardware
    Replies: 4
    Last Post: 18th May 2008, 08:53 AM
  4. Anyone Moved a Raid array.
    By K.C.Leblanc in forum Hardware
    Replies: 10
    Last Post: 15th November 2007, 05:52 PM
  5. 12 bay esata storage array
    By fooby in forum Hardware
    Replies: 8
    Last Post: 22nd June 2007, 08:35 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •