CommandBarTextBox; Dynamic width by content

2 posts, 1 answers
  1. Raoul
    Raoul avatar
    33 posts
    Member since:
    Apr 2012

    Posted 22 Feb 2013 Link to this post

    I have a RadCommandBar docked (to the bottom). It contains 2 labels and 2 textboxes with 1 seperataror. Its 'RightToLeft' property is set to true. It looks something like this:

                  label1   [ TextBox1 ]   |   label2   [ TextBox2  ]

    I'd like to make the width of TextBox1 depending on it's contents. E.g. when it contains "short" the width is 100px, but when "a somewhat longer text" is displayed e.g. 400px. The textboxes are readonly.

    The properties "AutoSize" and "AutoSizeMode" of the  RadCommandBar, CommandBarRowElement, CommandBarStripElement and CommandBarTextBox elements are set to True and Auto. The textbox doesn't resize when the textcontent changes?

    How can I obtain this functionality?

    Kind regards,

  2. Answer
    Peter avatar
    1148 posts

    Posted 26 Feb 2013 Link to this post

    Hello Raoul,

    Thank you for writing.

    CommandBarTextBox/RadTextBox are editable controls and this is why they does not support re-sizing. By design, the editable controls does not have this option. 
    However you can re-size manually the control on TextChanged event. For example:
    this.commandBarTextBox1.TextChanged += new System.EventHandler(this.commandBarTextBox1_TextChanged);
     private void commandBarTextBox1_TextChanged(object sender, EventArgs e)
         CommandBarTextBox textBoxElement = ((RadTextBoxItem)sender).FindAncestor<CommandBarTextBox>();
         Size textSize = TextRenderer.MeasureText(textBoxElement.Text, textBoxElement.Font);
         textBoxElement.MinSize = new Size(textSize.Width, 22);

    I hope this helps.

    Kind regards,
    the Telerik team
    Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
  3. DevCraft R3 2016 release webinar banner
Back to Top