If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Hybrid View

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.

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:

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:

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).

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.

Originally Posted by plywoods

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.