■ PropertyGrid 클래스에서 이미지 목록을 사용해 항목 값을 선택하는 방법을 보여준다.
▶ Direction.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
namespace TestProject { /// <summary> /// 방향 /// /// </summary> public enum Direction : int { /// <summary> /// LEFT /// </summary> LEFT, /// <summary> /// UP /// </summary> UP, /// <summary> /// RIGHT /// </summary> RIGHT, /// <summary> /// DOWN /// </summary> DOWN } } |
▶ Car.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
using System.ComponentModel; using System.Drawing.Design; namespace TestProject { /// <summary> /// 자동차 /// </summary> public class Car { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 방향 - Direction /// <summary> /// 방향 /// </summary> [Category("Car")] [Description("자동차 방향")] [Editor(typeof(DirectionEditor), typeof(UITypeEditor))] public Direction Dicrection { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Car /// <summary> /// 생성자 /// </summary> public Car() { } #endregion } } |
▶ DirectionEditor.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
using System.ComponentModel; using System.Drawing; using System.Drawing.Design; namespace TestProject { /// <summary> /// 방향 에디터 /// </summary> public class DirectionEditor : UITypeEditor { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 페인트 값 지원 여부 구하기 - GetPaintValueSupported(context) /// <summary> /// 페인트 값 지원 여부 구하기 /// </summary> /// <param name="context">컨텍스트</param> /// <returns>페인트 값 지원 여부</returns> public override bool GetPaintValueSupported(ITypeDescriptorContext context) { return true; } #endregion #region 값 페인트 하기 - PaintValue(e) /// <summary> /// 값 페인트 하기 /// </summary> /// <param name="e">이벤트 인자</param> public override void PaintValue(PaintValueEventArgs e) { Direction value = (Direction)e.Value; Image image = null; switch(value) { case Direction.LEFT : image = Properties.Resources.left; break; case Direction.UP : image = Properties.Resources.up; break; case Direction.RIGHT : image = Properties.Resources.right; break; case Direction.DOWN : image = Properties.Resources.down; break; } int x = (e.Bounds.Width - image.Width) / 2 + e.Bounds.X; int y = e.Bounds.Y; e.Graphics.DrawImage(image, x, y); } #endregion } } |
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
using System; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.setButton.Click += setButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 설정 버튼 클릭시 처리하기 - setButton_Click(sender, e) /// <summary> /// 설정 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void setButton_Click(object sender, EventArgs e) { Car car = new Car(); car.Dicrection = Direction.DOWN; propertyGrid.SelectedObject = car; } #endregion } } |