Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A Practical Use of the MVC Pattern

, 8 Jan 2007 CPOL
Another approach to the MVC pattern
using System;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using MvcExample.Models;

namespace MvcExample.Views
{
	public class BaseView : Form
	{
		public event EventHandler ViewClosed;

		#region Windows Form Designer generated code
		protected System.Windows.Forms.Button butClose;
		protected System.Windows.Forms.ProgressBar progressBar;

		private Container components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}


		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.butClose = new System.Windows.Forms.Button();
			this.progressBar = new System.Windows.Forms.ProgressBar();
			this.SuspendLayout();
			// 
			// butClose
			// 
			this.butClose.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.butClose.Location = new System.Drawing.Point(200, 240);
			this.butClose.Name = "butClose";
			this.butClose.Size = new System.Drawing.Size(80, 24);
			this.butClose.TabIndex = 2;
			this.butClose.Text = "&Close";
			this.butClose.Click += new System.EventHandler(this.butClose_Click);
			// 
			// progressBar
			// 
			this.progressBar.Location = new System.Drawing.Point(8, 248);
			this.progressBar.Name = "progressBar";
			this.progressBar.Size = new System.Drawing.Size(184, 16);
			this.progressBar.TabIndex = 3;
			// 
			// BaseView
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 271);
			this.Controls.Add(this.progressBar);
			this.Controls.Add(this.butClose);
			this.Name = "BaseView";
			this.Text = "BaseView";
			this.ResumeLayout(false);

		}
		#endregion

		#region constructor
		public BaseView()
		{
			InitializeComponent();
			this.Closed += new EventHandler(OnViewClosed);
		}
		#endregion constructor

		#region form eventhandlers
		private void OnViewClosed(object sender, EventArgs ea)
		{
			if (ViewClosed != null)
			{
				ViewClosed(this, ea);
			}
		}

		private void butClose_Click(object sender, EventArgs e)
		{
			this.Close();
		}
		#endregion form eventhandlers

		#region public methods 
		/// <summary>
		/// Called from the controller to update the View when the Model 
		/// is finished loading the data.
		/// </summary>
		public void UpdateView(object sender, EventArgs ea)
		{
			BaseModel model = sender as BaseModel;
			if (model == null)
				return;

			DataSet ds = model.QueryModel();
			ContinueUpdateView(ref ds);

			progressBar.Visible = false;
		}

		/// <summary>
		/// Called from the controller to update the View's progressbar when
		/// the Model is reporting some progress.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="percent"></param>
		public void UpdateProgress(object sender, int percent)
		{
			progressBar.Visible = true;

			if (percent >= 0 && percent <= 100)
			{
				progressBar.Value = percent;
			}
			else
			{
				progressBar.Value = 0;
			}
		}
		#endregion public methods

		#region virtual methods
		/// <summary>
		/// This method could be declared as abstract (and then the whole 
		/// BaseView class as an abstract class) but then we lose the ability to edit the
		/// form in the form editor.
		/// </summary>
		/// <param name="ds"></param>
		protected virtual void ContinueUpdateView(ref DataSet ds)
		{
		}
		#endregion virtual methods
	}
}

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)

Share

About the Author

Zoltan Balazs
Software Developer
Romania Romania
I've did some programming for Macintosh a few years back and working with Microsoft technologies since then.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 8 Jan 2007
Article Copyright 2007 by Zoltan Balazs
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid