473,386 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 6805
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
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
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
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
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
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
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
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.