Windows Develop Bookmark and Share   
 index > Windows Forms General > custom ListBox Problem with Items.Add()
 

custom ListBox Problem with Items.Add()

If i have the following code in a userControl

Code Block

#region Items

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("Items"), Description("String Collection of Items")]

public StringCollection Items

{

get

{

return sc;

}

set

{

sc = value;

try

{

ItemChange();

}

catch

{

}

foreach (Label l in this.Controls)

{

l.Dispose();

}

int rog = -1;

foreach (string item in sc)

{

rog++;

Label label1 = new Label();

//

// label1

//

label1.BackColor = System.Drawing.Color.Transparent;

label1.ForeColor = System.Drawing.Color.Black;

label1.ImageAlign = System.Drawing.ContentAlignment.TopLeft;

label1.Location = new System.Drawing.Point(-2, ((-2 * (rog + 1)) + (20 * rog)));

label1.Name = "label" + rog.ToString();

label1.TextAlign = ContentAlignment.MiddleLeft;

label1.Size = new System.Drawing.Size(this.Width, 20);

label1.TabIndex = 0;

label1.Text = item.ToString();

label1.MouseDown += new MouseEventHandler(item_Click);

label1.MouseEnter += new EventHandler(item_Over);

label1.MouseLeave += new EventHandler(item_Leave);

label1.DoubleClick += new EventHandler(item_DoubleClick);

label1.Tag = "Reg";

this.Controls.Add(label1);

}

}

}

#endregion

Why doesn'tthis work?

Code Block
richListBox1.Items.Add(
"Item");

Thanks,

paoloTheCool

paoloTheCool  Tuesday, November 20, 2007 3:44 PM

First of all, what error are you receiving?

If it's a null reference, are you initializing the member StringCollection sc?

If you're concerned that it's not adding your labels, that's because your code isn't setting the Items property. Instead, it's getting the reference to sc. This would work: richListBox1.Items = anotherSc

Chris Eargle  Tuesday, November 20, 2007 3:57 PM

I am not receiving an error, it is not adding labels. Can you explain exactly what you mean by richListBox1.Items = anotherSc?

In the code of my usercontrol, do I have to "bind" the StringCollection Items to sc? or is it possible to just use Items.

Thanks,

paoloTheCool

paoloTheCool  Tuesday, November 20, 2007 4:19 PM

It's perfectly fine to use the items. The problem is when you call Items.Add(), Items has wrapped sc. In effect, you're calling sc.Add(). Your code is depending upon the set accessor for Items, which will only be called if you try to assign a reference to Items. That's why I said "richListBox1.Items = anotherSc" would work.

I think to accomplish this task you willneed to derive from string collection so you can raise an event when .Add is called. Because I'm sure you don't want to exposethat much informationto the outside world, I recommend deriving from List<string> internally and exposing Items as ICollection<string>.

property ICollection<string> Items{get;set;}

Chris Eargle  Wednesday, November 21, 2007 4:08 AM

Ok...I have been thinking and I still cannot figure this out. Should I make "Items" a class and put like methods under it? or is that just really stupid?

I really cannot figure this out.

Thanks,

paoloTheCool

paoloTheCool  Tuesday, December 04, 2007 10:44 PM

I think that's what you're going to have to do. You need to know when the list changes, so you can refresh your control. You can make your new list fire an event anyime it changes.

Alternatively you can just put your Add method directly in the control, and maintain an internal list.

Chris Eargle  Tuesday, December 04, 2007 11:43 PM

You can use google to search for other answers

Custom Search

More Threads

• Help with Windows Forms Printing
• Designer runtime and property grid Problem
• Printing at exact location independent of Printer
• 5 errors that I dont really get why they there...
• Problems with System.Net.WebResponse
• Can we put GIF image
• Adding a Validation to a Dataset
• Using CheckBoxColumn in DataGrid
• Thread communication
• Install package with .NET Framework