PageRenderTime 23ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CSWinFormControls/MainForm.cs

#
C# | 189 lines | 104 code | 32 blank | 53 comment | 5 complexity | cd5fcd3eebf593b120ed8d8469c58db6 MD5 | raw file
  1. /************************************* Module Header **************************************\
  2. * Module Name: MainForm.cs
  3. * Project: CSWinFormControls
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * The Control Customization sample demonstrates how to customize the Windows Forms controls.
  7. *
  8. * In this sample, there're 4 examples:
  9. *
  10. * 1. Multiple Column ComboBox.
  11. * Demonstrates how to display multiple columns of data in the dropdown of a ComboBox.
  12. * 2. ListBox Items With Different ToolTips.
  13. * Demonstrates how to display different tooltips on each items of the ListBox.
  14. * 3. Numeric-only TextBox.
  15. * Demonstrates how to make a TextBox only accepts numbers.
  16. * 4. A Round Button.
  17. * Demonstrates how to create a Button with irregular shape.
  18. *
  19. * This source is subject to the Microsoft Public License.
  20. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  21. * All other rights reserved.
  22. *
  23. * History:
  24. * * 3/25/2009 3:00 PM Zhi-Xin Ye Created
  25. \******************************************************************************************/
  26. #region Using directives
  27. using System;
  28. using System.Collections.Generic;
  29. using System.ComponentModel;
  30. using System.Data;
  31. using System.Drawing;
  32. using System.Linq;
  33. using System.Text;
  34. using System.Windows.Forms;
  35. using System.Drawing.Drawing2D;
  36. #endregion
  37. namespace CSWinFormControls
  38. {
  39. public partial class MainForm : Form
  40. {
  41. public MainForm()
  42. {
  43. InitializeComponent();
  44. }
  45. private void frmCtrlCustomization_Load(object sender, EventArgs e)
  46. {
  47. #region Example 1 -- Multiple Column ComboBox
  48. // DataSource setup:
  49. //
  50. // Create a Table named Test and add 2 columns
  51. // ID: int
  52. // Name: string
  53. //
  54. DataTable dtTest = new DataTable();
  55. dtTest.Columns.Add("ID", typeof(int));
  56. dtTest.Columns.Add("Name", typeof(string));
  57. dtTest.Rows.Add(1, "John");
  58. dtTest.Rows.Add(2, "Amy");
  59. dtTest.Rows.Add(3, "Tony");
  60. dtTest.Rows.Add(4, "Bruce");
  61. dtTest.Rows.Add(5, "Allen");
  62. // Bind the ComboBox to the DataTable
  63. this.comboBox1.DataSource = dtTest;
  64. this.comboBox1.DisplayMember = "Name";
  65. this.comboBox1.ValueMember = "ID";
  66. // Enable the owner draw on the ComboBox.
  67. this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
  68. // Handle the DrawItem event to draw the items.
  69. this.comboBox1.DrawItem += delegate(object cmb, DrawItemEventArgs args)
  70. {
  71. // Draw the default background
  72. args.DrawBackground();
  73. // The ComboBox is bound to a DataTable,
  74. // so the items are DataRowView objects.
  75. DataRowView drv = (DataRowView)this.comboBox1.Items[args.Index];
  76. // Retrieve the value of each column.
  77. string id = drv["id"].ToString();
  78. string name = drv["name"].ToString();
  79. // Get the bounds for the first column
  80. Rectangle r1 = args.Bounds;
  81. r1.Width /= 2;
  82. // Draw the text on the first column
  83. using (SolidBrush sb = new SolidBrush(args.ForeColor))
  84. {
  85. args.Graphics.DrawString(id, args.Font, sb, r1);
  86. }
  87. // Draw a line to isolate the columns
  88. using (Pen p = new Pen(Color.Black))
  89. {
  90. args.Graphics.DrawLine(p, r1.Right, 0, r1.Right, r1.Bottom);
  91. }
  92. // Get the bounds for the second column
  93. Rectangle r2 = args.Bounds;
  94. r2.X = args.Bounds.Width/2;
  95. r2.Width /= 2;
  96. // Draw the text on the second column
  97. using (SolidBrush sb = new SolidBrush(args.ForeColor))
  98. {
  99. args.Graphics.DrawString(name, args.Font, sb, r2);
  100. }
  101. };
  102. #endregion
  103. #region Example 2 -- ListBox Items With Different ToolTips
  104. // Setup the ListBox items
  105. this.listBox1.Items.Add("Item1");
  106. this.listBox1.Items.Add("Item2");
  107. this.listBox1.Items.Add("Item3");
  108. this.listBox1.Items.Add("Item4");
  109. this.listBox1.Items.Add("Item5");
  110. this.listBox1.MouseMove += delegate(object lst, MouseEventArgs args)
  111. {
  112. // Retrieve the item index at where the mouse hovers
  113. int hoverIndex = this.listBox1.IndexFromPoint(args.Location);
  114. // If the mouse is over the items, display a tooltip
  115. if (hoverIndex >= 0 && hoverIndex < listBox1.Items.Count)
  116. {
  117. this.toolTip1.SetToolTip(listBox1, listBox1.Items[hoverIndex].ToString());
  118. }
  119. };
  120. #endregion
  121. #region Example 3 -- Numeric-only TextBox
  122. // Handle the TextBox.KeyPress event to filter the input characters.
  123. this.textBox1.KeyPress += delegate(object tb, KeyPressEventArgs args)
  124. {
  125. if (!(char.IsNumber(args.KeyChar) || args.KeyChar == '\b'))
  126. {
  127. // If the input character is not number or Backspace key
  128. // Then set the Handled property to true to indicate that
  129. // the KeyPress event is handled, so that the TextBox just
  130. // bypass the input character.
  131. args.Handled = true;
  132. }
  133. };
  134. #endregion
  135. #region Example 4 -- A Round Button
  136. this.roundButton1.Click += delegate(object btn, EventArgs args)
  137. {
  138. MessageBox.Show("Clicked!");
  139. };
  140. #endregion
  141. }
  142. }
  143. #region RoundButton Class
  144. public class RoundButton : Button
  145. {
  146. protected override void OnPaint(PaintEventArgs pevent)
  147. {
  148. // Change the region for the button so that when clicks outside the ellipse bounds,
  149. // the Click event won't fire.
  150. GraphicsPath path = new GraphicsPath();
  151. path.AddEllipse(0, 0, ClientSize.Width, ClientSize.Height);
  152. this.Region = new Region(path);
  153. base.OnPaint(pevent);
  154. }
  155. }
  156. #endregion
  157. }