Click here to Skip to main content
15,885,906 members
Articles / Desktop Programming / Windows Forms

The Model-View-Controller(MVC) Pattern with C#/WinForms

Rate me:
Please Sign up or sign in to vote.
4.94/5 (130 votes)
30 Dec 2013CPOL5 min read 873.8K   49.8K   301  
Briefly and clearly about MVC implementation, without lengthy discussion or minor details
using System;
using System.Collections;
using WinFormMVC.Model;

namespace WinFormMVC.Controller
{
    public class UsersController
    {
        IUsersView _view;
        IList      _users;
        User       _selectedUser;

        public UsersController(IUsersView view, IList users)
        {
            _view = view;
            _users = users;
            view.SetController(this);
        }

        public IList Users
        {
           get { return ArrayList.ReadOnly(_users); }
        }

        private void updateViewDetailValues(User usr)
        {
           _view.FirstName   =  usr.FirstName;
           _view.LastName    =  usr.LastName;
           _view.ID          =  usr.ID;
           _view.Department  =  usr.Department;
           _view.Sex         =  usr.Sex;
        }

        private void updateUserWithViewValues(User usr)
        {
           usr.FirstName     =  _view.FirstName;
           usr.LastName      =  _view.LastName;
           usr.ID            =  _view.ID;
           usr.Department    =  _view.Department;
           usr.Sex           =  _view.Sex;
        }


        public void LoadView()
        {
            _view.ClearGrid();
            foreach (User usr in _users)
                _view.AddUserToGrid(usr);

            _view.SetSelectedUserInGrid((User)_users[0]);

        }

        public void SelectedUserChanged(string selectedUserId)
        {
            foreach (User usr in this._users)
            {
                if (usr.ID == selectedUserId)
                {
                    _selectedUser = usr;
                    updateViewDetailValues(usr);
                    _view.SetSelectedUserInGrid(usr);
                    this._view.CanModifyID = false;
                    break;
                }
            }
        }


        public void AddNewUser()
        {
            _selectedUser = new User(""/*firstname*/, 
                                     "" /*lastname*/, 
                                     ""  /*id*/, 
                                     "" /*department*/,
                                     User.SexOfPerson.Male /*sex*/);
                                     
            this.updateViewDetailValues(_selectedUser);
            this._view.CanModifyID = true;
        }

        public void RemoveUser()
        {
            string id = this._view.GetIdOfSelectedUserInGrid();
            User userToRemove = null;

            if (id != "")
            {
                foreach (User usr in this._users)
                {
                    if (usr.ID == id)
                    {
                        userToRemove = usr;
                        break;
                    }
                }

                if (userToRemove != null)
                {
                    int newSelectedIndex = this._users.IndexOf(userToRemove);
                    this._users.Remove(userToRemove);
                    this._view.RemoveUserFromGrid(userToRemove);

                    if (newSelectedIndex > -1 && newSelectedIndex < _users.Count)
                    {
                        this._view.SetSelectedUserInGrid((User)_users[newSelectedIndex]);
                    }
                }
            }
        }

        public void Save()
        {
            updateUserWithViewValues(_selectedUser);
            if (!this._users.Contains(_selectedUser))
            {
                // Add new user
                this._users.Add(_selectedUser);
                this._view.AddUserToGrid(_selectedUser);
            }
            else
            {
                // Update existing
                this._view.UpdateGridWithChangedUser(_selectedUser);
            }
            _view.SetSelectedUserInGrid(_selectedUser);
            this._view.CanModifyID = false;

        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Canada Canada
Mr.Volynsky Alex is a Software Engineer in a leading software company. Alex is skilled in many areas of computer science. He has over 14 years of experience in the design & development of applications using C/C++/STL, Python, Qt, MFC, DirectShow, JavaScript, VBScript, Bash and of course - C#/.NET.

In addition, Alex is the active member of Intel® Developer Zone (he was awarded by Intel® Green Belt for his active contribution to the Intel Developer Zone community for developers using Intel technology).

Alex is also interested in the Objective-C development for the iPad/iPhone platforms and he is the developer of the free 15-puzzle game on the App Store.

Overall, Alex is very easy to work with. He adapts to new systems and technology while performing complete problem definition research.

His hobbies include yacht racing, photography and reading in multiple genres.
He is also fascinated by attending computer meetings in general, loves traveling, and also takes pleasure in exercising and relaxing with friends.

Visit his C++ 11 blog

Comments and Discussions