Filling listbox within the User control

Anjumnagpal

Member
Joined
Feb 4, 2009
Messages
15
Programming Experience
Beginner
I created a user control with a listbox in it and a public property ListItem()

Public Property ListItem() As String

Get
Return _ListItem
End Get

Set(ByVal value As String)
_ListItem = value
End Set

End Property

On FormLoad I wish to fill the Usercontrol Listbox with the values that the user supplies.


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

Dim u As New UserControl1()
u.ListItem = "hello"
u.ListBox1.Items.Add(u.ListItem)

End Sub

The Listbox does get filled.. what m I doing wrong??
 
If you normally add an item to a ListBox by calling Items.Add then it is logical that you would want to expose the Items property of the ListBox, so that you can call its Add method:
VB.NET:
Public ReadOnly Property ListItems() As ListBox.ObjectCollection
    Get
        Return Me.ListBox1.Items
    End Get
End Property
Now you simply do this:
VB.NET:
Dim u As New UserControl1

u.ListItems.Add("hello")
 
I know that what I suggested will work so if it doesn't work for you then you're using it incorrectly. As you haven't actually shown us how you're using it we can't tell you what you've done wrong.
 
I am sorry.. didnt mean to offend you at all. here's the code

user control code

public Class UserControl1

Public ReadOnly Property ListItems() As ListBox.ObjectCollection
Get
Return Me.ListBox1.Items
End Get
End Property

End Class
--------------------------------------------------------------------------

FORM CODE

Public Class Form1

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

Dim u As New UserControl1

u.ListItems.Add("hello")

End Sub

End Class

--------------------------------------------------------------------------

This is it.. that's the only thing I am doing.
 
Then what you're doing IS working. It's just that what you're doing isn't going to achieve what you actually want happen. In your Form1_Load method you are creating a NEW instance of your UserControl1 class and adding an item to its ListBox. That's the code you've written and that's exactly what happens. The thing is, you never actually do anything with that new instance so the user will never see it. Also, anything you do to that new instance won't affect any existing instance, so if you've already added an instance to your form in the designer then it will not be affected in any way by that code.

Consider this. Let's say that you have a notebook that has nothing written in it. If you went out and bought a NEW notebook and added some writing to that, what would you expect to happen to the original notebook? Would you expect the same writing to magically appear in it? Of course not. They are two different objects. They may be the same type of objects but that doesn't mean that what you do to one affects the other. The same goes for your user controls. If you create two instances then they are two different objects. The fact that they are the same type doesn't mean that a change made to one will affect the other.

So, if you've added an instance of your user control to your form in the designer then you need to add an item to THAT instance, not a new instance.
 
Thanks for such a great explaination. It was my first attempt to create a user control. I understand the whole thing now. I got the desired results now. I should not have instantiate a new usercontrol object. Thanks again:)
 
Back
Top