471,344 Members | 1,248 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 software developers and data experts.

How to make dropdown list wider than control's width?

Hello, friends,

We use System.Windows.Forms.ComboBox in our c#.net 2005 app. We want to make
the dropdown list wider than the comboBox's width. (Some items in the
dropdown list have more characters that go beyond the comboBox's width).

How do we do it? Thanks a lot.
Sep 12 '08 #1
3 6677
Hi i have this script in VB, so if you could translate, you can use it.
this code keeps the combo size, and only the dropdown list is wider than the
combo itself, and it matches the longest item in the list.

Private Function GetCboWidth(ByVal myIndex As Integer)
Dim cwidth As Long
Dim cnt As Long
Dim NumOfChars As Long
Dim LongestComboItem As Long
Dim avgCharWidth As Long
Dim NewDropDownWidth As Long
For cnt = 0 To cboCombo(myIndex).ListCount - 1
NumOfChars = SendMessage(cboCombo(myIndex).hwnd, CB_GETLBTEXTLEN, cnt,
ByVal 0)
If NumOfChars LongestComboItem Then LongestComboItem = NumOfChars
Next

avgCharWidth = GetFontDialogUnits()

NewDropDownWidth = (LongestComboItem - 2) * avgCharWidth

Call SendMessage(cboCombo(myIndex).hwnd, CB_SETDROPPEDWIDTH,
NewDropDownWidth, ByVal 0)

cwidth = SendMessage(cboCombo(myIndex).hwnd, CB_GETDROPPEDWIDTH, 0, ByVal
0)

End Function

"Andrew" <An****@discussions.microsoft.comwrote in message
news:F9**********************************@microsof t.com...
Hello, friends,

We use System.Windows.Forms.ComboBox in our c#.net 2005 app. We want to
make
the dropdown list wider than the comboBox's width. (Some items in the
dropdown list have more characters that go beyond the comboBox's width).

How do we do it? Thanks a lot.

Sep 12 '08 #2
I converted it to c#.net. It works now. Thanks a lot.

"Wndr" wrote:
Hi i have this script in VB, so if you could translate, you can use it.
this code keeps the combo size, and only the dropdown list is wider than the
combo itself, and it matches the longest item in the list.

Private Function GetCboWidth(ByVal myIndex As Integer)
Dim cwidth As Long
Dim cnt As Long
Dim NumOfChars As Long
Dim LongestComboItem As Long
Dim avgCharWidth As Long
Dim NewDropDownWidth As Long
For cnt = 0 To cboCombo(myIndex).ListCount - 1
NumOfChars = SendMessage(cboCombo(myIndex).hwnd, CB_GETLBTEXTLEN, cnt,
ByVal 0)
If NumOfChars LongestComboItem Then LongestComboItem = NumOfChars
Next

avgCharWidth = GetFontDialogUnits()

NewDropDownWidth = (LongestComboItem - 2) * avgCharWidth

Call SendMessage(cboCombo(myIndex).hwnd, CB_SETDROPPEDWIDTH,
NewDropDownWidth, ByVal 0)

cwidth = SendMessage(cboCombo(myIndex).hwnd, CB_GETDROPPEDWIDTH, 0, ByVal
0)

End Function

"Andrew" <An****@discussions.microsoft.comwrote in message
news:F9**********************************@microsof t.com...
Hello, friends,

We use System.Windows.Forms.ComboBox in our c#.net 2005 app. We want to
make
the dropdown list wider than the comboBox's width. (Some items in the
dropdown list have more characters that go beyond the comboBox's width).

How do we do it? Thanks a lot.


Sep 12 '08 #3


"Andrew" <An****@discussions.microsoft.comwrote in message
news:F9**********************************@microsof t.com...
Hello, friends,

We use System.Windows.Forms.ComboBox in our c#.net 2005 app. We want to
make
the dropdown list wider than the comboBox's width. (Some items in the
dropdown list have more characters that go beyond the comboBox's width).

How do we do it? Thanks a lot.
A non-pinvoke way:

private void ConfigureDropDownWindowWidth(ComboBox control)
{
int width = control.DropDownWidth;
Font f = control.Font;

using (Bitmap bmp = new Bitmap(16, 16)) {
using (Graphics canvas = Graphics.FromImage(bmp)) {
foreach (object item in control.Items) {
string s = item as string;
if (!string.IsNullOrEmpty(s)) {
// Measure the item's width.
SizeF itemSize = canvas.MeasureString(s, f);
if (width < itemSize.Width) {
width = (int) itemSize.Width;
}
}
}
}
}

if (width control.DropDownWidth) {
// Set the width of the drop-down area.
control.DropDownWidth = width;
}
}

HTH,
Mythran
Sep 12 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Ganco | last post: by
3 posts views Thread by RJN | last post: by
3 posts views Thread by evilmousse | last post: by
1 post views Thread by Samuel Shulman | last post: by
reply views Thread by PromisedOyster | last post: by
reply views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.