Skip to content

Commit a176880

Browse files
authored
Merge pull request #794 from telerik/new-kb-auto-sizing-bindingnavigator-winforms-20abee1b41a84b9699140391cff325ed
Added new kb article auto-sizing-bindingnavigator-winforms
2 parents 735ff2c + aee7d1e commit a176880

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
title: Auto Sizing BindingNavigator when its content grows
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 Adjust RadBindingNavigator Size
6+
meta_title: Dynamically Adjust RadBindingNavigator Size
7+
slug: auto-sizing-bindingnavigator-winforms
8+
tags: bindingnavigator, winforms, bindingsource, autosize, textchanged-event
9+
res_type: kb
10+
ticketid: 1698112
11+
---
12+
13+
## Environment
14+
15+
|Product Version|Product|Author|
16+
|----|----|----|
17+
|2025.3.812|RadBindingNavigator for WinForms|[Nadya Karaivanova](https://www.telerik.com/blogs/author/nadya-karaivanova)|
18+
19+
## Description
20+
21+
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.
22+
23+
This knowledge base article answers how to make RadBindingNavigator resize dynamically with its content.
24+
25+
## Solution
26+
27+
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:
28+
29+
1. Subscribe to the `TextChanged` event of the `BindingNavigatorElement.PageLabel` in your code.
30+
2. Implement a delayed execution mechanism using a timer to adjust the width of the RadBindingNavigator.
31+
32+
Here is a sample implementation:
33+
34+
````C#
35+
36+
public partial class Form1 : Telerik.WinControls.UI.RadForm
37+
{
38+
private int textLength = 0;
39+
40+
public Form1()
41+
{
42+
InitializeComponent();
43+
44+
this.radBindingNavigator1.BindingNavigatorElement.PageLabel.TextChanged += PageLabel_TextChanged;
45+
}
46+
47+
protected override void OnShown(EventArgs e)
48+
{
49+
base.OnShown(e);
50+
51+
// Store the initial width of the PageLabel.
52+
this.textLength = this.radBindingNavigator1.BindingNavigatorElement.PageLabel.BoundingRectangle.Width;
53+
}
54+
55+
private void PageLabel_TextChanged(object? sender, EventArgs e)
56+
{
57+
// Use a timer for delayed execution to handle asynchronous UI updates.
58+
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
59+
timer.Interval = 10; // Set a small delay.
60+
timer.Tick += (t, args) =>
61+
{
62+
timer.Stop();
63+
64+
// Calculate the difference in label width and adjust navigator width.
65+
int newLength = this.radBindingNavigator1.BindingNavigatorElement.PageLabel.BoundingRectangle.Width;
66+
this.radBindingNavigator1.Width += (newLength - this.textLength);
67+
this.textLength = newLength;
68+
};
69+
70+
timer.Start();
71+
}
72+
}
73+
74+
````
75+
76+
![Dynamically Adjusting RadBindingNavigator Size](images/bindingnavigator-auto-sizing.gif)
119 KB
Loading

0 commit comments

Comments
 (0)