닷넷, C#, MVC-View 프로젝트 실습
- MVC-Controller, MVC-Model 프로젝트를 참조추가.
[UserView.designer.cs]
namespace WinFormMVC.View
{
partial class UsersView
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txtID = new System.Windows.Forms.TextBox();
this.lblID = new System.Windows.Forms.Label();
this.btnRemove = new System.Windows.Forms.Button();
this.grpDetails = new System.Windows.Forms.GroupBox();
this.grbSex = new System.Windows.Forms.GroupBox();
this.rdFamele = new System.Windows.Forms.RadioButton();
this.rdMale = new System.Windows.Forms.RadioButton();
this.txtDepartment = new System.Windows.Forms.TextBox();
this.lblDepartment = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.lblName = new System.Windows.Forms.Label();
this.btnAdd = new System.Windows.Forms.Button();
this.btnRegister = new System.Windows.Forms.Button();
this.userList = new System.Windows.Forms.ListView();
this.grpDetails.SuspendLayout();
this.grbSex.SuspendLayout();
this.SuspendLayout();
//
// txtID
//
this.txtID.Location = new System.Drawing.Point(454, 26);
this.txtID.Name = "txtID";
this.txtID.Size = new System.Drawing.Size(241, 21);
this.txtID.TabIndex = 5;
//
// lblID
//
this.lblID.Location = new System.Drawing.Point(370, 29);
this.lblID.Name = "lblID";
this.lblID.Size = new System.Drawing.Size(93, 21);
this.lblID.TabIndex = 25;
this.lblID.Text = "사 번 : ";
//
// btnRemove
//
this.btnRemove.Location = new System.Drawing.Point(748, 50);
this.btnRemove.Name = "btnRemove";
this.btnRemove.Size = new System.Drawing.Size(113, 21);
this.btnRemove.TabIndex = 32;
this.btnRemove.Text = "&Remove User";
this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
//
// grpDetails
//
this.grpDetails.Controls.Add(this.grbSex);
this.grpDetails.Controls.Add(this.txtDepartment);
this.grpDetails.Controls.Add(this.lblDepartment);
this.grpDetails.Controls.Add(this.txtID);
this.grpDetails.Controls.Add(this.lblID);
this.grpDetails.Controls.Add(this.txtName);
this.grpDetails.Controls.Add(this.lblName);
this.grpDetails.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.grpDetails.Location = new System.Drawing.Point(14, 11);
this.grpDetails.Name = "grpDetails";
this.grpDetails.Size = new System.Drawing.Size(716, 133);
this.grpDetails.TabIndex = 34;
this.grpDetails.TabStop = false;
this.grpDetails.Text = "Register new user :";
//
// grbSex
//
this.grbSex.Controls.Add(this.rdFamele);
this.grbSex.Controls.Add(this.rdMale);
this.grbSex.ForeColor = System.Drawing.SystemColors.MenuText;
this.grbSex.Location = new System.Drawing.Point(372, 53);
this.grbSex.Name = "grbSex";
this.grbSex.Size = new System.Drawing.Size(322, 50);
this.grbSex.TabIndex = 29;
this.grbSex.TabStop = false;
this.grbSex.Text = "성 별";
//
// rdFamele
//
this.rdFamele.Location = new System.Drawing.Point(161, 18);
this.rdFamele.Name = "rdFamele";
this.rdFamele.Size = new System.Drawing.Size(78, 22);
this.rdFamele.TabIndex = 5;
this.rdFamele.Text = "Female";
//
// rdMale
//
this.rdMale.Location = new System.Drawing.Point(66, 18);
this.rdMale.Name = "rdMale";
this.rdMale.Size = new System.Drawing.Size(62, 22);
this.rdMale.TabIndex = 4;
this.rdMale.Text = "Male";
//
// txtDepartment
//
this.txtDepartment.Location = new System.Drawing.Point(100, 71);
this.txtDepartment.Name = "txtDepartment";
this.txtDepartment.Size = new System.Drawing.Size(246, 21);
this.txtDepartment.TabIndex = 27;
//
// lblDepartment
//
this.lblDepartment.Location = new System.Drawing.Point(21, 71);
this.lblDepartment.Name = "lblDepartment";
this.lblDepartment.Size = new System.Drawing.Size(93, 21);
this.lblDepartment.TabIndex = 28;
this.lblDepartment.Text = "부 서 :";
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(100, 26);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(246, 21);
this.txtName.TabIndex = 1;
//
// lblName
//
this.lblName.Location = new System.Drawing.Point(21, 29);
this.lblName.Name = "lblName";
this.lblName.Size = new System.Drawing.Size(73, 21);
this.lblName.TabIndex = 19;
this.lblName.Text = "셩 명 : ";
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(748, 23);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(113, 21);
this.btnAdd.TabIndex = 31;
this.btnAdd.Text = "&Add New User";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnRegister
//
this.btnRegister.Location = new System.Drawing.Point(748, 113);
this.btnRegister.Name = "btnRegister";
this.btnRegister.Size = new System.Drawing.Size(113, 21);
this.btnRegister.TabIndex = 33;
this.btnRegister.Text = "&Save";
this.btnRegister.Click += new System.EventHandler(this.btnRegister_Click);
//
// userList
//
this.userList.Dock = System.Windows.Forms.DockStyle.Bottom;
this.userList.FullRowSelect = true;
this.userList.GridLines = true;
this.userList.Location = new System.Drawing.Point(0, 150);
this.userList.Name = "userList";
this.userList.Size = new System.Drawing.Size(875, 274);
this.userList.Sorting = System.Windows.Forms.SortOrder.Ascending;
this.userList.TabIndex = 35;
this.userList.UseCompatibleStateImageBehavior = false;
this.userList.View = System.Windows.Forms.View.Details;
this.userList.SelectedIndexChanged += new System.EventHandler(this.userList_SelectedIndexChanged);
//
// UsersView
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(875, 424);
this.Controls.Add(this.userList);
this.Controls.Add(this.btnRemove);
this.Controls.Add(this.grpDetails);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.btnRegister);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "UsersView";
this.Text = "사용자 리스트";
this.grpDetails.ResumeLayout(false);
this.grpDetails.PerformLayout();
this.grbSex.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
internal System.Windows.Forms.TextBox txtID;
internal System.Windows.Forms.Label lblID;
internal System.Windows.Forms.Button btnRemove;
internal System.Windows.Forms.GroupBox grpDetails;
internal System.Windows.Forms.TextBox txtName;
internal System.Windows.Forms.Label lblName;
internal System.Windows.Forms.Button btnAdd;
internal System.Windows.Forms.Button btnRegister;
internal System.Windows.Forms.ListView userList;
internal System.Windows.Forms.TextBox txtDepartment;
internal System.Windows.Forms.Label lblDepartment;
private System.Windows.Forms.GroupBox grbSex;
internal System.Windows.Forms.RadioButton rdFamele;
internal System.Windows.Forms.RadioButton rdMale;
}
}
[UserView.cs]
using System;
using System.Windows.Forms;
using WinFormMVC.Controller;
using WinFormMVC.Model;
namespace WinFormMVC.View
{
public partial class UsersView : Form, IUsersView
{
public UsersView()
{
InitializeComponent();
}
UsersController _controller;
#region Events raised back to controller
// 사용자 추가 모드, 상단 입력화면을 지우고 입력이 가능하도록
private void btnAdd_Click(object sender, EventArgs e)
{
this._controller.AddNewUser();
}
private void btnRemove_Click(object sender, EventArgs e)
{
DialogResult result1 = MessageBox.Show(GetIdOfSelectedUserInGrid()
+ " User를 삭제하시겠습니까? ",
"Delete User Message",
MessageBoxButtons.YesNo);
if (result1 == DialogResult.Yes) this._controller.RemoveUser();
}
//Save 버튼 클릭
private void btnRegister_Click(object sender, EventArgs e)
{
DialogResult result1 = MessageBox.Show(" 저장 하시겠습니까? ",
"Save User Message",
MessageBoxButtons.YesNo);
if (result1 == DialogResult.Yes) this._controller.Save();
}
private void userList_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.userList.SelectedItems.Count > 0)
this._controller.SelectedUserChanged(this.userList.SelectedItems[0].Text);
}
#endregion
#region ICatalogView implementation
public void SetController(UsersController controller)
{
_controller = controller;
}
//ListView의 컬럼을 정의하고 items을 Clear
public void ClearGrid()
{
// Define columns in grid
this.userList.Columns.Clear();
this.userList.Columns.Add("Id", 200, HorizontalAlignment.Left);
this.userList.Columns.Add("Name", 250, HorizontalAlignment.Left);
this.userList.Columns.Add("Department", 300, HorizontalAlignment.Left);
this.userList.Columns.Add("Sex", 120, HorizontalAlignment.Left);
this.userList.Items.Clear();
}
// 인자로 입력되는 User를 ListView에 추가
public void AddUserToGrid(User usr)
{
ListViewItem parent;
parent = this.userList.Items.Add(usr.ID);
parent.SubItems.Add(usr.Name);
parent.SubItems.Add(usr.Department);
parent.SubItems.Add(Enum.GetName(typeof(User.SexOfPerson), usr.Sex));
}
// 인자로 입력되는 User로 ListView를 변경
public void UpdateGridWithChangedUser(User usr)
{
ListViewItem rowToUpdate = null;
foreach (ListViewItem row in this.userList.Items)
{
if (row.Text == usr.ID)
{
rowToUpdate = row;
}
}
if (rowToUpdate != null)
{
rowToUpdate.Text = usr.ID;
rowToUpdate.SubItems[1].Text = usr.Name;
rowToUpdate.SubItems[2].Text = usr.Department;
rowToUpdate.SubItems[3].Text = Enum.GetName(typeof(User.SexOfPerson), usr.Sex);
}
}
// 인자로 넘어오는 User 객체를 ListView에서 삭제
public void RemoveUserFromGrid(User usr)
{
ListViewItem rowToRemove = null;
foreach (ListViewItem row in this.userList.Items)
{
if (row.Text == usr.ID)
{
rowToRemove = row;
}
}
if (rowToRemove != null)
{
this.userList.Items.Remove(rowToRemove);
//삭제하고 다음 User에 Focus를 준다.
this.userList.Focus();
}
}
//현재 선택된 User의 ID를 문자열로 리턴
public string GetIdOfSelectedUserInGrid()
{
if (this.userList.SelectedItems.Count > 0)
return this.userList.SelectedItems[0].Text;
else
return "";
}
// 인자로 넘어오는 User 객체를 ListView에서 검색해서
// 선택이 되도록 한다.
public void SetSelectedUserInGrid(User usr)
{
foreach (ListViewItem row in this.userList.Items)
{
if (row.Text == usr.ID)
{
//ListView의 해당 데이터가 Select되도록 함으로써
//ListView의 SelectedIndexChaged 이벤트가 발생한다.
row.Selected = true;
}
}
}
public string Name
{
get { return this.txtName.Text; }
set { this.txtName.Text = value; }
}
public string ID
{
get { return this.txtID.Text; }
set { this.txtID.Text = value; }
}
public string Department
{
get { return this.txtDepartment.Text; }
set { this.txtDepartment.Text = value; }
}
public User.SexOfPerson Sex
{
get
{
if (this.rdMale.Checked)
return User.SexOfPerson.Male;
else
return User.SexOfPerson.Female;
}
set
{
if (value == User.SexOfPerson.Male)
this.rdMale.Checked = true;
else
this.rdFamele.Checked = true;
}
}
public bool CanModifyID
{
set { this.txtID.Enabled = value; }
}
#endregion
}
}
6. MVC-View 프로젝트
- MVC-Model, MVC-View, MVC-Controller 프로젝트를 참조추가
[Program.cs]
using System;
using System.Collections;
using WinFormMVC.Model;
using WinFormMVC.View;
using WinFormMVC.Controller;
namespace UseMVCApplication
{
static class Program
{
[STAThread]
static void Main()
{
UsersView view = new UsersView();
view.Visible = false;
// Add some dummy data
IList users = new ArrayList();
users.Add(new User("가길동", "1", "영업부", User.SexOfPerson.Male));
users.Add(new User("나길순", "2", "경리부", User.SexOfPerson.Female));
users.Add(new User("다길동", "3", "개발부", User.SexOfPerson.Male));
users.Add(new User("라길순", "4", "경리부", User.SexOfPerson.Female));
users.Add(new User("마길동", "5", "총무부", User.SexOfPerson.Male));
users.Add(new User("바길순", "6", "총무부", User.SexOfPerson.Female));
users.Add(new User("사길동", "7", "총무부", User.SexOfPerson.Male));
users.Add(new User("아길동", "8", "영업부", User.SexOfPerson.Male));
users.Add(new User("자길동", "9", "경리부", User.SexOfPerson.Male));
UsersController controller = new UsersController(view, users);
controller.LoadView();
view.ShowDialog();
}
}
}
#닷넷, #MVC, #윈폼MVC, #윈폼