Adding Items To A Listbox problem

LeeSalter

Freshman
Joined
Feb 13, 2003
Messages
26
Location
In a House
All,

I have some code in a Class that I want to add items to a listbox that lives on a form.

The Form is in one .vb file, the class code is in a seperate .vb file under the same solution.

I cannot reference the listbox on the form form the code in the class! Ie :- frmName.Listbox does not work.

If I inherit the form in the Class code, I can reference the listbox by doing me.Listbox

However, when I code to add items to this listbox, the items do not appear!! Any idea what I'm doing wrong??
 
I guess your startup object is the frmName?
And the code you wrote to populate this listbox are all on the derived class (inherited from the form)?

The form is created by the framework (startup object), NOT the class.
What I suggest is the following:
Add a parameter of type listbox to your class's constructor:
Visual Basic:
Public Sub New(myListbox as Listbox)
_myListbox = myListbox
End Sub
'this holds the reference to the listbox passed into the constructor
private _myListbox as Listbox

'and now in your e.g. Forms' load event, you can create a  instance of the class (also class level)
private Form_load(..)
_MyClass = new _MyClass(Listbox)
end sub
the code you write will effect the current instance of the listbox on the form.
Cheers
 
Back
Top