PageRenderTime 3ms CodeModel.GetById 30ms app.highlight 15ms RepoModel.GetById 2ms 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
 27#region Using directives
 28using System;
 29using System.Collections.Generic;
 30using System.ComponentModel;
 31using System.Data;
 32using System.Drawing;
 33using System.Linq;
 34using System.Text;
 35using System.Windows.Forms;
 36using System.Drawing.Drawing2D;
 37#endregion
 38
 39
 40namespace CSWinFormControls
 41{
 42    public partial class MainForm : Form
 43    {
 44        public MainForm()
 45        {
 46            InitializeComponent();
 47        }
 48
 49        private void frmCtrlCustomization_Load(object sender, EventArgs e)
 50        {
 51            #region  Example 1 -- Multiple Column ComboBox
 52
 53            //  DataSource setup:
 54            //  
 55            //  Create a Table named Test and add 2 columns
 56            //   ID:     int
 57            //   Name:   string
 58            //
 59            DataTable dtTest = new DataTable();
 60            dtTest.Columns.Add("ID", typeof(int));
 61            dtTest.Columns.Add("Name", typeof(string));
 62
 63            dtTest.Rows.Add(1, "John");
 64            dtTest.Rows.Add(2, "Amy");
 65            dtTest.Rows.Add(3, "Tony");
 66            dtTest.Rows.Add(4, "Bruce");
 67            dtTest.Rows.Add(5, "Allen");
 68
 69            // Bind the ComboBox to the DataTable
 70            this.comboBox1.DataSource = dtTest;
 71            this.comboBox1.DisplayMember = "Name";
 72            this.comboBox1.ValueMember = "ID";
 73
 74            // Enable the owner draw on the ComboBox.
 75            this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
 76            // Handle the DrawItem event to draw the items.
 77            this.comboBox1.DrawItem += delegate(object cmb, DrawItemEventArgs args)
 78            {
 79                // Draw the default background
 80                args.DrawBackground();
 81
 82
 83                // The ComboBox is bound to a DataTable,
 84                // so the items are DataRowView objects.
 85                DataRowView drv = (DataRowView)this.comboBox1.Items[args.Index];
 86
 87                // Retrieve the value of each column.
 88                string id = drv["id"].ToString();
 89                string name = drv["name"].ToString();
 90
 91                // Get the bounds for the first column
 92                Rectangle r1 = args.Bounds;
 93                r1.Width /= 2;
 94
 95                // Draw the text on the first column
 96                using (SolidBrush sb = new SolidBrush(args.ForeColor))
 97                {
 98                    args.Graphics.DrawString(id, args.Font, sb, r1);
 99                }
100
101                // Draw a line to isolate the columns 
102                using (Pen p = new Pen(Color.Black))
103                {
104                    args.Graphics.DrawLine(p, r1.Right, 0, r1.Right, r1.Bottom);
105                }
106
107                // Get the bounds for the second column
108                Rectangle r2 = args.Bounds;
109                r2.X = args.Bounds.Width/2;
110                r2.Width /= 2;
111
112                // Draw the text on the second column
113                using (SolidBrush sb = new SolidBrush(args.ForeColor))
114                {
115                    args.Graphics.DrawString(name, args.Font, sb, r2);
116                }
117            };
118
119            #endregion
120                
121            #region Example 2 -- ListBox Items With Different ToolTips
122
123            // Setup the ListBox items
124            this.listBox1.Items.Add("Item1");
125            this.listBox1.Items.Add("Item2");
126            this.listBox1.Items.Add("Item3");
127            this.listBox1.Items.Add("Item4");
128            this.listBox1.Items.Add("Item5");
129
130            this.listBox1.MouseMove += delegate(object lst, MouseEventArgs args)
131            {
132                // Retrieve the item index at where the mouse hovers
133                int hoverIndex = this.listBox1.IndexFromPoint(args.Location);
134
135                // If the mouse is over the items, display a tooltip
136                if (hoverIndex >= 0 && hoverIndex < listBox1.Items.Count)
137                {
138                    this.toolTip1.SetToolTip(listBox1, listBox1.Items[hoverIndex].ToString());
139                }
140            };
141
142            #endregion
143
144            #region Example 3 -- Numeric-only TextBox
145
146            // Handle the TextBox.KeyPress event to filter the input characters.
147            this.textBox1.KeyPress += delegate(object tb, KeyPressEventArgs args)
148            {
149                if (!(char.IsNumber(args.KeyChar) || args.KeyChar == '\b'))
150                {
151                    // If the input character is not number or Backspace key
152                    // Then set the Handled property to true to indicate that 
153                    // the KeyPress event is handled, so that the TextBox just 
154                    // bypass the input character.
155                    args.Handled = true;
156                }
157            };
158
159
160            #endregion
161
162            #region Example 4 -- A Round Button
163
164            this.roundButton1.Click += delegate(object btn, EventArgs args)
165            {
166                MessageBox.Show("Clicked!");
167            };
168            #endregion
169        }
170    }
171
172    #region RoundButton Class
173
174    public class RoundButton : Button
175    {
176        protected override void OnPaint(PaintEventArgs pevent)
177        {
178            // Change the region for the button so that when clicks outside the ellipse bounds,
179            // the Click event won't fire.
180            GraphicsPath path = new GraphicsPath();
181            path.AddEllipse(0, 0, ClientSize.Width, ClientSize.Height);
182            this.Region = new Region(path);
183
184            base.OnPaint(pevent);
185        }
186    }
187
188    #endregion
189}