Add an SVG image button to a Telerik RadTextBox with and Embedded Label

Discovered a new feature in the Telerik Winforms framework. Embedded Labels! Now to add a button to the control. There are several layers that need to be moved and reattached.


private void TextBoxButton(RadTextBox textBox, string svgImagePath)
{
// Copy existing RadTextBox elements
RadTextBoxItem tbe = textBox.TextBoxElement.TextBoxItem;
LightVisualElement el = textBox.TextBoxElement.EmbeddedLabel;
StackLayoutPanel slp = textBox.TextBoxElement.ButtonsStack;

// Remove them
textBox.TextBoxElement.Children.Remove(slp);
textBox.TextBoxElement.Children.Remove(tbe);
textBox.TextBoxElement.Children.Remove(el);

// Create a new button
RadButtonElement button = new RadButtonElement();
button.Click += new EventHandler(button_Click);
button.SvgImage = RadSvgImage.FromByteArray(svgImagePath);
button.SvgImage.Size = new Size(15, 15);
button.ShowBorder = false;
button.Tag = textBox.Name;
button.Margin = new Padding(0, 0, 10, 0);

// Add existing elements and button to panel
DockLayoutPanel dockPanel = new DockLayoutPanel();
dockPanel.Children.Add(button);
dockPanel.Children.Add(tbe);
dockPanel.Children.Add(slp);
dockPanel.Children.Add(el);

// Dock all elements. Position is important.
DockLayoutPanel.SetDock(slp, Telerik.WinControls.Layouts.Dock.Left);
DockLayoutPanel.SetDock(el, Telerik.WinControls.Layouts.Dock.Top);
DockLayoutPanel.SetDock(tbe, Telerik.WinControls.Layouts.Dock.Bottom);
DockLayoutPanel.SetDock(button, Telerik.WinControls.Layouts.Dock.Right);

// Add the panel to the RadTextBox
textBox.TextBoxElement.Children.Add(dockPanel);
}

Leave a Reply