473,507 Members | 8,054 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6813
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2956
by: Ganco | last post by:
We have a dropdown combobox, where some of the text is wider than the textarea. I cannot set the width of the SELECT any higher due to design restrictions on the company website. When clicking...
3
10727
by: Robert Mark Bram | last post by:
Hi All! Has anyone seen an example of the following javascript powered control: - a dropdown select menu - the width of the control is set. Assume by "control" I mean the visible part of the...
3
16144
by: RJN | last post by:
Hi The texts in the dropdown are too long and the width is not sufficient to show the entire text. Increasing the width is not an option. Is there a way to show the selected item text as a...
7
3307
by: aviad | last post by:
I am writing a Form application I need it to fit both resolution of 1600*1200 and 800*600 (and any other resolution that might jump in) the application is meant for regular PCs another question...
3
4956
by: evilmousse | last post by:
my app has some particularly long dropdownlist items. rather than taking up such wide realestate, i'm looking to have the dropdown's width grow upon focus and shrink back on lose focus. however i'm...
1
1557
by: Samuel Shulman | last post by:
I would like to set the width of the list of the drop down box to be wider then the top of the control Is that possible? Samuel
19
3647
by: Jim | last post by:
Hi, I have two questions/problems pertaining to CSS horizontal dropdown menus and am hoping that someone here can help me out. (1) I'm having a problem centering the menu. I picked up the...
0
1374
by: Andrus | last post by:
I need to make dropdown list wider than column width. I tried code below but dropdown width is not increased when I open dropdown. How to increase it ? using System; using...
0
1059
by: PromisedOyster | last post by:
I have a datagridview. One of the database bound columns shows a code. However, when I drop down this combo, I would like to see a code + description as this may help the user select the...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7030
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5041
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.