CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2009
    Posts
    48

    Arrow 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

  2. #2
    Join Date
    Sep 2006
    Posts
    31

    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

  3. #3
    Join Date
    Dec 2009
    Posts
    48

    Talking 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

  4. #4
    Join Date
    Sep 2006
    Posts
    31

    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

  5. #5
    Join Date
    Dec 2009
    Posts
    48

    Re: Loading up ListBox and Event Throwing

    You nailed it! thank you a lot It was pretty helpful!!

  6. #6
    Join Date
    Sep 2006
    Posts
    31

    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
  •  





Click Here to Expand Forum to Full Width

Featured