Sign up to the jQuery Grid Subscription list.

How to use Collections and Inheritence (with listbox)

I have come up with several difficulties when I want to create a list on a list box that contains objects. (Each element have several data).

Collections and Inheritence solved this problem for me.
Just I create a class that inherit from BaseCollection and add some methods to it, like "add", "remove"... to manage items.

This will make it easier to add and remove items from Listbox, to get the object from the selected element on the list box and also to save the listbox contect on an XML file.
I'm going to explain you more about this project and also to give you some source code and the application file to download.

Our application is very simple, it consits of 
  • Entre a person data (firstname, lastname, age)
  • Show the persons list on a text box
  • Show the selected person data (Complete name, age)
  • Save the the whole data to an XML file
  • Reload the XML file to memory
If you are new with Inheritence and Collections so this is almost impossible for you.
You have a list of classes, how to deal with the list box and also saving and reloading from the XML file.

   1. Designing the user interface

First of all, we need to create a lcean user interface. Simple and easy to use.
  • Buttons (Add, Remove, Show, Save, Load)
  • Text Boxes (firstname, lastname, age)
  • Listbox
Here's an example of what you should get
Now let's divide our project into parts
  • The person class
  • The listbox Collection class
  • Methods (Add, Remove, Show)
  • Saving and loading to an XML File (will be discussed in another post)
  2- Creating the Person Class

The person class consist of "First name", "Lastname" and "age"
We can put those as fields or as properties. As our application is very simple, so we are going to use fields.
Also we need a method called "Complete name" that displat first and last name at once.
We can also use constructors just to make things easier and faster.
The following code can be used for the Person Class. To do, create a new Class file and name it portfolio.
You should get this code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace person_portfolio
{
class portfolio
{
}
}


Now you can delete the portfolio class, I just wanted to make the classes file separated than other files.
The final code should be like this


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace person_portfolio
{
class person
{
// Declare fields
public string firstname;
public string lastname;
public int age;
// constructor
public person(string _firstname, string _lastname, int _age)
{
firstname = _firstname;
lastname = _lastname;
age = _age;
}
// Override and return complete name
public override string ToString()
{
return firstname + " " + lastname;
}
}
}


You may ask why I did override the Tostring() Method. I'll answer that question later.

 3- Creating the Collection class

After creating the person class, we need to create a class that collects those classes (persons). We can do that without creating the Collection Class, but by doing so we can have more possibilities and customize the way the class works.


class plist : System.Collections.CollectionBase
{
public person_portfolio.person this[int index]
{
get
{
return ((person_portfolio.person)List[index]);
}

}
public void Add(person_portfolio.person pers)
{
List.Add(pers);
}
public void Remove(person_portfolio.person pers)
{
List.Remove(pers);
}

}


The collection class is simple. It inherits from the 'CollectionBase' class and contain an 'add' and 'remove' method. And also a method to get the object through its index.

 4- Creating the user interface

As the code is too long, then you'd better download the project source.
The user interface should contain an Add and Remove button. Also a show button to display Person data.

Here's the form code with the method


public partial class Form1 : Form
{
person_portfolio.plist pers_list = new plist();
public Form1()
{
InitializeComponent();
}

private void btn_add_Click(object sender, EventArgs e)
{
person new_person = new person(tb_fn .Text , tb_ln .Text , System.Convert .ToInt32(tb_age .Text.ToString ()));
pers_list.Add(new_person);
refresh_list();

}
private void refresh_list()
{
listbox.Items.Clear();
for (int i = 0; i < pers_list.Count; i++)
{
listbox.Items.Add(pers_list[i]);
}
}

private void btn_show_Click(object sender, EventArgs e)
{
person get_person = (person)listbox.Items [listbox .SelectedIndex ];
MessageBox.Show("First name: " + get_person .firstname .ToString () +Environment .NewLine + "Last name: " + get_person .lastname .ToString () + Environment .NewLine + "Age: " + get_person.age.ToString());
}

private void btn_remove_Click(object sender, EventArgs e)
{
int index = listbox.SelectedIndex;
listbox.Items.RemoveAt(index);
pers_list.RemoveAt(index);
}
}

The application is still missing the 'save' and 'load' functions, which I'm going to explain later.

Feel free to download the project source.
If you found any bug or problem please post a comment.

0 comments:

Post a Comment