Adding Items to a Listbox on a Windows form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do I create an item for a listbox?

I want to do something like this EXCEPT for a listbox:

ListViewItem item = new ListViewItem();
item.Text = column.Name;
int height = item.Bounds.Height;
listView1.Items.Add(item);

I want to use the item's properties for something else.

What's the syntax look like for instantiating an item for a listbox?
 
How do I create an item for a listbox?

I want to do something like this EXCEPT for a listbox:

ListViewItem item = new ListViewItem();
item.Text = column.Name;
int height = item.Bounds.Height;
listView1.Items.Add(item);

I want to use the item's properties for something else.

What's the syntax look like for instantiating an item for a listbox?

A ListBox is a bit more simple than a ListView so it's just:

listBox1.Items.Add(Item);

Item can be any object so either just a string or, if you want more
data availble when an item is clicked, you can add an instance of a
class.

e.g. if you had a Class called cListData that contained say a string
called Name, a number called RecordNumber etc. set up an instance of
the class (say cldTemp), set up the data you want and then do:

listBox1.Items.Add(cldTemp);

If you do this you may want to over ride the string property of the
class to return whatever makes most sense to show in the ListBox e.g.

// Override ToString method so that Name is default
public override string ToString()
{
return this.Name;
}
 
Back
Top