 |
|
 |
It helps me more
|
|
|
|
 |
|
 |
hi sir,
can you send code for this sample application in web based.
mail id : stulnaik123@gmail.com
Thanks
Atul Naik
|
|
|
|
 |
|
 |
hi sir,
can you send me code of this application in web based...
mail id- atulnaik123@gmail.com
thanks & regards
Atul Naik
|
|
|
|
 |
|
 |
private void AdjustWidthComboBox_DropDown(object sender, System.EventArgs e)
{
ComboBox senderComboBox = (ComboBox)sender;
int width = senderComboBox.DropDownWidth;
Graphics g = senderComboBox.CreateGraphics();
Font font = senderComboBox.Font;
int vertScrollBarWidth =
(senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
? SystemInformation.VerticalScrollBarWidth : 0;
int newWidth;
foreach (object item in ((ComboBox)sender).Items)
{
string s = senderComboBox.GetItemText(item);
newWidth = (int)g.MeasureString(s, font).Width
+ vertScrollBarWidth;
if (width < newWidth)
{
width = newWidth;
}
}
senderComboBox.DropDownWidth = width;
}
|
|
|
|
 |
|
 |
How can I make it work with Databound combobox? In this case, Items property points to row collection, and we dont get strings in that collection.
|
|
|
|
 |
|
 |
got my 5
|
|
|
|
 |
|
 |
Hi All,
You can use an extension method so you don't have to rewrite the samecode for all your ComboBoxes
ExtensionMethods.cs
using System;
using System.Linq;
using System.Windows.Forms;
namespace MyNamespace
{
public static class ExtensionMethods
{
public static void AutoSizeDropDownWidth(this ComboBox sender)
{
if (sender.Items.Count == 0)
{
sender.DropDownWidth = sender.Width;
return;
}
var comboBoxItems = new object[sender.Items.Count];
sender.Items.CopyTo(comboBoxItems, 0);
using (var graphics = sender.CreateGraphics())
{
var largestItem = (from width in
(from item in comboBoxItems
select Convert.ToInt32(graphics.MeasureString(item.ToString(), sender.Font).Width))
select width).Max();
sender.DropDownWidth = largestItem > sender.Width ? largestItem : sender.Width;
}
}
}
}
now the only thing you need to call is
private void comboBoxTypes_DropDown(object sender, EventArgs e)
{
comboBoxTypes.AutoSizeDropDownWidth();
}
|
|
|
|
 |
|
 |
Could you please send me the code to daniel_h_hernandez@msn.com
Thanks in advance,
Daniel
Daniel HH
|
|
|
|
 |
|
 |
Hi SathishVJ,
Its a very good article, most of the developer faces this issue. after reading your article but i couldn't implement the same. i have to ask that
On which event we have to bind this? because i have used on OnLoad event but it didn't work.
So, Please mention the event name as well as aspx code for the same. so that i can implement the same.
Your reply will be helpful for me.
Thanks & Regards
Harsh
|
|
|
|
 |
|
 |
hi
can u plz provide sample source code for this article..
Thanks iam waitng for ur reply...
ramesh n
ramesh
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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)
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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)
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
I want brief explanation on custom validations in asp .net with a small example
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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.
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
can you please send me the code to my mail id : surendran.u@gmail.com
|
|
|
|
 |
|
 |
Any chance you could publish the code in VB?
|
|
|
|
 |