 |
|
|
If the items of the combobox are created dynamically, for the better don't assign senderComboBox.DropDownWidth to variable 'width'. If the new string's length is shorter than the old one's, the drop down list length would be too long.
qian_xu
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I would suggest that you also set the ComboBox.Width property, that way the drop down matches the width of the box it is coming from. Also, if you add a little bit to the width, like 5 or so, it will take into account the button at the end of the box.
Otherwise, Im glad I could find this, I had this code but it is somewhere else.
CleaKO
"I think you'll be okay here, they have a thin candy shell. 'Surprised you didn't know that.'" - Tommy (Tommy Boy) "Fill it up again! Fill it up again! Once it hits your lips, it's so good!" - Frank the Tank (Old School)
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I want to know if this method can work with a DropDownList instead of a combo box. It would be very kindful of you if you could suggest me some solution. Its really very urgent.
Vinod G Nair
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
A DropDownList is the same thing as a combobox in the sense of a Windows App. When speaking about a web application, though, a DropDownList autosizes itself.
CleaKO
"I think you'll be okay here, they have a thin candy shell. 'Surprised you didn't know that.'" - Tommy (Tommy Boy) "Fill it up again! Fill it up again! Once it hits your lips, it's so good!" - Frank the Tank (Old School)
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
This does work for English, but not for other language font. It seems like the newWidth measured from other language characters are less than the actual appearance. For example, i added Thai-language items into a combobox which has a width of 121. The max width of the items measured from the code is 119 but its length actually exceeds (it appears to exceed) the width of the combobox. Any suggestion on this issue please?...
One way i'm trying to do is adding more length for the new width but don't know how many pixel or how many percentage to add... T-T
BTW, i added this line of code after measuring the newWidth:
newWidth = (int)((double)newWidth * 1.25);
KiT
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I just want to know if this method can work with a DropDownList instead of a combo box. If yes, wich EventHandler i must use because the event DropDown doesn't exist ?
Thanks a lot
|
| Sign In·View Thread·PermaLink | 2.00/5 (2 votes) |
|
|
|
 |
|
|
Here is the vb.net code, I used the developerfusion.com converter
Private Sub AdjustWidthComboBox_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Dim senderComboBox As ComboBox = CType(sender, ComboBox) Dim width As Integer = senderComboBox.DropDownWidth Dim g As Graphics = senderComboBox.CreateGraphics Dim font As Font = senderComboBox.Font Dim vertScrollBarWidth As Integer = Microsoft.VisualBasic.IIf((senderComboBox.Items.Count > senderComboBox.MaxDropDownItems),SystemInformation.VerticalScrollBarWidth,0) Dim newWidth As Integer For Each s As String In CType(sender, ComboBox).Items newWidth = CType(g.MeasureString(s, font).Width, Integer) + vertScrollBarWidth If width < newWidth Then width = newWidth End If Next senderComboBox.DropDownWidth = width End Sub
Alardaen http://www.gvilas.com
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Hi,
the code is nice and usefull, but I think it would be better if you only calculate the width when the combobox gets filled instead of doing the always in the OnDrop event.
You could add a flag in the eventhandler or whatever.
But anyhow, it's nice (I used it thanks,
regards, Ike
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Very Nice 
To make the code a little more general, allow the combo items to be objects and use combobox.GetItemText(Item) to get the string to measure:
foreach (Object s in senderComboBox.Items) { string ss = senderComboBox.GetItemText(s); newWidth = (int)g.MeasureString(ss, font).Width + vertScrollBarWidth; etc.
Thanks for the code and a useful, simple trick.
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
Hello, everyone
Good afternoon, would you mind to tell me, is it work in the ASP.NET Web Page or only work in the .Net windows, if it can use in the ASP.NET, please advise me how to use or call or what ever to use it.
Thank you very much. Oscar
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
If the feature is added as it in the article, it is working fine. I have tried to encapsulate this functionality by extending the ComboBox , the code should go to OnDropDown protected method. I have used the code
using System.Drawing; //add it in the top of the code
protected override void OnDropDown(EventArgs e) { int width = this.DropDownWidth; Graphics g = this.CreateGraphics(); Font font = this.Font; int vertScrollBarWidth = (this.Items.Count > this.MaxDropDownItems) ?SystemInformation.VerticalScrollBarWidth ;
int newWidth; foreach (string s in this.Items) { newWidth = (int) g.MeasureString(s, font).Width + vertScrollBarWidth; if (width < newWidth ) { width = newWidth; } } this.DropDownWidth = width; base.OnDropDown(e); }
The code fails to compile and gives the error messages 'System.Drawing.Graphics' is inaccessible due to its protection level 'System.Drawing.Font' is inaccessible due to its protection level
The compiler is not allowing creating Graphics and Font objects inside the entire class. Please send your ideas to solve the problem.
Thanks, -Srinivaasan
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I tried your code in a couple of ways and it works perfectly fine. Mebbe there is something here that I do not see.
Tried mailing u code that works for me but ur mailbox is full.
~/sathishvj
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
This is what I use, and is based off some code by Erik Porter and others:
Public Sub RecalculateDropDownBoxWidth() ' Just reset if there are no items If Me.Items.Count = 0 Then Me.DropDownWidth = Me.Width Return End If
Dim g As Graphics = Me.CreateGraphics() Dim WidestWidth As Integer = 0 Dim ValueToMeasure As String Dim CurrentWidth As Integer Dim LeftCorner As Integer
' Find the longest string For i As Integer = 0 To Me.Items.Count - 1 ValueToMeasure = Me.GetItemText(Me.Items(i)) CurrentWidth = CType(g.MeasureString(ValueToMeasure, Me.Font).Width, Integer) WidestWidth = Math.Max(CurrentWidth, WidestWidth) Next
' Add a little space for a scroll bar If Me.Items.Count > Me.MaxDropDownItems Then WidestWidth += SystemInformation.VerticalScrollBarWidth End If
' Make sure we don't go off the screen LeftCorner = Me.PointToScreen(New Point(0, Me.Left)).X WidestWidth = Math.Min(Screen.PrimaryScreen.WorkingArea.Width - LeftCorner, WidestWidth)
Me.DropDownWidth = Math.Max(WidestWidth, Me.Width)
g.Dispose() End Sub
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |