views:

39

answers:

1

Heya Guys.

I have a application im working on that uses the Jabber Libraries to connect to a jabber Server and receive contacts etc.

I have buit all login system and interface for the chat but now im working on how to Bind the data for the contacts to the ListView

I have a function that is called when a contact comes online such, See Below

//AppController.cs
public void XmppConnection_OnRosterItem(Object Sender, RosterItem RosterItem)
{
    if (LoginWindow.ActiveForm.InvokeRequired)
    {
        LoginWindow.ActiveForm.BeginInvoke(
           new XmppClientConnection.RosterHandler(XmppConnection_OnRosterItem),
              new object[] { Sender, RosterItem}
        );
        return;
    }
    //UPDATE HERE
}

The idea is to have a class such as ContactList so that when the above function is called i can go ContactList.AddRoster(Roster);

What i need to know is how do I create a custom list class and then bind it to the the Form witch holds the ListView element

And if possible set an update interval to recompile the ListVeiw?

Hope you guys can help me


Edit:

If I could have 2 classes one for the individual contact and one to hold the collection like so:

Contact C = new Contact(Roster.Name,Roster.Jid,Roster.Group);
ContactList.Add(C);

This as well would be good.

+2  A: 

You could create a Contact class the just create a List of Contacts

List<Contact> ContactList=new List<Contact>();
ContactList.Add(Roster);

How to Bind ListView to List
http://www.vistax64.com/avalon/615-how-bind-listview-list-mystruct.html

Not sure about the update interval though. Attach it to a certain event and check the time in between maybe? MouseMove (Performance Cost?)

Anyone else have any ideas?

EDIT:

class ContactList:List<Contact>
    {
        public ContactList()
        {

        }
    }

You shouldn't need to add anything to this class

class Contact
    {
        public string _Name;
        public string _Jid;
        public string _Group;
        public Contact()
        {
            _Name = "Test";
            _Jid = "One";
            _Group = "Two";
        }
        public Contact(string Name, string Jid, string Group)
        {
            _Name = Name;
            _Jid = Jid;
            _Group = Group;
        }
        public override string ToString()
        {
            return _Name+" "+_Jid+" "+_Group;
        }

    }

Overiding the ToString function allows you easier control over what is displayed in the listbox

public Form1()
        {
            InitializeComponent();
            ContactList C = new ContactList();
            C.Add(new Contact("Name","Jid","Group"));
            C.Add(new Contact());
            C.Add(new Contact("Test","2","Something"));
            for (int i = 0; i < C.Count; i++)
            {
                listView1.Items.Add(C[i].ToString());
            }
        }

Let me know if this works for you.

Gage
You could create a System.Windows.Forms.Timer and do an update in the Tick event. This happens on the UI thread, so you don't have to worry about cross-threading operations (like if you used System.Threading.Timer).
Coding Gorilla
Thank you so much for your answer, I'v created the `ContactLists` and the `Contact` Class, Can you just show me how I bind to the form?
RobertPitt
AFAIK you cannot bind them directly to the ListView control, you will need to essentially iterate the list and add them to your ListView.Items collection. Again, I'd suggest doing this a Timer.Tick event.
Coding Gorilla
OK thanks guys Let me give it a try :)
RobertPitt
Im going to accept your answer as you have shown me the way, your my chosen one :), any ways thank you for your help, Much appreciated.
RobertPitt