Winform Runtime Debugging

Published Jul 31, 2022 | Updated May 19, 2023 | 0 comments

윈폼으로 개발시 UI 디자인을 하다보면 코드를 매번 수정할 때 마다 종료 => 디버깅 재시작이 매우 번거로운 경우가 있다.

그래서 예전부터 개인적으로 사용하던 방법이 있는데, Property Grid 를 런타임에 표시해서 일부 컨트롤의 속성을 변경하면 나름 좀 편하다.

아래 스크린샷을 보면 우측 상단 콤보 박스에 나열된 컨트롤 목록을 볼 수 있다.

컨트롤을 선택하면 해당 컨트롤의 속성이 하단 Property Grid 에 표시가 되고,

Property Grid 에서 속성을 변경하면 좌측 화면에 바로 바로 반영된다.

UI 디자인 할때 위치나 크기 때문에 매번 재실행 하는것 보다 이렇게 미리 한번에 확인하면 좀 더 편리하게 작업이 가능하다.

Property Grid 를 그대로 사용하기 때문에 Visual Studio 개발환경과 동일한 장점도 있다.

위의 방식대로 구현하기 위한 코드를 간단하게 작성해 봅니다.

먼저 콤보박스에 바인딩할 PropertyItem 이란 Class 와 Runtime Property 화면을 생성할 Extension Class 를 하나 추가합니다.

using System;
using System.Windows.Forms;

namespace RuntimeProperty
{
	public class PropertyItem
	{
		public object Ctl { get; set; }

		public string? Display { get; set; }

		public PropertyItem(object ctl, string? display)
		{
			this.Ctl = ctl;
			Display = display;
		}
		public int CompareTo(PropertyItem other)
		{
			return Convert.ToInt32(Ctl.Equals(other.Ctl));
		}

		public override string ToString()
		{
			return Display ?? "Null";
		}
	}
	public static class Extensions
	{
		#region Show Property
		public static void ShowProperty(this object obj, params object[] objOthers)
		{
			Form frm = new Form();
			frm.Text = frm.Name = "Runtime Property";
			ComboBox cmb = new ComboBox();
			if (obj is Control)
			{
				((Control)obj).RecursiveControlsAddToCombo(cmb);
			}
			else
			{
				cmb.Items.Add(new PropertyItem(obj, $"[{obj.GetType().Name}] - {obj.ToString()}"));
			}
			if (objOthers != null)
			{
				foreach (object objOther in objOthers)
				{
					if (objOther is Control)
					{
						((Control)objOther).RecursiveControlsAddToCombo(cmb);
					}
					else
					{
						cmb.Items.Add(new PropertyItem(obj, $"[{obj.GetType().Name}] - {obj.ToString()}"));
					}
				}
			}
			frm.Controls.Add(cmb);
			cmb.Dock = DockStyle.Top;

			PropertyGrid pg = new PropertyGrid();
			frm.Controls.Add(pg);
			pg.Dock = DockStyle.Fill;
			pg.BringToFront();

			cmb.SelectedIndexChanged += (sender, args) => { pg.SelectedObject = ((PropertyItem)cmb.SelectedItem).Ctl; };
			if (cmb.Items.Count > 0)
			{
				cmb.SelectedIndex = 0;
			}

			//frm.TopMost = true;
			frm.Show();
			frm.BringToFront();
		}

		private static void RecursiveControlsAddToCombo(this Control ctl, ComboBox cmb)
		{
			cmb.Items.Add(new PropertyItem(ctl, $"[{ctl.GetType().Name}] - {ctl.Name}"));
			foreach (Control ctlInner in ctl.Controls)
			{
				ctlInner.RecursiveControlsAddToCombo(cmb);
			}
		}

		#endregion // Show Property
	}
}

그 다음은 메인 폼 소스 코드에 Extension Method 를 호출하기 위해 단축키로 실행하는 코드를 추가합니다.

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			if (keyData == (Keys.Control | Keys.Shift | Keys.P))
			{
				this.ShowProperty();
				return true;
			}

			return base.ProcessCmdKey(ref msg, keyData);
		}

소스 코드는 다음 Github 에 적용되어 있습니다.


https://github.com/kkomzi7179/MdiTabStripCS
0 forks.
0 stars.
0 open issues.

Recent commits:

TAG INFORMATION

Learn more on this topic

Related Blog Posts

중국어 간자체 변환

Public Function ToSCUnicode(ByVal str As String) As String Dim arrByte_GBK As Byte() Dim arrByte_UTF16 As Byte() Dim strUTF16 As String ' 현재 시스템 인코딩 Dim _defaultEncoding As Encoding = System.Text.Encoding.GetEncoding(0) ' Chinese Simplified (GB2312) Dim encGBK As...

read more
DevExpress DisableThreadingProblemsDetection

DevExpress DisableThreadingProblemsDetection

DevExpress 를 8.1.6 을 쓰다가 전체적으로 8.3.3 으로 업그레이드 했다… 리빌드 할땐 문제없다… but… 런타임에 에러가 생긴다… 머지?? 처음엔 내가 Catch 안한 오류가 있나 해서 소스를 다시 한번 살펴봤다… 근데.. 소스는 바뀐게 없는데… 이상하다 그래서 자세히 살펴보니 Cross Thread… 얼레.. 분명히 왠만한건 InvokeRequired 체크해서 다 되어있는데…. 물론 이 부분도 소스를 수정하지 않았기 때문에 이전에 없던 문제인데…...

read more

DevExpress XtraGrid 에서 선택된 행 유지

출처 : http://www.devexpress.com/Support/Center/KB/p/A364.aspx?searchtext=two+gridview&tid=4b2d6f97-c4ae-48fc-87f6-8c5da6541e40&pid=-1 정렬 이벤트에서 미리 View 와 Rowhandle 을 잡아두고 정렬이 끝나면 이전 잡아둔 행으로 이동새로고침 될때 사용하면 되겠네… using DevExpress.XtraGrid; using...

read more

Join in the conversation

Leave a Comment

0 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

무료 온라인 전광판

전광판

텍스트를 입력하고 텍스트 효과 및 배경효과 를 변경해서 전체화면으로 표시할 수 있는 전광판 용도로 사용하실 수 있습니다. 각종 스포츠 및 공연 관람시 응원 용도로 사용이 가능합니다.

Carousel

여러개의 슬라이드를 추가하여 프레젠테이션 및 이미지 슬라이드 용도로 사용하실 수 있습니다. 브라우저가 포함된 IT 기기로 큰 모니터에 연결하여 매장 내 공지사항 및 메뉴소개를 이미지로 표시할 수 있습니다.

Pin It on Pinterest

Shares
Share This

Share This

Share this post with your friends!

Shares