Sep 17, 2010

MVC sample Program

Simple Example of MVC (Model View Controller) Design Pattern for Abstraction in Visual Studio 2005/C#

Introduction

Model-view-controller (MVC) is a pattern used to isolate business logic from the user interface. Using MVC, the Model represents the information (the data) of the application and the business rules used to manipulate the data, the View corresponds to elements of the user interface such as text, checkbox items, and so forth, and the Controller manages details involving the communication between the model and view. The controller handles user actions such as keystrokes and mouse movements and pipes them into the model or view as required.
Figure4.gif

steps:

1.open new Project in visual studio 2005.

2.then create form like










 form1.cs


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{

    public interface frmAddView
    {
        void addListener(AddController contoller);
        string Total
        {
            get;
            set;
        }
    };




    public partial class frmAddViewClass:Form,frmAddView
    {
        AddController contoller;
        public frmAddViewClass()
        {
            InitializeComponent();
        }
        public void addListener(AddController contoller)
        {
            this.contoller = contoller;
        }

        public string Total
        {
            get
            {
                return txt_Result.Text;
            }
            set
            {
             txt_Result.Text=value;
            }
        }
        private void frmAddViewClass_Load(object sender, EventArgs e)
        {

        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            contoller.onclick(txt_first.Text,txt_second.Text);
        }

        private void btn_Clear_Click(object sender, EventArgs e)
        {
            txt_first.Text = "";
            txt_second.Text = "";
            txt_Result.Text = "";
        }

    
    }



    public interface AddController
    {
        void onclick(string num1, string num2);
    };

    public class AddControllerClass : AddController
    {
        frmAddView view;
        AddModel model;


        public AddControllerClass(frmAddView view,AddModel model)
        {
            this.model = model;
            this.view = view;
            this.view.addListener(this);
        }
        public void onclick(string num1, string num2)
        {
            view.Total = model.addnum(num1, num2).ToString();
        }
    }




    public interface AddModel
    {
        string addnum(string num1, string num2);
    };





    public class AddModelClass : AddModel
    {
        public AddModelClass()
        {
        }
       
        public string addnum(string num1, string num2)
        {
            return num1 + num2;
        }
    }




  
}




program.cs:


using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace MVCExample
{
    static class Program
    {
        ///
        /// The main entry point for the application.
        ///

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            frmCalcView view = new frmCalcView();
            CalculatorModel model = new CalculatorModel();
            CalcController controller = new CalcController(model, view);

            Application.Run(view);
        }
    }
}


form1.designer.cs:

namespace WindowsApplication1
{
    partial class frmAddViewClass
    {
        ///
        /// Required designer variable.
        ///

        private System.ComponentModel.IContainer components = null;

        ///
        /// Clean up any resources being used.
        ///

        ///
true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        ///
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        ///

        private void InitializeComponent()
        {
            this.txt_first = new System.Windows.Forms.TextBox();
            this.txt_second = new System.Windows.Forms.TextBox();
            this.btn_add = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.txt_Result = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.btn_Clear = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // txt_first
            //
            this.txt_first.Location = new System.Drawing.Point(112, 6);
            this.txt_first.Name = "txt_first";
            this.txt_first.Size = new System.Drawing.Size(85, 20);
            this.txt_first.TabIndex = 0;
            //
            // txt_second
            //
            this.txt_second.Location = new System.Drawing.Point(112, 32);
            this.txt_second.Name = "txt_second";
            this.txt_second.Size = new System.Drawing.Size(85, 20);
            this.txt_second.TabIndex = 1;
            //
            // btn_add
            //
            this.btn_add.Location = new System.Drawing.Point(54, 58);
            this.btn_add.Name = "btn_add";
            this.btn_add.Size = new System.Drawing.Size(60, 23);
            this.btn_add.TabIndex = 2;
            this.btn_add.Text = "ADD";
            this.btn_add.UseVisualStyleBackColor = true;
            this.btn_add.Click += new System.EventHandler(this.btn_add_Click);
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(90, 13);
            this.label1.TabIndex = 3;
            this.label1.Text = "Enter The 1st NO";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(12, 35);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(88, 13);
            this.label2.TabIndex = 4;
            this.label2.Text = "Enter the 2nd No";
            //
            // txt_Result
            //
            this.txt_Result.Location = new System.Drawing.Point(112, 87);
            this.txt_Result.Name = "txt_Result";
            this.txt_Result.Size = new System.Drawing.Size(85, 20);
            this.txt_Result.TabIndex = 5;
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(12, 94);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(75, 13);
            this.label3.TabIndex = 6;
            this.label3.Text = "Result of ADD";
            //
            // btn_Clear
            //
            this.btn_Clear.Location = new System.Drawing.Point(120, 58);
            this.btn_Clear.Name = "btn_Clear";
            this.btn_Clear.Size = new System.Drawing.Size(60, 23);
            this.btn_Clear.TabIndex = 7;
            this.btn_Clear.Text = "Clear";
            this.btn_Clear.UseVisualStyleBackColor = true;
            this.btn_Clear.Click += new System.EventHandler(this.btn_Clear_Click);
            //
            // frmAddViewClass
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(201, 116);
            this.Controls.Add(this.btn_Clear);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.txt_Result);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.btn_add);
            this.Controls.Add(this.txt_second);
            this.Controls.Add(this.txt_first);
            this.Name = "frmAddViewClass";
            this.Text = "CONCATINATION";
            this.Load += new System.EventHandler(this.frmAddViewClass_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox txt_first;
        private System.Windows.Forms.TextBox txt_second;
        private System.Windows.Forms.Button btn_add;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox txt_Result;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Button btn_Clear;
    }
}

No comments:

Post a Comment