Click here to Skip to main content
15,895,799 members
Articles / Desktop Programming / Windows Forms

A Practical Use of the MVC Pattern

Rate me:
Please Sign up or sign in to vote.
4.55/5 (41 votes)
8 Jan 2007CPOL5 min read 181.5K   2.7K   145  
Another approach to the MVC pattern
using System;
using System.Windows.Forms;
using MvcExample.EventHandlers;
using MvcExample.Models;
using MvcExample.Views;

namespace MvcExample.Controllers
{
	public class RptController : BaseController
	{
		#region private members
		private BaseModel model;
		private BaseView view;
		#endregion private members

		#region constructor 
		/// <summary>
		/// Instanciates the view (the representation of the data) and 
		/// the model (the data). 
		/// </summary>
		public RptController(RptControllerType controllerType)
		{
			switch (controllerType)
			{
				case RptControllerType.List:
					view = new View_List_Income();
					model = new Model_Income();
					break;

				case RptControllerType.Graph:
					view = new View_Graph_Income();
					model = new Model_Income();
					break;

				default:
					MessageBox.Show("Type of report unimplemented!", "", MessageBoxButtons.OK, MessageBoxIcon.Stop);
					return;
			}

			SubscribeControllerToView();
			SubsctribeModelToView();

			view.Show();
			model.GenerateReport();
		}
		#endregion constructor

		#region subscriber methods
		protected override void SubscribeControllerToView()
		{
			if (view == null)
				return;

			view.ViewClosed += new EventHandler(OnView_ViewClosed);
		}

		protected override void SubsctribeModelToView()
		{
			if (model == null)
				return;

			model.ModelChanged += new EventHandler(OnModel_ModelChanged);
			model.ModelProgress += new ProgressEventHandler(OnModel_ModelProgress);
		}
		#endregion subscriber methods

		#region view eventhandlers
		private void OnView_ViewClosed(object sender, EventArgs e)
		{
			if (model != null)
			{
				model.CancelBackgroundWorker();
			}
		}
		#endregion view eventhandlers

		#region model eventhandlers
		private void OnModel_ModelChanged(object sender, EventArgs ea)
		{
			if (view == null)
				return;

			if (view.InvokeRequired)
			{
				view.Invoke(
					new GenericEventHandler(view.UpdateView),
					new object[] {sender, ea});
			}
			else
			{
				view.UpdateView(sender, ea);
			}
		}

		private void OnModel_ModelProgress(object sender, int percent)
		{
			if (view == null)
				return;

			if (view.InvokeRequired)
			{
				view.Invoke(
					new ProgressEventHandler(view.UpdateProgress),
					new object[] {sender, percent});
			}
			else
			{
				view.UpdateProgress(sender, percent);
			}
		}
		#endregion model eventhandlers
	}	// RptController
}

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
Romania Romania
I've did some programming for Macintosh a few years back and working with Microsoft technologies since then.

Comments and Discussions