I want to populate my ListBox by a list of objects with properties , and I want to know how to define the listbox to display a certain property within this object which is some text,
and the other is the name of methods to be invoked while the listBox Items are clicked (SelectIndexChanged)
-
Instead of using a list of dictionaries try using observable collection.Refer this link for binding an observable collection to listbox.Naren– Naren2013-08-13 08:27:52 +00:00Commented Aug 13, 2013 at 8:27
-
Why is this a list of dictionaries instead of just a dictionary, or a list of key/value pairs?Jon– Jon2013-08-13 08:29:24 +00:00Commented Aug 13, 2013 at 8:29
-
I have used this technique in java and it worked, dont know in c#! I myself see it complex , but dont know any other idea, if there is a good , better idea , i'll use itrabar kareem– rabar kareem2013-08-13 08:32:06 +00:00Commented Aug 13, 2013 at 8:32
-
OK @Naren , but mine is c# and your link uses WPF, and DataContext is WPF , and I cant manupulate design of my Listview tagrabar kareem– rabar kareem2013-08-13 08:42:59 +00:00Commented Aug 13, 2013 at 8:42
-
1Its easy to do this, but again maybe you didnt understand me and thats the main problem - do you NEED to have a List that holds an array/collection/dictionary of objects or is sufficient if it holds only 1 object, so to sum it up List<string> or List<string[]> ?!? If you only need an single object you can just create a class and do a list of these, this class can hold anything methods/properties what ever you wish... (not limited to only 2 properties - as in a keyvaluepair<string, string>) - Array or no array inside a list what shall it be?!?Rand Random– Rand Random2013-08-13 09:30:13 +00:00Commented Aug 13, 2013 at 9:30
|
Show 9 more comments
1 Answer
Hope this helps.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Create a listbox, with given height/width and top/left
var lstBox = new ListBox
{
Width = 300,
Height = 300,
Top = 10,
Left = 10
};
//Add the listbox to your form
this.Controls.Add(lstBox);
//Create a list of your customclass
var listCustomClass = new List<CustomClass>();
//Populate the list with values
for (int i = 0; i < 50; i++)
{
//create an instanze of your customclass
var customClass = new CustomClass();
//set properties of your class
customClass.Name = "Name " + i;
customClass.Description = "Description " + i;
if (i % 2 == 0)
customClass.MethodName = "CallMeBaby";
else
customClass.MethodName = "CallMeBabyWithParameter";
customClass.RandomProperty1 = "RandomProperty1 " + i;
//add the newly created customclass into your list
listCustomClass.Add(customClass);
}
//set the listbox to display or value what you need
lstBox.DisplayMember = "Description"; //Name of a property inside the class CustomClass
lstBox.ValueMember = "Name"; //Name of a property inside the class CustomClass
//set the datasource
lstBox.DataSource = listCustomClass;
//register the selectedindexchanged event
lstBox.SelectedIndexChanged += lstBox_SelectedIndexChanged;
}
private void lstBox_SelectedIndexChanged(object sender, EventArgs e)
{
//get the listbox from the sender
var lstBox = (sender as ListBox);
if (lstBox != null)
{
//safe cast the selecteditem to your customclass to get full access to any public property with the class definition
var customClass = lstBox.SelectedItem as CustomClass;
if (customClass != null)
{
//do what ever you want with the object and its properties
var name = customClass.Name;
var desription = customClass.Description;
var methodName = customClass.MethodName;
var randomProperty1 = customClass.RandomProperty1;
//call a certain method based on a string within the object
if (methodName == "CallMeBaby")
CallMeBaby();
else if (methodName == "CallMeBabyWithParameter")
CallMeBaby(name);
}
}
}
//declare the methods that are being called
private void CallMeBaby(string value)
{
//Access the parameter and do something
if (value == "HelloWorld!")
{
//Do something...
}
}
//parameterless method to show the possibilities...
private void CallMeBaby()
{
//Do something...
}
//define a public class
public class CustomClass
{
//random properties, can be extended to have what ever your need
public string Name { get; set; }
public string Description { get; set; }
public string MethodName { get; set; }
public string RandomProperty1 { get; set; }
}
}