Skip to content

Commit 50e8c4f

Browse files
author
KB Bot
committed
Added new kb article auto-sizing-bindingnavigator-winforms
1 parent 98e402f commit 50e8c4f

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
title: Auto Sizing BindingNavigator in UI for WinForms
3+
description: Learn how to make the RadBindingNavigator in UI for WinForms grow and shrink dynamically with its contents.
4+
type: how-to
5+
page_title: Dynamically Adjusting RadBindingNavigator Size in WinForms
6+
meta_title: Dynamically Adjusting RadBindingNavigator Size in WinForms
7+
slug: auto-sizing-bindingnavigator-winforms
8+
tags: radbindingnavigator, winforms, bindingsource, autosize, textchanged-event
9+
res_type: kb
10+
ticketid: 1698112
11+
---
12+
13+
## Environment
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td> Product </td>
18+
<td> UI for WinForms RadBindingNavigator </td>
19+
</tr>
20+
<tr>
21+
<td> Version </td>
22+
<td> Current </td>
23+
</tr>
24+
</tbody>
25+
</table>
26+
27+
## Description
28+
29+
I want the RadBindingNavigator to grow or shrink dynamically when its contents change, such as when the `CommandBarLabel` text updates. Placing it inside a `TableLayoutPanel` set to auto-size and without docking or anchoring causes the last button to get hidden when the content grows. Docking the navigator and using alignment and stretch settings on its `CommandBarRowElement` and `CommandBarStripElement` also aligns it to the left instead of keeping it centered.
30+
31+
This knowledge base article also answers the following questions:
32+
- How to make RadBindingNavigator resize dynamically with its content?
33+
- Why does RadBindingNavigator align left when docked and centered in the designer?
34+
- How to handle RadBindingNavigator alignment and resizing in UI for WinForms?
35+
36+
## Solution
37+
38+
To make the RadBindingNavigator grow and shrink dynamically with its contents, manually update its size based on the change in the size of the element holding the text label. Use the following approach:
39+
40+
1. Subscribe to the `TextChanged` event of the `PageLabel` in your code.
41+
2. Implement a delayed execution mechanism using a timer to adjust the width of the RadBindingNavigator.
42+
43+
Here is a sample implementation:
44+
45+
```csharp
46+
public partial class Form1 : Telerik.WinControls.UI.RadForm
47+
{
48+
private int textLength = 0;
49+
50+
public Form1()
51+
{
52+
InitializeComponent();
53+
54+
this.radBindingNavigator1.BindingNavigatorElement.PageLabel.TextChanged += PageLabel_TextChanged;
55+
}
56+
57+
protected override void OnShown(EventArgs e)
58+
{
59+
base.OnShown(e);
60+
61+
// Store the initial width of the PageLabel.
62+
this.textLength = this.radBindingNavigator1.BindingNavigatorElement.PageLabel.BoundingRectangle.Width;
63+
}
64+
65+
private void PageLabel_TextChanged(object? sender, EventArgs e)
66+
{
67+
// Use a timer for delayed execution to handle asynchronous UI updates.
68+
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
69+
timer.Interval = 10; // Set a small delay.
70+
timer.Tick += (t, args) =>
71+
{
72+
timer.Stop();
73+
74+
// Calculate the difference in label width and adjust navigator width.
75+
int newLength = this.radBindingNavigator1.BindingNavigatorElement.PageLabel.BoundingRectangle.Width;
76+
this.radBindingNavigator1.Width += (newLength - this.textLength);
77+
this.textLength = newLength;
78+
};
79+
80+
timer.Start();
81+
}
82+
}
83+
```
84+
85+
### Notes:
86+
- The RadBindingNavigator is designed to be docked, and its inner elements are left-aligned by default. To achieve center alignment and dynamic resizing, use the manual adjustment approach discussed above.
87+
- Test the solution thoroughly as this approach may not handle all possible scenarios.
88+
89+
## See Also
90+
91+
- [RadBindingNavigator Documentation](https://docs.telerik.com/devtools/winforms/controls/binding-navigator/overview)
92+
- [RadCommandBarRowElement API](https://docs.telerik.com/devtools/winforms/api/telerik.wincontrols.ui.commandbarrowelement)
93+
- [RadCommandBarStripElement API](https://docs.telerik.com/devtools/winforms/api/telerik.wincontrols.ui.commandbarstripelement)
94+
- [Handling TextChanged Events in UI for WinForms](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.textchanged?view=windowsdesktop-7.0)

0 commit comments

Comments
 (0)