This is a migrated thread and some comments may be shown as answers.
How to auto expand the width of dropdown menu items?
5 Answers 129 Views
This is a migrated thread and some comments may be shown as answers.
john
Top achievements
Rank 1
john asked on 15 Sep 2012, 02:40 PM
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?

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 17 Sep 2012, 07:49 AM
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.
0
john
Top achievements
Rank 1
answered on 17 Sep 2012, 09:32 AM
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.
0
Ivan Zhekov
Telerik team
answered on 19 Sep 2012, 07:18 AM
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.
0
Ryan Pope
Top achievements
Rank 1
answered on 30 Oct 2013, 07:54 PM
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."
0
Princy
Top achievements
Rank 1
answered on 31 Oct 2013, 09:42 AM
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.
Asked by
john
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
john
Top achievements
Rank 1
Ivan Zhekov
Telerik team
Ryan Pope
Top achievements
Rank 1
Share this question
or