Skip to content

Commit 85549d7

Browse files
committed
2 new KB articles
1 parent 1a19449 commit 85549d7

11 files changed

+293
-3
lines changed

controls/gridview/editors/editing-lifecycle.md

+2
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ By default, **RadGridView** starts [editing]({%slug winforms/gridview/editors/ed
3838

3939
* [Using custom editors]({%slug winforms/gridview/editors/using-custom-editors%})
4040

41+
* [How to AutoSize GridView's Row While Editing]({%slug row-autosizing-while-editing%})
42+

controls/gridview/rows/resizing-rows.md

+4
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,7 @@ Me.RadGridView1.MasterView.SummaryRows(0).Height = 50
143143
{{endregion}}
144144

145145
![gridview-rows-adding-and-inserting-rows 002](images/row-resizing008.png)
146+
147+
# See Also
148+
149+
* [How to AutoSize GridView's Row While Editing]({%slug row-autosizing-while-editing%})

controls/propertygrid/editors/customizing-editor-behavior.md

+1
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,4 @@ End Sub
8686

8787
* [Custom Editors]({%slug winforms/propertygrid/editors/using-custom-editor%})
8888
* [Validation]({%slug winforms/propertygrid/editors/validation%})
89+
* [How to allow end-users to add custom items to PropertyGridDropDownListEditor]({%slug allow-end-users-to-add-items-to-propertygriddropdownlisteditor%})

controls/propertygrid/editors/overview.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,5 @@ There are a number of build-in ediotrs which are used for editing different data
5656

5757
* [Events]({%slug winforms/propertygrid/editors/events%})
5858
* [Validation]({%slug winforms/propertygrid/editors/validation%})
59-
* [Custom Editors]({%slug winforms/propertygrid/editors/using-custom-editor%})
59+
* [Custom Editors]({%slug winforms/propertygrid/editors/using-custom-editor%})
60+
* [How to allow end-users to add custom items to PropertyGridDropDownListEditor]({%slug allow-end-users-to-add-items-to-propertygriddropdownlisteditor%})

controls/propertygrid/editors/using-custom-editor.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,5 @@ End Sub
154154
# See Also
155155

156156
* [Customizing Editor Behavior]({%slug winforms/propertygrid/editors/using-custom-editor%})
157-
* [Validation]({%slug winforms/propertygrid/editors/customizing-editor-behavior%})
157+
* [Validation]({%slug winforms/propertygrid/editors/customizing-editor-behavior%})
158+
* [How to allow end-users to add custom items to PropertyGridDropDownListEditor]({%slug allow-end-users-to-add-items-to-propertygriddropdownlisteditor%})
Loading
Loading
Loading

knowledge-base/measure-text.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
title: How to Measure Text
3+
description: Learn how to measure text in WinForms.
4+
type: how-to
5+
page_title: How to Measure Text
6+
slug: measure-text
7+
position: 5
8+
tags: text, measure, font
9+
ticketid: 1521435
10+
res_type: kb
11+
---
12+
13+
14+
## Environment
15+
|Product Version|Product|Author|
16+
|----|----|----|
17+
|2021.2.511|Telerik UI for WinForms|[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
18+
19+
## Description
20+
21+
There are different approaches for measuring some text considering the font family and font size. That is why it is important to know first how the [text is being rendered]({%slug winforms/telerik-presentation-framework/text-rendering%}) in order to measure it correctly.
22+
23+
## Solution
24+
25+
By default, Telerik Presentation Framework uses **GDI+** to measure and render the text. You can easily switch to **GDI** instead by setting the **UseCompatibleTextRendering** property to **false** for the respective control:
26+
27+
* [Draw text with GDI: TextRenderer.DrawText](https://docs.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-draw-text-with-gdi?view=netframeworkdesktop-4.8)
28+
29+
* [Draw text with GDI+: Graphics.DrawString](https://docs.microsoft.com/en-us/windows/win32/gdiplus/-gdiplus-drawing-text-use)
30+
31+
That is why when using **GDI** for rendering the text, it is suitable to use the TextRdenderer.[MeasureText](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.textrenderer.measuretext?redirectedfrom=MSDN&view=net-5.0#overloads) method for measuring it. And for **GDI+**, feel free to use the Graphics.[MeasureString](https://docs.microsoft.com/en-us/dotnet/api/system.drawing.graphics.measurestring?view=net-5.0) method. Both methods offer different overloads allowing you to measure the text in a precise manner according to different criteria that you may have, e.g. format flags, font, available width, etc.
32+
33+
# See Also
34+
35+
* [Text Rendering]({%slug winforms/telerik-presentation-framework/text-rendering%})
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
---
2+
title: How to AutoSize GridView's Row While Editing
3+
description: Learn how to autosize RadGridView's row while editing a multiline editor.
4+
type: how-to
5+
page_title: How to AutoSize GridView's Row While Editing
6+
slug: row-autosizing-while-editing
7+
position: 5
8+
tags: grid, autosize, edit, multiline
9+
ticketid: 1521435
10+
res_type: kb
11+
---
12+
13+
14+
## Environment
15+
|Product Version|Product|Author|
16+
|----|----|----|
17+
|2021.2.511|RadGridView|[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
18+
19+
## Description
20+
21+
The RadGridView.**AutoSizeRows** property controls whether row's height in a **RadGridView** will expand for multiline cell text if the column is wrapped. However, note that when a cell is in edit mode and you are typing in the editor, this value is not committed to the cell yet. That is why the row's height is not updated until you commit the editor's value:
22+
23+
![row-autosizing-while-editing 001](images/row-autosizing-while-editing001.gif)
24+
25+
## Solution
26+
27+
This solution demonstrates how to disable the rows auto sizing when the editor is activated and the row's height is adjusted by the **MinHeight** property in order to ensure that the whole multiline text inside the editor is visible:
28+
29+
![row-autosizing-while-editing 002](images/row-autosizing-while-editing002.gif)
30+
31+
32+
````C#
33+
34+
public RadForm1()
35+
{
36+
InitializeComponent();
37+
38+
GridViewTextBoxColumn textColumn = new GridViewTextBoxColumn("Description");
39+
this.radGridView1.Columns.Add(textColumn);
40+
textColumn.WrapText = true;
41+
this.radGridView1.AutoSizeRows = true;
42+
this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
43+
44+
this.radGridView1.BeginUpdate();
45+
for (int i = 0; i < 10; i++)
46+
{
47+
this.radGridView1.Rows.Add(i + "0.Data" + Environment.NewLine + i + "1.Data");
48+
}
49+
this.radGridView1.EndUpdate();
50+
51+
this.radGridView1.CellEditorInitialized += radGridView1_CellEditorInitialized;
52+
this.radGridView1.CellEndEdit += radGridView1_CellEndEdit;
53+
}
54+
55+
private void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
56+
{
57+
this.radGridView1.AutoSizeRows = true;
58+
}
59+
60+
private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
61+
{
62+
this.radGridView1.AutoSizeRows = false;
63+
RadTextBoxEditor tbEditor = e.ActiveEditor as RadTextBoxEditor;
64+
if (tbEditor != null)
65+
{
66+
tbEditor.Multiline = true;
67+
tbEditor.AcceptsReturn = true;
68+
RadTextBoxEditorElement el = tbEditor.EditorElement as RadTextBoxEditorElement;
69+
if (el != null)
70+
{
71+
if (textHeight == -1)
72+
{
73+
using (Graphics g = el.TextBoxItem.TextBoxControl.CreateGraphics())
74+
{
75+
textHeight = TextRenderer.MeasureText(g, el.TextBoxItem.HostedControl.Text.Substring(0,el.Text.IndexOf(Environment.NewLine)), el.TextBoxItem.HostedControl.Font).Height;
76+
}
77+
}
78+
el.TextBoxItem.TextBoxControl.TextChanged -= TextBoxControl_TextChanged;
79+
el.TextBoxItem.TextBoxControl.TextChanged += TextBoxControl_TextChanged;
80+
81+
this.radGridView1.CurrentRow.MinHeight = this.radGridView1.CurrentRow.Height = ( el.TextBoxItem.TextBoxControl.Lines.Length+1)* textHeight;
82+
}
83+
}
84+
}
85+
86+
int textHeight = -1;
87+
88+
private void TextBoxControl_TextChanged(object sender, EventArgs e)
89+
{
90+
HostedTextBoxBase tb = sender as HostedTextBoxBase;
91+
if (tb != null)
92+
{
93+
this.radGridView1.CurrentRow.MinHeight = this.radGridView1.CurrentRow.Height=( tb.Lines.Length+1)* textHeight;
94+
}
95+
}
96+
97+
98+
````
99+
````VB.NET
100+
101+
Public Sub New()
102+
InitializeComponent()
103+
Dim textColumn As GridViewTextBoxColumn = New GridViewTextBoxColumn("Description")
104+
Me.RadGridView1.Columns.Add(textColumn)
105+
textColumn.WrapText = True
106+
Me.RadGridView1.AutoSizeRows = True
107+
Me.RadGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
108+
Me.RadGridView1.BeginUpdate()
109+
110+
For i As Integer = 0 To 10 - 1
111+
Me.RadGridView1.Rows.Add(i & "0.Data" & Environment.NewLine & i & "1.Data")
112+
Next
113+
114+
Me.RadGridView1.EndUpdate()
115+
AddHandler Me.RadGridView1.CellEditorInitialized, AddressOf radGridView1_CellEditorInitialized
116+
AddHandler Me.RadGridView1.CellEndEdit, AddressOf radGridView1_CellEndEdit
117+
End Sub
118+
119+
Private Sub radGridView1_CellEndEdit(ByVal sender As Object, ByVal e As GridViewCellEventArgs)
120+
Me.RadGridView1.AutoSizeRows = True
121+
End Sub
122+
123+
Private Sub radGridView1_CellEditorInitialized(ByVal sender As Object, ByVal e As GridViewCellEventArgs)
124+
Me.RadGridView1.AutoSizeRows = False
125+
Dim tbEditor As RadTextBoxEditor = TryCast(e.ActiveEditor, RadTextBoxEditor)
126+
127+
If tbEditor IsNot Nothing Then
128+
tbEditor.Multiline = True
129+
tbEditor.AcceptsReturn = True
130+
Dim el As RadTextBoxEditorElement = TryCast(tbEditor.EditorElement, RadTextBoxEditorElement)
131+
132+
If el IsNot Nothing Then
133+
134+
If textHeight = -1 Then
135+
136+
Using g As Graphics = el.TextBoxItem.TextBoxControl.CreateGraphics()
137+
textHeight = TextRenderer.MeasureText(g, el.TextBoxItem.HostedControl.Text.Substring(0, _
138+
el.Text.IndexOf(Environment.NewLine)), el.TextBoxItem.HostedControl.Font).Height
139+
End Using
140+
End If
141+
142+
RemoveHandler el.TextBoxItem.TextBoxControl.TextChanged, AddressOf TextBoxControl_TextChanged
143+
AddHandler el.TextBoxItem.TextBoxControl.TextChanged, AddressOf TextBoxControl_TextChanged
144+
145+
Me.RadGridView1.CurrentRow.MinHeight = (el.TextBoxItem.TextBoxControl.Lines.Length + 1) * textHeight
146+
Me.RadGridView1.CurrentRow.Height = (el.TextBoxItem.TextBoxControl.Lines.Length + 1) * textHeight
147+
End If
148+
End If
149+
End Sub
150+
151+
Private textHeight As Integer = -1
152+
153+
Private Sub TextBoxControl_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
154+
Dim tb As HostedTextBoxBase = TryCast(sender, HostedTextBoxBase)
155+
156+
If tb IsNot Nothing Then
157+
Me.RadGridView1.CurrentRow.MinHeight = (tb.Lines.Length + 1) * textHeight
158+
Me.RadGridView1.CurrentRow.Height = (tb.Lines.Length + 1) * textHeight
159+
End If
160+
End Sub
161+
162+
````
163+
164+
However, if there is not any specific validation logic and it is not a problem to commit the editor's value immediately after typing, it is possible to use a very simple solution by handling the **ValueChanging** event that RadGridView offers and updating the cell's value accordingly. Thus, you can keep the rows autositing enabled:
165+
166+
![row-autosizing-while-editing 003](images/row-autosizing-while-editing003.gif)
167+
168+
````C#
169+
public RadForm1()
170+
{
171+
InitializeComponent();
172+
173+
GridViewTextBoxColumn textColumn = new GridViewTextBoxColumn("Description");
174+
this.radGridView1.Columns.Add(textColumn);
175+
textColumn.WrapText = true;
176+
this.radGridView1.AutoSizeRows = true;
177+
this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
178+
179+
this.radGridView1.BeginUpdate();
180+
for (int i = 0; i < 10; i++)
181+
{
182+
this.radGridView1.Rows.Add(i + "0.Data" + Environment.NewLine + i + "1.Data");
183+
}
184+
this.radGridView1.EndUpdate();
185+
186+
this.radGridView1.CellEditorInitialized += radGridView1_CellEditorInitialized;
187+
188+
this.radGridView1.ValueChanging += radGridView1_ValueChanging;
189+
}
190+
191+
private void radGridView1_ValueChanging(object sender, ValueChangingEventArgs e)
192+
{
193+
this.radGridView1.CurrentCell.Value = e.NewValue;
194+
}
195+
196+
private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
197+
{
198+
RadTextBoxEditor tbEditor = e.ActiveEditor as RadTextBoxEditor;
199+
if (tbEditor != null)
200+
{
201+
tbEditor.Multiline = true;
202+
tbEditor.AcceptsReturn = true;
203+
}
204+
}
205+
206+
````
207+
````VB.NET
208+
Public Sub New()
209+
InitializeComponent()
210+
Dim textColumn As GridViewTextBoxColumn = New GridViewTextBoxColumn("Description")
211+
Me.radGridView1.Columns.Add(textColumn)
212+
textColumn.WrapText = True
213+
Me.radGridView1.AutoSizeRows = True
214+
Me.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
215+
Me.radGridView1.BeginUpdate()
216+
217+
For i As Integer = 0 To 10 - 1
218+
Me.RadGridView1.Rows.Add(i & "0.Data" & Environment.NewLine & i & "1.Data")
219+
Next
220+
221+
Me.radGridView1.EndUpdate()
222+
AddHandler Me.RadGridView1.CellEditorInitialized, AddressOf radGridView1_CellEditorInitialized
223+
AddHandler Me.RadGridView1.ValueChanging, AddressOf radGridView1_ValueChanging
224+
End Sub
225+
226+
Private Sub radGridView1_ValueChanging(ByVal sender As Object, ByVal e As ValueChangingEventArgs)
227+
Me.radGridView1.CurrentCell.Value = e.NewValue
228+
End Sub
229+
230+
Private Sub radGridView1_CellEditorInitialized(ByVal sender As Object, ByVal e As GridViewCellEventArgs)
231+
Dim tbEditor As RadTextBoxEditor = TryCast(e.ActiveEditor, RadTextBoxEditor)
232+
233+
If tbEditor IsNot Nothing Then
234+
tbEditor.Multiline = True
235+
tbEditor.AcceptsReturn = True
236+
End If
237+
End Sub
238+
239+
````
240+
241+
# See Also
242+
243+
* [Resizing rows]({%slug winforms/gridview/rows/resizing-rows%})
244+

telerik-presentation-framework/text-rendering.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ All controls that contain a **TextPrimitive** in its internal structure can spec
3131

3232
* [GDI+](https://msdn.microsoft.com/en-us/library/windows/desktop/ms533798(v=vs.85).aspx)
3333
* [GDI](https://msdn.microsoft.com/en-us/library/windows/desktop/dd145203(v=vs.85).aspx)
34-
* [How to Change the Font of Telerik RadControls]({%slug change-the-font%})
34+
* [How to Change the Font of Telerik RadControls]({%slug change-the-font%})
35+
* [How to Measure Text]({%slug measure-text%})

0 commit comments

Comments
 (0)