CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2013
    Posts
    1

    Events in MVC (Need some Help)

    Hello

    I need some help. I am newcomer in C#.

    The application is the PhoneBook. All the data serializable and stored in the List<>. Ithe main idea is to realize MVC pattern.
    The question is how to implement events for deleting and editing contacts? Event for adding a contact is already exist and works.

    To delete contact - inside the buttonDeleteContact_Click handler I need to get a selected object, than wrap it in DeletePersonEventArgs
    and call the event (identical to AddClicked) DeleteClicked that will contail a code that delete a contact.

    1. How to describe a type of Delete event ?
    Code:
      internal class DeletePersonEventArgs : EventArgs
    2. What should be in
    Code:
    buttonDeleteContact_Click handler
    ?

    3. What should be inside controller's method that handles the DeleteClicked event?

    Code:
    void form_DeleteClicked(object sender, DeletePersonEventArgs e)
    The same issues for Update code.

    Could you please provide a code example for this events and help me to realize this logic.

    Source code attachedPhoneBook v1.8.zip

  2. #2
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Events in MVC (Need some Help)

    Generally, you need to have some way to identify from the selected ListViewItem the corresponding data in the model (where model in your case is the people field in the ManageControler class).
    You can achieve this in several ways. For example, you can use the Name property of the ListViewItem which acts as a string-based key for each item to store some sort of id, but that's somewhat hackish and error prone. You can do something better.

    First, add another internal class, which derives from ListViewItem. It's very simple:
    Code:
        internal class PhoneBookListViewItem : ListViewItem
        {
            public PhoneBookListViewItem(Person p)
                :base(new string[] { p.Name, p.Phone, p.Email })
            {
                Person = p;
            }
    
            public Person Person { get; private set; }
        }
    Notice that this simply accepts a Person, automatically does, via a call to a base class constructor, what you have been doing before and it also stores a reference to the Person object itself. This is generally speaking not a significant overhead, since Person is a reference type (class), so internally it's essentially just a managed pointer.

    Now you're ready to go. Since this new class derives from ListViewItem, it can be passed to anything that expects a ListViewItem, so modify your old code like this:
    Code:
            public void UpdateView(List<Person> list)
            {
    
                listViewPeople.Items.Clear();
                foreach (var item in list)
                {
                    // ListViewItem lv_item = new ListViewItem(new string[] { item.Name, item.Phone, item.Email });
                    PhoneBookListViewItem lv_item = new PhoneBookListViewItem(item);
                    listViewPeople.Items.Add(lv_item);
                }
            }
    Quote Originally Posted by plywoods View Post
    1. How to describe a type of Delete event ?
    Code:
      internal class DeletePersonEventArgs : EventArgs
    With the above done, you can now replace your entire DeletePersonEventArgs with just this:
    Code:
        internal class DeletePersonEventArgs : EventArgs
        {
            public DeletePersonEventArgs(IList<Person> personsToDelete)
            {
                PersonsToDelete = personsToDelete;
            }
    
            public IList<Person> PersonsToDelete { get; private set; }
        }
    BTW, note that with this setup you can delete more than one record simultaneously - you should probably modify the name of the class to reflect that (as it currently suggests deletion of a single entry).

    Quote Originally Posted by plywoods View Post
    2. What should be in
    Code:
    buttonDeleteContact_Click handler
    ?
    This:
    Code:
            private void buttonDeleteContact_Click(object sender, EventArgs e)
            {
                // First, extract the Person objects from the selected items:
                List<Person> personsToDelete = new List<Person>();
                foreach (var item in listViewPeople.SelectedItems)
                {
                    personsToDelete.Add(((PhoneBookListViewItem)item).Person);
                }
    
                // And then raise the event
                if (DeleteClicked != null)
                    DeleteClicked(this, new DeletePersonEventArgs(personsToDelete));            
            }
    Quote Originally Posted by plywoods View Post
    3. What should be inside controller's method that handles the DeleteClicked event?
    Code:
    void form_DeleteClicked(object sender, DeletePersonEventArgs e)
    Now you can simply loop through the person collection in DeletePersonEventArgs and remove them from the internal persons list.
    Code:
            void form_DeleteClicked(object sender, DeletePersonEventArgs e)
            {
                foreach (Person p in e.PersonsToDelete)
                    people.Remove(p);
    
                form.UpdateView(people);
            }
    Note that foreach should not be used when removing elements of a collection being iterated by that same foreach loop - but here you're removing items from a different collection, so it's OK.

    Quote Originally Posted by plywoods View Post
    The same issues for Update code.
    Use similar approach - start by modifying the UpdatePersonEventArgs class. You can also put some additional properties into the UpdatePersonEventArgs to store things like, say, the index of the selected item so that you can potentially avoid updating the entire list view, etc.

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