728x90
C#에서 Panel 컨트롤은 다양한 UI 디자인 패턴을 구현하는 데 유용합니다. 단순히 폼을 상속하는 것뿐 아니라, 여러 방식으로 활용할 수 있습니다. 아래에는 다양한 활용 방법과 함께 간단한 예제를 소개합니다.
2탄입니다. 조금 심화된 기능들이 있으니 주의해주시면 좋습니다.
4. 사용자 정의 컨트롤 만들기
Panel을 사용해 사용자 정의 컨트롤을 만들고, 다양한 컨트롤을 하나로 묶어 재사용 가능한 컴포넌트를 생성할 수 있습니다.
예제
다음 예제는 Panel을 사용해 사용자 정의 컨트롤로 간단한 카드 형식의 UI를 구성합니다.
using System;
using System.Drawing;
using System.Windows.Forms;
public class CardControl : Panel
{
private Label titleLabel;
private Label contentLabel;
public CardControl(string title, string content)
{
this.BorderStyle = BorderStyle.FixedSingle;
this.Padding = new Padding(10);
this.Width = 200;
this.Height = 100;
titleLabel = new Label() { Text = title, Font = new Font("Arial", 12, FontStyle.Bold), Dock = DockStyle.Top };
contentLabel = new Label() { Text = content, Dock = DockStyle.Fill, TextAlign = ContentAlignment.MiddleLeft };
this.Controls.Add(contentLabel);
this.Controls.Add(titleLabel);
}
}
public class MainForm : Form
{
public MainForm()
{
CardControl card = new CardControl("Title", "This is the content of the card.");
card.Location = new Point(10, 10);
this.Controls.Add(card);
this.Text = "Card Example";
}
}
사용자 정의컨트롤로 미리 내가 원하는 구성대로 만들어 놓을수 있습니다.
이런 컴포넌트들을 많이 만들어 놓으면 개발속도가 매우 올라갑니다.
하지만 사용에 주의하여 주시길 바랍니다.
5. 드래그 앤 드롭 구현
Panel을 활용해 드래그 앤 드롭 기능을 구현하여 파일 업로드 영역을 만들거나, 다양한 컨트롤의 위치를 사용자가 직접 변경할 수 있도록 할 수 있습니다.
예제
아래는 Panel을 통해 사용자가 버튼을 드래그할 수 있도록 구현한 간단한 예제입니다.
using System;
using System.Drawing;
using System.Windows.Forms;
public class MainForm : Form
{
private Panel dragPanel;
private Button dragButton;
private Point offset;
public MainForm()
{
dragPanel = new Panel() { Dock = DockStyle.Fill };
this.Controls.Add(dragPanel);
dragButton = new Button() { Text = "Drag me", Location = new Point(50, 50) };
dragButton.MouseDown += DragButton_MouseDown;
dragButton.MouseMove += DragButton_MouseMove;
dragPanel.Controls.Add(dragButton);
}
private void DragButton_MouseDown(object sender, MouseEventArgs e)
{
offset = new Point(e.X, e.Y);
}
private void DragButton_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
dragButton.Left = e.X + dragButton.Left - offset.X;
dragButton.Top = e.Y + dragButton.Top - offset.Y;
}
}
}
마우스로 클릭후 드래그 앤 드랍으로 이동이가능합니다.
이방법을 활용하면 여러가지로 활용이 가능합니다.
728x90
'Function > C#' 카테고리의 다른 글
[C# Git error] bin\roslyn\csc.exe 경로의 일부를 찾을 수 없습니다. (0) | 2024.10.30 |
---|---|
C# Panel 컨트롤 활용 2편 다양한 UI 디자인 패턴 구현하기 (0) | 2024.10.18 |
C# Panel 컨트롤에 폼 상속시키기: 다중 화면 전환 구현하기 (0) | 2024.10.17 |
C# Dialog문 사용법 정리 (0) | 2024.08.29 |
C# Thread 사용방법 정리 (0) | 2024.08.28 |