-
December 13th, 2009, 10:22 PM
#1
Loading up ListBox and Event Throwing
May I say first this forum is just great and there are lots of people here always ready the help and so, I must congratulate all the forum! Well, again, lets stat... I wanted to know how can I load a linked list into a Listbox component. Or if it is even possible.
Then I also wanted to know if it is possible to when select items on the list throw events with a specific item propriety (to confusing?).
Lets say I load up a Listbox with Various Products... and then When I select a given product I wanted that product's info to be loaded into a panel for example.
Hope you can help. thanks
-
December 14th, 2009, 05:56 AM
#2
Re: Loading up ListBox and Event Throwing
You should use an arrayList.. you can fill the listbox like this(i think you can do the same with linklists but i would use an arraylist):
Code:
List<string> list = new List<string>();
list.Add("Product 1");
list.Add("Product 2");
list.Add("Product 3");
listBox1.DataSource = list;
for the event handeling:
I used a messagebox but you can call a function that you made yourself that unhides a panel and shows the info.
Code:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("" +listBox1.SelectedValue);
}
greetz kristof
-
December 14th, 2009, 10:00 AM
#3
Re: Loading up ListBox and Event Throwing
Thank you very much!! you explained it very simple and objective! ...thought, I was looking for something more advanced... (I think) lets say the elements of the array are objects from a class for example "class car" and when I list them I want to show their names, or their license nunber, or their year... :/ I think I can load objects into the array but then again how could I list them by one specific proprieties (or by more than one). Should I use a diferent type of component?
thank you once again
-
December 14th, 2009, 11:35 AM
#4
Re: Loading up ListBox and Event Throwing
You're welcome here is how you can achive what you want to do:
lets say you have a class car like this one:
Code:
class car
{
public string Name { get; set; }
public string Licencenr { get; set; }
public string Year { get; set; }
public car(string name, string licencenr, string year) {
Name = name;
Licencenr = licencenr;
Year = year;
}
}
Then make a function that will be your eventhandler like so:
Code:
private void CheckSelectedListbox1(object sender, EventArgs e)
{
if (!(listBox1.SelectedIndex ==-1))//If something is selected
{
MessageBox.Show("" + listBox1.SelectedValue );
}
}
Now in for example in your formload do this:
Code:
car car1 = new car("BMW", "BGJ-123", "2009");
car car2 = new car("Audi", "DFY-150", "2008");
car car3 = new car("Ford", "HDT-343", "2007");
List<car> list = new List<car>();
list.Add(car1);
list.Add(car2);
list.Add(car3);
listBox1.SelectedIndex = -1;//select nothing
listBox1.DataSource = list;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "Licencenr";
//Set event after the data is binded!
listBox1.SelectedIndexChanged += new EventHandler(CheckSelectedListbox1);
DisplayMember = "Name" Makes the listbox show the name(variable of car class) as the item text.
When you use ValueMember = "Licencenr" and you use listbox1.valuemember you'll get the licencenr
greetz kristof
-
December 14th, 2009, 12:21 PM
#5
Re: Loading up ListBox and Event Throwing
You nailed it! thank you a lot It was pretty helpful!!
-
December 14th, 2009, 01:55 PM
#6
Re: Loading up ListBox and Event Throwing
No problem mate!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|