1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Pedro MartÃnez Juliá <pedromj@gmail.com>
32 namespace System.Windows.Forms {
34 public class DataGridViewTextBoxCell : DataGridViewCell {
36 private int maxInputLength = 32767;
37 private static DataGridViewTextBoxEditingControl editingControl;
39 static DataGridViewTextBoxCell () {
40 editingControl = new DataGridViewTextBoxEditingControl();
41 editingControl.Multiline = false;
42 editingControl.BorderStyle = BorderStyle.None;
45 public DataGridViewTextBoxCell () {
48 public override Type FormattedValueType {
49 get { return typeof(string); }
52 public virtual int MaxInputLength {
53 get { return maxInputLength; }
56 throw new ArgumentOutOfRangeException("MaxInputLength coudn't be less than 0.");
58 maxInputLength = value;
62 public override Type ValueType {
63 get { return typeof(string); }
66 public override object Clone () {
67 DataGridViewTextBoxCell result = (DataGridViewTextBoxCell) base.Clone();
68 result.maxInputLength = maxInputLength;
72 public override void DetachEditingControl () {
73 if (DataGridView == null) {
74 throw new InvalidOperationException("There is no associated DataGridView.");
76 if (DataGridView.Controls.Contains(editingControl)) {
77 DataGridView.Controls.Remove(editingControl);
79 Console.WriteLine("Detached: ({0}, {1});", RowIndex, ColumnIndex);
82 public override void InitializeEditingControl (int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) {
83 if (DataGridView == null) {
84 throw new InvalidOperationException("There is no associated DataGridView.");
86 if (!DataGridView.Controls.Contains(editingControl)) {
87 DataGridView.Controls.Add(editingControl);
89 editingControl.EditingControlDataGridView = DataGridView;
90 editingControl.MaxLength = maxInputLength;
91 if (initialFormattedValue == null || (string) initialFormattedValue == "") {
92 editingControl.Text = "";
95 editingControl.Text = (string) initialFormattedValue;
97 editingControl.ApplyCellStyleToEditingControl(dataGridViewCellStyle);
98 editingControl.PrepareEditingControlForEdit(true);
101 public override bool KeyEntersEditMode (KeyEventArgs e) {
102 throw new NotImplementedException();
105 public override void PositionEditingControl (bool setLocation, bool setSize, Rectangle cellBounds, Rectangle cellClip, DataGridViewCellStyle cellStyle, bool singleVerticalBorderAdded, bool singleHorizontalBorderAdded, bool isFirstDisplayedColumn, bool isFirstDisplayedRow) {
107 editingControl.Size = new Size(cellBounds.Width, cellBounds.Height + 2);
110 editingControl.Location = new Point(cellBounds.X, cellBounds.Y);
112 editingControl.Invalidate();
115 public override string ToString () {
116 return this.GetType().Name;
119 protected override Rectangle GetContentBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex) {
120 throw new NotImplementedException();
123 protected override Rectangle GetErrorIconBounds (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex) {
124 throw new NotImplementedException();
127 protected override Size GetPreferredSize (Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize) {
128 throw new NotImplementedException();
131 protected override void OnEnter (int rowIndex, bool throughMouseClick) {
134 protected override void OnLeave (int rowIndex, bool throughMouseClick) {
137 protected override void OnMouseClick (DataGridViewCellMouseEventArgs e) {
140 protected override void OnMouseEnter (int rowIndex) {
143 protected override void OnMouseLeave (int rowIndex) {
146 protected override void Paint (Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) {
149 Size size = DataGridViewCell.MeasureTextSize(graphics, (string) formattedValue, cellStyle.Font, TextFormatFlags.Default);
150 switch (cellStyle.Alignment) {
151 case DataGridViewContentAlignment.TopLeft:
154 //cell.SetContentBounds(cellBounds);
157 if ((cellState & DataGridViewElementStates.Selected) != 0) {
158 graphics.FillRectangle(new SolidBrush(cellStyle.SelectionBackColor), cellBounds);
159 graphics.DrawString((string) formattedValue, cellStyle.Font, new SolidBrush(cellStyle.SelectionForeColor), cellBounds, StringFormat.GenericDefault);
162 graphics.FillRectangle(new SolidBrush(cellStyle.BackColor), cellBounds);
163 graphics.DrawString((string) formattedValue, cellStyle.Font, new SolidBrush(cellStyle.ForeColor), cellBounds, StringFormat.GenericDefault);
165 PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
168 protected override void OnDataGridViewChanged () {
169 editingControl.EditingControlDataGridView = DataGridView;