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>
30 using System
.ComponentModel
;
33 namespace System
.Windows
.Forms
{
35 public class DataGridViewTextBoxCell
: DataGridViewCell
{
37 private int maxInputLength
= 32767;
38 private static DataGridViewTextBoxEditingControl editingControl
;
40 static DataGridViewTextBoxCell ()
42 editingControl
= new DataGridViewTextBoxEditingControl();
43 editingControl
.Multiline
= false;
44 editingControl
.BorderStyle
= BorderStyle
.None
;
47 public DataGridViewTextBoxCell ()
51 public override Type FormattedValueType
{
52 get { return typeof(string); }
55 [DefaultValue (32767)]
56 public virtual int MaxInputLength
{
57 get { return maxInputLength; }
60 throw new ArgumentOutOfRangeException("MaxInputLength coudn't be less than 0.");
62 maxInputLength
= value;
66 public override Type ValueType
{
67 get { return typeof(string); }
70 public override object Clone ()
72 DataGridViewTextBoxCell result
= (DataGridViewTextBoxCell
) base.Clone();
73 result
.maxInputLength
= maxInputLength
;
77 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
78 public override void DetachEditingControl ()
80 if (DataGridView
== null) {
81 throw new InvalidOperationException("There is no associated DataGridView.");
83 if (DataGridView
.Controls
.Contains(editingControl
)) {
84 DataGridView
.Controls
.Remove(editingControl
);
86 Console
.WriteLine("Detached: ({0}, {1});", RowIndex
, ColumnIndex
);
89 public override void InitializeEditingControl (int rowIndex
, object initialFormattedValue
, DataGridViewCellStyle dataGridViewCellStyle
)
91 if (DataGridView
== null) {
92 throw new InvalidOperationException("There is no associated DataGridView.");
94 if (!DataGridView
.Controls
.Contains(editingControl
)) {
95 DataGridView
.Controls
.Add(editingControl
);
97 editingControl
.EditingControlDataGridView
= DataGridView
;
98 editingControl
.MaxLength
= maxInputLength
;
99 if (initialFormattedValue
== null || (string) initialFormattedValue
== "") {
100 editingControl
.Text
= "";
103 editingControl
.Text
= (string) initialFormattedValue
;
105 editingControl
.ApplyCellStyleToEditingControl(dataGridViewCellStyle
);
106 editingControl
.PrepareEditingControlForEdit(true);
109 public override bool KeyEntersEditMode (KeyEventArgs e
)
111 throw new NotImplementedException();
114 public override void PositionEditingControl (bool setLocation
, bool setSize
, Rectangle cellBounds
, Rectangle cellClip
, DataGridViewCellStyle cellStyle
, bool singleVerticalBorderAdded
, bool singleHorizontalBorderAdded
, bool isFirstDisplayedColumn
, bool isFirstDisplayedRow
)
117 editingControl
.Size
= new Size(cellBounds
.Width
, cellBounds
.Height
+ 2);
120 editingControl
.Location
= new Point(cellBounds
.X
, cellBounds
.Y
);
122 editingControl
.Invalidate();
125 public override string ToString ()
127 return this.GetType().Name
;
130 protected override Rectangle
GetContentBounds (Graphics graphics
, DataGridViewCellStyle cellStyle
, int rowIndex
)
132 throw new NotImplementedException();
135 protected override Rectangle
GetErrorIconBounds (Graphics graphics
, DataGridViewCellStyle cellStyle
, int rowIndex
)
137 throw new NotImplementedException();
140 protected override Size
GetPreferredSize (Graphics graphics
, DataGridViewCellStyle cellStyle
, int rowIndex
, Size constraintSize
)
142 throw new NotImplementedException();
145 protected override void OnEnter (int rowIndex
, bool throughMouseClick
)
149 protected override void OnLeave (int rowIndex
, bool throughMouseClick
)
153 protected override void OnMouseClick (DataGridViewCellMouseEventArgs e
)
157 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
)
161 Size size = DataGridViewCell.MeasureTextSize(graphics, (string) formattedValue, cellStyle.Font, TextFormatFlags.Default);
162 switch (cellStyle.Alignment) {
163 case DataGridViewContentAlignment.TopLeft:
166 //cell.SetContentBounds(cellBounds);
169 if ((cellState
& DataGridViewElementStates
.Selected
) != 0) {
170 graphics
.FillRectangle(new SolidBrush(cellStyle
.SelectionBackColor
), cellBounds
);
171 graphics
.DrawString((string) formattedValue
, cellStyle
.Font
, new SolidBrush(cellStyle
.SelectionForeColor
), cellBounds
, StringFormat
.GenericDefault
);
174 graphics
.FillRectangle(new SolidBrush(cellStyle
.BackColor
), cellBounds
);
175 graphics
.DrawString((string) formattedValue
, cellStyle
.Font
, new SolidBrush(cellStyle
.ForeColor
), cellBounds
, StringFormat
.GenericDefault
);
177 PaintBorder(graphics
, clipBounds
, cellBounds
, cellStyle
, advancedBorderStyle
);