ListBox and objects
-
I have an ArrayList populated with UserControl objects. I need to display this list on a windows form and when a user clicks on any of the items I need display the properties of the control. I thought I could use ListBox control since we can populate it with the objects. But the problem is that when I populate the list box I dont get any text values. And if you click on the list box you can clearly see where the items were populated but no textual representation for it.
foreach(UserControl c in mb.UserControls) { listBox1.Items.Add(c); }
unless i do:
foreach(UserControl c in mb.UserControls) { listBox1.Items.Add(c.ToString()); }
Its something really simple but for some reason I am not getting it. Would appreciate some help. Thanks.
-
I have an ArrayList populated with UserControl objects. I need to display this list on a windows form and when a user clicks on any of the items I need display the properties of the control. I thought I could use ListBox control since we can populate it with the objects. But the problem is that when I populate the list box I dont get any text values. And if you click on the list box you can clearly see where the items were populated but no textual representation for it.
foreach(UserControl c in mb.UserControls) { listBox1.Items.Add(c); }
unless i do:
foreach(UserControl c in mb.UserControls) { listBox1.Items.Add(c.ToString()); }
Its something really simple but for some reason I am not getting it. Would appreciate some help. Thanks.
-
I have an ArrayList populated with UserControl objects. I need to display this list on a windows form and when a user clicks on any of the items I need display the properties of the control. I thought I could use ListBox control since we can populate it with the objects. But the problem is that when I populate the list box I dont get any text values. And if you click on the list box you can clearly see where the items were populated but no textual representation for it.
foreach(UserControl c in mb.UserControls) { listBox1.Items.Add(c); }
unless i do:
foreach(UserControl c in mb.UserControls) { listBox1.Items.Add(c.ToString()); }
Its something really simple but for some reason I am not getting it. Would appreciate some help. Thanks.
Listbox.Items is an ObjectCollect so it can store strings as well as ListBoxItems
only two letters away from being an asset
-
I have an ArrayList populated with UserControl objects. I need to display this list on a windows form and when a user clicks on any of the items I need display the properties of the control. I thought I could use ListBox control since we can populate it with the objects. But the problem is that when I populate the list box I dont get any text values. And if you click on the list box you can clearly see where the items were populated but no textual representation for it.
foreach(UserControl c in mb.UserControls) { listBox1.Items.Add(c); }
unless i do:
foreach(UserControl c in mb.UserControls) { listBox1.Items.Add(c.ToString()); }
Its something really simple but for some reason I am not getting it. Would appreciate some help. Thanks.
Hi, a ListBox is great at showing a collection of objects in a vertical list. There are basically two ways to visualize the items: 1. provide a ToString() method that returns the string you want; it could show whatever member(s) of the item, or some other calculated value. This way always renders a single string, all in one color. 2. or make the entire ListBox userdrawn; by providing a paint handler to the DrawItem event, you can paint anything you like, even the actual look of the UserControl itself... You probably will want to show selected items in a different way than unselected ones, e.g. a different background color. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
Hi, a ListBox is great at showing a collection of objects in a vertical list. There are basically two ways to visualize the items: 1. provide a ToString() method that returns the string you want; it could show whatever member(s) of the item, or some other calculated value. This way always renders a single string, all in one color. 2. or make the entire ListBox userdrawn; by providing a paint handler to the DrawItem event, you can paint anything you like, even the actual look of the UserControl itself... You probably will want to show selected items in a different way than unselected ones, e.g. a different background color. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
well I ended up doing something like this:
Dictionary<int, UserControl> dic = new Dictionary<int, UserControl>(); ... int count = 0; foreach (UserControl c in mb.UserControls) { listBox1.Items.Add(c.Name); dic.Add(count, c); count++; }
and when user double clicks a control I simply use the selectedIndex to pull the control out from dictionary
foreach (KeyValuePair<int, UserControl> kvp in dic) { if (kvp.Key == listBox1.SelectedIndex) { MessageBox.Show(kvp.Value.Name, kvp.Value.Size.ToString()); winForm.Controls.Add(kvp.Value); } }
is that a terrible solution?
-
well I ended up doing something like this:
Dictionary<int, UserControl> dic = new Dictionary<int, UserControl>(); ... int count = 0; foreach (UserControl c in mb.UserControls) { listBox1.Items.Add(c.Name); dic.Add(count, c); count++; }
and when user double clicks a control I simply use the selectedIndex to pull the control out from dictionary
foreach (KeyValuePair<int, UserControl> kvp in dic) { if (kvp.Key == listBox1.SelectedIndex) { MessageBox.Show(kvp.Value.Name, kvp.Value.Size.ToString()); winForm.Controls.Add(kvp.Value); } }
is that a terrible solution?
Hi, if it suits your needs, then it is fine. One remark though: since all your keys are different (as they should), and a dictionary has an indexer (i.e. can be read as if it were an array), there is no need to search for a specific item, you could simply do:
int index=listBox1.SelectedIndex;
if (index>=0) {
UserControl uc=dic[index];
MessageBox.Show(uc.Name, uc.Size.ToString());
winForm.Controls.Add(uc);
}BTW: you don't really need a Dictionary, a simple List< UserControl> would work the same way. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.