By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 983 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a


"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.