[프로퍼티 그리드 컨트롤을 다루어 보자.]
프로퍼티 그리드란 이름 그대로 속성의 값을 편집할수 있도록
자동으로 폼디자인을 제공하는 툴이다.
닷넷 편집기에서 사용하는 속성창과 똑같이 작동된다.

우리가 VS.NET툴을 사용하면서 사용하는 속성창과 다를것이 없다.
그럼 이제 컨트롤을 추가하여 작성하는 방법을 알아보도록 하겠다.

먼저 윈도우폼 프로젝트를 하나 새로 만든다. 그리고 왼쪽 도구상자의 메뉴에서 도구 추가삭제를 선택한다.
그러면 아래 쪽에 내려보면 PropertyGrid라는 컨트롤이 있을것이다. 이 컨트롤을 선택한다.

그리고 선택한 컨트롤을 폼위의 올려 놓으면 디자인 타임에서 해야할 작업은 간단하게 끝나게 된다.
이제 속성이 담긴 클래스를 바인딩하는 방법과 카테고리를 나누는 방법을 알아 보도록하자.
먼저 많이 사용하는 데이터 그리드는 데이터 테이블이나 데이터 셋을 바인딩을 시킨다.
이와같이 프로퍼티 그리드는 인스턴스화된 클래스를 바인딩 시키게 된다.
바인딩부분을 살펴보자.
private void Form1_Load(object sender, System.EventArgs e)
{
ProPertyClass uClass=new ProPertyClass();
propertyGrid1.SelectedObject=uClass;
}
단순히 클래스를 인스턴스화 하고 SelectedObject에 Object를 전달하여 주는것이로
정말 간단하게 프로그램이 끝나게 되었다. 그럼ProPertyClass라는 클래스에서
사용한 프로퍼티를 살펴보자.
public class ProPertyClass
{
public ProPertyClass(){}
private string _이름;
private int _나이;
private Color _색깔;
private Font _폰트;
[Category("신상정보"), Description("이름을 적으세요.")]
public string 이름
{
get{return _이름;}
set{_이름=value;}
}
[Category("신상정보"), Description("나이를 적으세요.")]
public int 나이
{
get{return _나이;}
set{_나이=value;}
}
[Category("_기타"), Description("선의 굵기Size 입니다.")]
public Color 색깔
{
get{return _색깔;}
set{_색깔=value;}
}
[Category("기타"), Description("폰트를 선택하세요.")]
public Font 폰트
{
get{return _폰트;}
set{_폰트=value;}
}
}
프로퍼티 그리드는 속성의 형식에 맞는 입력 형태를 만들어준다.
그리고 자동으로 Validator체크 하여 준다.
여기서 살펴본것은 Font 타입이나 Color라는 속성을 이용해보았다.
이것뿐만 아니라 배열이나 Array 같은 형식들도 한번 바인딩 하여 사용하여 보자.

전체소스
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace PropertyGrid
{
/// <summary>
/// Form1에 대한 요약 설명입니다.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PropertyGrid propertyGrid1;
/// <summary>
/// 필수 디자이너 변수입니다.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows Form 디자이너 지원에 필요합니다.
//
InitializeComponent();
//
// TODO: InitializeComponent를 호출한 다음 생성자 코드를 추가합니다.
//
}
/// <summary>
/// 사용 중인 모든 리소스를 정리합니다.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form 디자이너에서 생성한 코드
/// <summary>
/// 디자이너 지원에 필요한 메서드입니다.
/// 이 메서드의 내용을 코드 편집기로 수정하지 마십시오.
/// </summary>
private void InitializeComponent()
{
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.SuspendLayout();
//
// propertyGrid1
//
this.propertyGrid1.CommandsVisibleIfAvailable = true;
this.propertyGrid1.LargeButtons = false;
this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid1.Location = new System.Drawing.Point(152, 8);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.Size = new System.Drawing.Size(192, 288);
this.propertyGrid1.TabIndex = 0;
this.propertyGrid1.Text = "propertyGrid1";
this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid1.ViewForeColor = System.Drawing.SystemColors.WindowText;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(352, 317);
this.Controls.Add(this.propertyGrid1);
this.Name = "Form1";
this.Text = "PropertyGrid";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 해당 응용 프로그램의 주 진입점입니다.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
ProPertyClass uClass=new ProPertyClass();
propertyGrid1.SelectedObject=uClass;
}
}
public class ProPertyClass
{
public ProPertyClass(){}
private string _이름;
private int _나이;
private Color _색깔;
private Font _폰트;
[Category("신상정보"), Description("이름을 적으세요.")]
public string 이름
{
get{return _이름;}
set{_이름=value;}
}
[Category("신상정보"), Description("나이를 적으세요.")]
public int 나이
{
get{return _나이;}
set{_나이=value;}
}
[Category("_기타"), Description("선의 굵기Size 입니다.")]
public Color 색깔
{
get{return _색깔;}
set{_색깔=value;}
}
[Category("기타"), Description("폰트를 선택하세요.")]
public Font 폰트
{
get{return _폰트;}
set{_폰트=value;}
}
}
}
|
|
- Low-Level 키보드 입력 후킹 (0)2007/01/25
- 인터넷 웹 브라우저를 만들어보자. (0)2007/01/23
- 프로퍼티 그리드 컨트롤을 다루어 보자. (0)2007/01/23
- 윈도우 종료/재부팅 (0)2006/11/24
- 해당 드라이브 타입 알아내기 (0)2006/11/24

수안이의 컴퓨터 연구실



Leave your greetings.