PageRenderTime 42ms CodeModel.GetById 16ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CSWinFormDataGridView/EditingControlHosting/MainForm.cs

#
C# | 172 lines | 108 code | 24 blank | 40 comment | 16 complexity | eef4d3802796d343b50334499a7aac73 MD5 | raw file
  1/************************************* Module Header **************************************\
  2* Module Name:	EditingControlHosting
  3* Project:		CSWinFormDataGridView
  4* Copyright (c) Microsoft Corporation.
  5* 
  6* This sample demonstrates how to host a control in the current DataGridViewCell  for 
  7* editing.
  8* 
  9* This source is subject to the Microsoft Public License.
 10* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
 11* All other rights reserved.
 12* 
 13* History:
 14* * 6/05/2009 3:00 PM Zhi-Xin Ye Created
 15\******************************************************************************************/
 16
 17#region Using directives
 18using System;
 19using System.Collections.Generic;
 20using System.ComponentModel;
 21using System.Data;
 22using System.Drawing;
 23using System.Linq;
 24using System.Text;
 25using System.Windows.Forms;
 26#endregion
 27
 28
 29namespace CSWinFormDataGridView.EditingControlHosting
 30{
 31    public partial class MainForm : Form
 32    {
 33        public MainForm()
 34        {
 35            InitializeComponent();
 36        }
 37
 38        private MaskedTextBox maskedTextBoxForEditing;
 39        private bool IsKeyPressHandled = false;
 40
 41        private void MainForm_Load(object sender, EventArgs e)
 42        {
 43            this.maskedTextBoxForEditing = new MaskedTextBox();
 44
 45            // The "000-00-0000" mask allows only digits can be input 
 46            this.maskedTextBoxForEditing.Mask = "000-00-0000";
 47            // this.maskedTextBoxForEditing.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
 48
 49            // Hide the MaskedTextBox 
 50            this.maskedTextBoxForEditing.Visible = false;
 51
 52            // Add the MaskedTextBox to the DataGridView's control collection
 53            this.dataGridView1.Controls.Add(this.maskedTextBoxForEditing);
 54
 55            // Add a DataGridViewTextBoxColumn to the 
 56            DataGridViewTextBoxColumn tc = new DataGridViewTextBoxColumn();
 57            tc.HeaderText = "Mask Column";
 58            tc.Name = "MaskColumn";
 59            this.dataGridView1.Columns.Add(tc);
 60
 61            // Add some empty rows for testing purpose.
 62            for (int j = 0; j < 30; j++)
 63            {
 64                this.dataGridView1.Rows.Add();
 65            }
 66
 67            // Handle the CellBeginEdit event to show the MaskedTextBox on
 68            // the current editing cell
 69            this.dataGridView1.CellBeginEdit +=
 70                new DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit);
 71
 72            // Handle the CellEndEdit event to hide the MaskedTextBox when 
 73            // editing completes.
 74            this.dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
 75
 76            // Handle the Scroll event to adjust the location of the MaskedTextBox as it is showing
 77            // when scrolling the DataGridView
 78            this.dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);
 79
 80            // Handle the EditingControlShowing event to pass the focus to the 
 81            // MaskedTextBox when begin editing with keystrokes
 82            this.dataGridView1.EditingControlShowing += 
 83                new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
 84        }
 85
 86        void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
 87        {
 88            // If the current cell is on the "MaskColumn", we use the MaskedTextBox control
 89            // for editing instead of the default TextBox control; 
 90            if (e.ColumnIndex == this.dataGridView1.Columns["MaskColumn"].Index)
 91            {
 92                // Calculate the cell bounds of the current cell
 93                Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(
 94                    e.ColumnIndex, e.RowIndex, true);
 95                // Adjust the MaskedTextBox's size and location to fit the cell
 96                this.maskedTextBoxForEditing.Size = rect.Size;
 97                this.maskedTextBoxForEditing.Location = rect.Location;
 98
 99                // Set value for the MaskedTextBox 
100                if (this.dataGridView1.CurrentCell.Value != null)
101                {
102                    this.maskedTextBoxForEditing.Text = this.dataGridView1.CurrentCell.Value.ToString();
103                }
104
105                // Show the MaskedTextBox 
106                this.maskedTextBoxForEditing.Visible = true;
107            }
108        }
109
110        void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
111        {
112            // When finish editing on the "MaskColumn", we replace the cell value with
113            // the text typed in the MaskedTextBox, and hide the MaskedTextBox;
114            if (e.ColumnIndex == this.dataGridView1.Columns["MaskColumn"].Index)
115            {
116                this.dataGridView1.CurrentCell.Value = this.maskedTextBoxForEditing.Text;
117                this.maskedTextBoxForEditing.Text = "";
118                this.maskedTextBoxForEditing.Visible = false;
119            }
120        }
121
122        void dataGridView1_Scroll(object sender, ScrollEventArgs e)
123        {
124            if (this.dataGridView1.IsCurrentCellInEditMode == true)
125            {
126                // Adjust the location for the MaskedTextBox while scrolling
127                Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(
128                    this.dataGridView1.CurrentCell.ColumnIndex,
129                    this.dataGridView1.CurrentCell.RowIndex, true);
130
131                Console.WriteLine(rect.ToString());
132                Console.WriteLine(this.dataGridView1.CurrentCellAddress.ToString());
133                Console.WriteLine("");
134
135                if (rect.X <= 0 || rect.Y <= 0)
136                {
137                    this.maskedTextBoxForEditing.Visible = false;
138                }
139                else
140                {
141                    this.maskedTextBoxForEditing.Location = rect.Location;
142                }
143            }
144        }
145
146        void dataGridView1_EditingControlShowing(object sender, 
147            DataGridViewEditingControlShowingEventArgs e)
148        {
149            if (!this.IsKeyPressHandled
150                && this.dataGridView1.CurrentCell.ColumnIndex == 
151                this.dataGridView1.Columns["MaskColumn"].Index)
152            {
153                TextBox tb = e.Control as TextBox;
154                tb.KeyPress += new KeyPressEventHandler(tb_KeyPress);
155                this.IsKeyPressHandled = true;
156            }
157        }
158
159        void tb_KeyPress(object sender, KeyPressEventArgs e)
160        {
161            if (this.dataGridView1.CurrentCell.ColumnIndex == 
162                this.dataGridView1.Columns["MaskColumn"].Index)
163            {
164                // Prevent the key char to be input in the editing control
165                e.Handled = true;
166
167                // Set focus to the MaskedTextBox for editing.
168                this.maskedTextBoxForEditing.Focus();
169            }
170        }
171    }
172}