Adding an itme to a list box in a class

The easiest way is to create a subroutine that takes the ComboBox as a parameter and then modify it from there. For example in the class write...

Visual Basic:
Public Sub AddComboItem(ByVal ComboBox as ComboBox, ByVal Item as String)

   ComboBox.Items.Add(Item)

End Sub

Then all you have to do is call the subroutine from Form1 (or from elsewhere within the class).

Jamie
 
You need to have the instance of the form object stored in a variable
which can be passed to the class. At that point you can do
Visual Basic:
myFormVariable.ComboBox1.Items.Add("This and That")
To set a variable to a form's instance, put the following like on the
Form's constructor or _Load event:
Visual Basic:
myFormVariable = Me
 
the class is in its own file to make it easier for me to get back to when i need to. the onload thing wont work (i tried it) i tried:

Visual Basic:
        Dim Form1 As Form1
        Form1.ComboBox1.Items.Add("United States(Dollar)")
        Form1.ComboBox1.Items.Add("Canada(Dollar)")

note: this is a curency converter.
 
Last edited:
ok maybe i said my question wrong...

i want to add an item to a list box by calling it from a class.

i.e.: i call the class Class1.Action1

in the action i want to add items to a list box.
 
Didn't you see jjjamie's post?

Here's the code:
Visual Basic:
' Put this in Class1
Public Sub Action1(ByVal ComboBox As ComboBox, ByVal Item As String)
   ComboBox.Items.Add(Item)
End Sub

You can leave off Item if you want, if the Class1 object is going to know what to add.

-Nerseus
 
I assume Class1 is an object created in the form. The form is callign the Action1 method on the instance of Class1. The form certainly knows about the ComboBox and can pass it along to the class.

-Ner
 
well i have the class in a differnt file and it doesnt know the combo box is there. iif i have to i'll put it in the main form.
 
ok heres what i got and its in form1 with the combobox:

Visual Basic:
    Class fill
        Public Shared Sub fillUSCAN()
            ComboBox2.Items.Add("United States(Dollar)")
            ComboBox2.Items.Add("Canada(Dollar)")
        End Sub
    End Class
it tells me 'Referance to a non shared member requires an object referance.'
 
Back
Top