PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms 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. #region Using directives
  17. using System;
  18. using System.Collections.Generic;
  19. using System.ComponentModel;
  20. using System.Data;
  21. using System.Drawing;
  22. using System.Linq;
  23. using System.Text;
  24. using System.Windows.Forms;
  25. #endregion
  26. namespace CSWinFormDataGridView.EditingControlHosting
  27. {
  28. public partial class MainForm : Form
  29. {
  30. public MainForm()
  31. {
  32. InitializeComponent();
  33. }
  34. private MaskedTextBox maskedTextBoxForEditing;
  35. private bool IsKeyPressHandled = false;
  36. private void MainForm_Load(object sender, EventArgs e)
  37. {
  38. this.maskedTextBoxForEditing = new MaskedTextBox();
  39. // The "000-00-0000" mask allows only digits can be input
  40. this.maskedTextBoxForEditing.Mask = "000-00-0000";
  41. // this.maskedTextBoxForEditing.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
  42. // Hide the MaskedTextBox
  43. this.maskedTextBoxForEditing.Visible = false;
  44. // Add the MaskedTextBox to the DataGridView's control collection
  45. this.dataGridView1.Controls.Add(this.maskedTextBoxForEditing);
  46. // Add a DataGridViewTextBoxColumn to the
  47. DataGridViewTextBoxColumn tc = new DataGridViewTextBoxColumn();
  48. tc.HeaderText = "Mask Column";
  49. tc.Name = "MaskColumn";
  50. this.dataGridView1.Columns.Add(tc);
  51. // Add some empty rows for testing purpose.
  52. for (int j = 0; j < 30; j++)
  53. {
  54. this.dataGridView1.Rows.Add();
  55. }
  56. // Handle the CellBeginEdit event to show the MaskedTextBox on
  57. // the current editing cell
  58. this.dataGridView1.CellBeginEdit +=
  59. new DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit);
  60. // Handle the CellEndEdit event to hide the MaskedTextBox when
  61. // editing completes.
  62. this.dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
  63. // Handle the Scroll event to adjust the location of the MaskedTextBox as it is showing
  64. // when scrolling the DataGridView
  65. this.dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);
  66. // Handle the EditingControlShowing event to pass the focus to the
  67. // MaskedTextBox when begin editing with keystrokes
  68. this.dataGridView1.EditingControlShowing +=
  69. new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
  70. }
  71. void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
  72. {
  73. // If the current cell is on the "MaskColumn", we use the MaskedTextBox control
  74. // for editing instead of the default TextBox control;
  75. if (e.ColumnIndex == this.dataGridView1.Columns["MaskColumn"].Index)
  76. {
  77. // Calculate the cell bounds of the current cell
  78. Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(
  79. e.ColumnIndex, e.RowIndex, true);
  80. // Adjust the MaskedTextBox's size and location to fit the cell
  81. this.maskedTextBoxForEditing.Size = rect.Size;
  82. this.maskedTextBoxForEditing.Location = rect.Location;
  83. // Set value for the MaskedTextBox
  84. if (this.dataGridView1.CurrentCell.Value != null)
  85. {
  86. this.maskedTextBoxForEditing.Text = this.dataGridView1.CurrentCell.Value.ToString();
  87. }
  88. // Show the MaskedTextBox
  89. this.maskedTextBoxForEditing.Visible = true;
  90. }
  91. }
  92. void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  93. {
  94. // When finish editing on the "MaskColumn", we replace the cell value with
  95. // the text typed in the MaskedTextBox, and hide the MaskedTextBox;
  96. if (e.ColumnIndex == this.dataGridView1.Columns["MaskColumn"].Index)
  97. {
  98. this.dataGridView1.CurrentCell.Value = this.maskedTextBoxForEditing.Text;
  99. this.maskedTextBoxForEditing.Text = "";
  100. this.maskedTextBoxForEditing.Visible = false;
  101. }
  102. }
  103. void dataGridView1_Scroll(object sender, ScrollEventArgs e)
  104. {
  105. if (this.dataGridView1.IsCurrentCellInEditMode == true)
  106. {
  107. // Adjust the location for the MaskedTextBox while scrolling
  108. Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(
  109. this.dataGridView1.CurrentCell.ColumnIndex,
  110. this.dataGridView1.CurrentCell.RowIndex, true);
  111. Console.WriteLine(rect.ToString());
  112. Console.WriteLine(this.dataGridView1.CurrentCellAddress.ToString());
  113. Console.WriteLine("");
  114. if (rect.X <= 0 || rect.Y <= 0)
  115. {
  116. this.maskedTextBoxForEditing.Visible = false;
  117. }
  118. else
  119. {
  120. this.maskedTextBoxForEditing.Location = rect.Location;
  121. }
  122. }
  123. }
  124. void dataGridView1_EditingControlShowing(object sender,
  125. DataGridViewEditingControlShowingEventArgs e)
  126. {
  127. if (!this.IsKeyPressHandled
  128. && this.dataGridView1.CurrentCell.ColumnIndex ==
  129. this.dataGridView1.Columns["MaskColumn"].Index)
  130. {
  131. TextBox tb = e.Control as TextBox;
  132. tb.KeyPress += new KeyPressEventHandler(tb_KeyPress);
  133. this.IsKeyPressHandled = true;
  134. }
  135. }
  136. void tb_KeyPress(object sender, KeyPressEventArgs e)
  137. {
  138. if (this.dataGridView1.CurrentCell.ColumnIndex ==
  139. this.dataGridView1.Columns["MaskColumn"].Index)
  140. {
  141. // Prevent the key char to be input in the editing control
  142. e.Handled = true;
  143. // Set focus to the MaskedTextBox for editing.
  144. this.maskedTextBoxForEditing.Focus();
  145. }
  146. }
  147. }
  148. }