שתף באמצעות


Asign an array to a Combobox.Items --VB.NET

Question

Sunday, March 28, 2010 1:16 AM

Hi,

I would like to how I can asign an array to a combobox.items. Which type have I to choose?

 

Thanks

Regards

All replies (15)

Sunday, March 28, 2010 2:01 AM ✅Answered

Use :

        ClasificacionAreaComboBox.Items.CopyTo(arrayClasificacionArea, 0)


Sunday, March 28, 2010 3:26 AM ✅Answered

The combobox items collection is an array that can provide the string values of the combobox items.   See:

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.items.aspx

 


Monday, March 29, 2010 2:07 AM ✅Answered

Hi Joe ( jwavila ),

The only problem with AddRange is that it will fail if the array is full of Nothing or if any one item is Nothing .

I do not mean that the array is Nothing, I actually mean where all the items in the array are Nothing .

To see what I mean step through this code and you will see the For Next loop will loop for a total of 8 times from array index zero to array index 7 if the ComboBox is empty or not .

 

Try this code with 1 ComboBox ( with or without items ) on a Form and 1 button .

 

Regards,

John

 

P.S. If you read my code comments, and my code you will see it is better

to iterate ( loop through ) an array and add an item to a ComboBox if it is Not Nothing

as it is safer as the code avoids any possible ArgumentNullException . :-)

I try to write code that avoids any exception errors and that it allows for an item being Nothing, when I'm fully awake .

This does not mean that I use Try ... Catch ... End Try code blocks a lot either. LOL!!

 

If you have items in the ComboBox they will be copied to the array, the ComboBox is cleared, and the items are copied back from the array. Result = No Change to the ComboBox.

However this code just demonstrates the ComboBox.Items.CopyTo method, the Combobox.Items.Clear method and the Array.Clear method.

:-)

 

To iker garcia ,

I hope you find some of what this code shows you, useful.

 

Option Strict On
Public Class Form1

    Private myArray() As System.Object = New Object() {1, Nothing, 3, 4, "5", "6", "7", "8"}

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'If you uncomment the next line the code will fail as the item in myArray at index 1 is Nothing >>
        'ComboBox1.Items.AddRange(myArray)

        'If you want to copy ComboBox items to an array you might want to clear it first? >>
        Array.Clear(myArray, 0, myArray.Count)
        'Copy the ComboBox1 items to an array starting an index zero.>>
        ComboBox1.Items.CopyTo(myArray, 0)

        'If you want to do the reverse you may want to clear
        'the ComboBox items first? >>
        ComboBox1.Items.Clear()
        'Copy the array items that are NOT "Nothing" to ComboBox1.Items collection.
        'AddRange will fail if an array item is Nothing.
        For index As Integer = 0 To myArray.GetUpperBound(0)
            If Not (myArray(index) Is Nothing) Then
                ComboBox1.Items.Add(myArray(index))
            End If
        Next

    End Sub
End Class

 

Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4


Sunday, March 28, 2010 1:26 AM

doesn't matter - a ComboBox can accept any type of Object in it's items

http://msdn.microsoft.com/en-us/library/aa983551(VS.71).aspx

 

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox(VS.71).aspx


Sunday, March 28, 2010 1:40 AM

I have the following code:

 

Dim arrayClasificacionArea() As String

arrayClasificacionArea = ClasificacionAreaComboBox.Items()

 

For Each i As String In arrayClasificacionArea

 

Next

But the compiler gives me the following error:  Error 1 A value of type 'System.Windows.Forms.ComboBox.ObjectCollection' it can´t convert to  '1- dimensional array of String


Sunday, March 28, 2010 2:10 AM

hi

It gives the following warning:

Variable '<arrayClasificacionArea>' is used before it has been assigned a value. A null reference exception could result at run time


Sunday, March 28, 2010 2:23 AM

why are you putting all the ComboBox items in an array? They are already in a collection; just use that collection as you would an array

For Each s As String In ComboBox1.Items

        Next

Sunday, March 28, 2010 2:36 AM

to obtain a substring of each value of the combobox


Sunday, March 28, 2010 3:23 AM

The warning is telling you that you shuold initialise the array before you assign the combobox items to it.  Arrays are not initialised by default.  Set the array elements up with default vaues before doing the copy.


Sunday, March 28, 2010 4:43 AM

so take the Substring from each item in the combobox Items Collection.

What I'm trying to tell you is you can use the Items Collection the same as you would an array - it will work the same

try this in an app with just a ComboBox on the Form

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ComboBox1.Items.AddRange(New String() {"123Blue", "456Red", "789Green"})

        For Each s As String In ComboBox1.Items
            Debug.WriteLine(s.Substring(3))
        Next

    End Sub

Monday, March 29, 2010 4:46 AM

Hi John

but the problem is not using an array to populate a ComboBox's Items Collection

The OP is trying to go the other way - if you look at the OP's second post, the one with the code, you can see:

   1. a String array is being initialized,

   2. the array elements are trying to be set from the ComboBox's Items Collection

   3. an attempt to then loop through the array elements

and when I queried the OP, the response was:

to obtain a substring of each value of the combobox

which tells me the OP thought the ComboBox's items needed to be in an array to get a substring of each item. I was just trying to get the OP to realize the Items Collection can be used the same way. Maybe the confusion is coming from the fact I populate the ComboBox with an array - maybe I should have put the For Each loop in a Button Click event and only shown that :(

At least that is how I interpreted the sample code and response to my question. And it seems Acamar understood it that way also - see his third post. But I may be wrong!

I'll try to get around to resending that email I mentioned earlier


Monday, March 29, 2010 5:25 AM

Hi again Joe,

Okay fair enough. Seeing as I've read your nearly identical previous two posts, do you fancy deleting one of them?

Regards,

John

 

Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4


Monday, March 29, 2010 5:35 AM

Hi John

Ooooh, that's not good

when I clicked the button the first time it gave me the Unexpected error message. When that happens it usually doesn't post my response, I have to copy what I typed, close the window, re-open it and sign in again. Then I can paste what I typed earlier.

For some reason it accepted my first Submit request - never had that happen before. Sorry, I wasn't trying to be rude or anything. It DOES look like bad manners, though.

I'm in the middle of trying something else right now - as soon as I get to a stopping place, I'll try your code :)


Tuesday, March 30, 2010 12:43 AM

edit: it just did it to me again - twice!!!  Are you having trouble with the Forum? This time it did NOT post the first time I clicked

Hi Joe,

I have occasionally had problems with the forum, and I'm not entirely sure if it is down to which Browser I am using.

Sometimes when I go to edit a post it enters HTML view instead. :-S

I guess I'm not the only one who has occasional problems with this forum then!!

 

Regards,

John

 

Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4


Tuesday, March 30, 2010 12:51 AM

Yeah, it does that to me also.

I found that it usually happens if I post and then immediately try to edit. So if I think of something I forgot I close the window and re-open it. Never had it happen when I do that.

Joe