How to auto expand the width of dropdown menu items?

6 posts, 0 answers
  1. john
    john avatar
    67 posts
    Member since:
    Jan 2012

    Posted 15 Sep 2012 Link to this post

    If the width of some dropdown menu items exceed the width of RadCombox, how to expand dropdown menu in that case all menu items will be shown?
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 17 Sep 2012 Link to this post

    Hi John,

    Try the following code snippet to achieve your scenario.

    ASPX:
    <telerik:RadComboBox ID="RadComboBox1" runat="server" NoWrap="true" ondatabound="RadComboBox1_DataBound">
    </telerik:RadComboBox>

    C#:
    protected void RadComboBox2_DataBound(object sender, EventArgs e)
        {
            var cbox = ((RadComboBox)sender);
            int MaxWidth = 155;
     
            foreach (RadComboBoxItem item in cbox.Items)
            {
                int Width = TextWidth(item.Text);
                if (Width > MaxWidth)
                {
                    MaxWidth = Width;
                }
            }
     
            cbox.DropDownWidth = new Unit(MaxWidth);
        }
     
    public static int TextWidth(String TheText)
        {
            Font DrawFont = null;
            Graphics DrawGraphics = null;
            Bitmap TextBitmap = null;
            try
            {
                TextBitmap = new Bitmap(1, 1);
                DrawGraphics = Graphics.FromImage(TextBitmap);
                DrawFont = new Font("Segoe UI", 12);
     
                int Width = (int)DrawGraphics.MeasureString(TheText, DrawFont).Width;
     
                return Width;
            }
            finally
            {
                TextBitmap.Dispose();
                DrawFont.Dispose();
                DrawGraphics.Dispose();
            }
        }

    Please take a look into this for more information.

    Hope this helps.

    Regards,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. john
    john avatar
    67 posts
    Member since:
    Jan 2012

    Posted 17 Sep 2012 Link to this post

    Thanks. Also, whether you have any plan to integrate the feature into your RadCombox control? After all developer need to add those code for every RadCombox and it's not very not convenient.
  5. Ivan Zhekov
    Admin
    Ivan Zhekov avatar
    539 posts

    Posted 19 Sep 2012 Link to this post

    Hi,

    We are planning to incorporate such clientside feature in the ComboBox. At the moment it's looking very promising and the best part of it, it does not iterate over the items to find the widest item, but uses another approach instead.

    Regards,
    Ivan Zhekov
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  6. Ryan Pope
    Ryan Pope avatar
    61 posts
    Member since:
    Apr 2008

    Posted 30 Oct 2013 Link to this post

    Ivan,

    Did you ever get this added into RadComboboxFunctionality?
    "Hi,
    We are planning to incorporate such clientside feature in the ComboBox. At the moment it's looking very promising and the best part of it, it does not iterate over the items to find the widest item, but uses another approach instead."
  7. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 31 Oct 2013 Link to this post

    Hi Ryan Pope,

    Please try setting the  DropDownAutoWidth property  of a RadComboBox to Enabled for expanding  the width of DropDownItems. By default its value is disabled.Please have a look into this help documentation for more details..

    Thanks,
    Princy.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017