본문으로 바로가기

Custom ToolStrip, MenuStrip Demo

category Development/C# 2009. 8. 31. 19:59
ToolStrip과 MenuStrip에 DateTimePicker, CheckBox를 넣는 예제임. 
우선 툴바나 메뉴의 아이템으로 넣고 싶은 컨트롤을 만든다음 ToolStripControlHost로 감싸주면 간단히 구현할수 있다. 
// ToolStrip 에 컨트롤 넣기.
ToolStripControlHost dateTimePickerHost = new ToolStripControlHost(new DateTimePicker());
ToolStripControlHost checkboxHost;

dateTimePickerHost.Font = new Font("Arial", 7.0F, FontStyle.Italic);
dateTimePickerHost.Width = 100;
dateTimePickerHost.DisplayStyle = ToolStripItemDisplayStyle.Text;
dateTimePickerHost.Text = "12/23/2005";
((DateTimePicker)dateTimePickerHost.Control).Format = DateTimePickerFormat.Short;

CheckBox checkbox = new CheckBox();
checkbox.Text = "This is Checkbox";
checkboxHost = new ToolStripControlHost(checkbox);
          
toolStrip1.Items.Add(dateTimePickerHost);
toolStrip1.Items.Add(checkboxHost);
checkbox.CheckedChanged += new EventHandler(checkbox_CheckedChanged);
// MenuStrip에 컨트롤 넣기. ToolStrip과 같은 방법으로
dateTimePickerHost2.Font = new Font("Arial", 7.0F, FontStyle.Italic);

dateTimePickerHost2.Width = 100;
dateTimePickerHost2.DisplayStyle = ToolStripItemDisplayStyle.Text;
dateTimePickerHost2.Text = "12/23/2005";
((DateTimePicker)dateTimePickerHost2.Control).Format = DateTimePickerFormat.Short;

CheckBox checkbox2 = new CheckBox();
checkbox2.Text = "This is Checkbox";
checkbox2.CheckedChanged += new EventHandler(checkbox_CheckedChanged);
checkboxHost2 = new ToolStripControlHost(checkbox2);

ToolStripMenuItem menuItem = new ToolStripMenuItem("MenuStrip");
menuItem.DropDownItems.Add(dateTimePickerHost2);
menuItem.DropDownItems.Add(checkboxHost2);
menuStrip1.Items.Add(menuItem);
// 이벤트 처리는 일반적인 방법으로...
private void checkbox_CheckedChanged(object sender, EventArgs e)
{
       MessageBox.Show("Checked Changed: " + ((CheckBox)sender).CheckState);
}

'Development > C#' 카테고리의 다른 글

레퍼런스(Reference) 변수를 ref 인자로 넘겨주는 이유  (0) 2009.09.01
동적으로 메서드 실행  (0) 2009.08.31
네트워크 드라이브 연결하기  (0) 2009.08.27
UTC 시차 극복 방법  (0) 2009.08.27
Amaizing! C# 4.0  (0) 2009.03.12