I took a crack at creating a MVC pattern for C# Add-ins (since I mostly do Java). I am not quite sure I have the event handler correct. Would love to have some feedback on this.Program.cs (the View, Model and Controller would be created in your on_click event for a button Add-in)You would also want to pass a reference of IMap to the Model and Controller.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (_view == null)
{
_view = View.getInstance();
}
if (_model == null)
{
_model = new Model(_view);
}
if (_controller == null)
{
_controller = new Controller(_view, _model);
}
Application.Run(_view);
}
private static View _view;
private static Model _model;
private static Controller _controller;
}
}
Model.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class Model
{
private View m_view;
public Model(View vw)
{
m_view = vw;
}
public void respondClick()
{
MessageBox.Show("The title of this form is: " + m_view.Text + "\r\n" + "you clicked me");
}
}
}
Controller.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
namespace WindowsFormsApplication1
{
public class Controller
{
private View c_view;
private Model c_model;
public Controller(View vw, Model md)
{
c_view = vw;
c_model = md;
c_view.getBtnClickMe().Click += handleClick;
}
private void handleClick(object sender, EventArgs e)
{
c_model.respondClick();
}
}
}
View.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class View : Form
{
public View()
{
InitializeComponent();
}
}
}
View.Designer.cs
namespace WindowsFormsApplication1
{
partial class View
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private static View view = null;
public static View getInstance()
{
if (view == null)
{
view = new View();
}
return view;
}
private void InitializeComponent()
{
this.btnClickMe = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnClickMe
//
this.btnClickMe.Location = new System.Drawing.Point(98, 60);
this.btnClickMe.Name = "btnClickMe";
this.btnClickMe.Size = new System.Drawing.Size(75, 23);
this.btnClickMe.TabIndex = 0;
this.btnClickMe.Text = "Click Me";
this.btnClickMe.UseVisualStyleBackColor = true;
//
// View
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.btnClickMe);
this.Name = "View";
this.Text = "View1";
this.ResumeLayout(false);
}
public System.Windows.Forms.Button getBtnClickMe()
{
return this.btnClickMe;
}
private System.Windows.Forms.Button btnClickMe;
}
}