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

Code:
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
Code:
myFormVariable.ComboBox1.Items.Add("This and That")
To set a variable to a forms instance, put the following like on the
Forms constructor or _Load event:
Code:
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:

Code:
        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 by a moderator:
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.
 
Didnt you see jjjamies post?

Heres the code:
Code:
 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
 
well theres a problem to that. because its in a class it dosnt see the combobox on form1. but thanks for ir help
 
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 ill put it in the main form.
 
ok heres what i got and its in form1 with the combobox:

Code:
    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