Hello,
I have a form with limited layout space for things like super wide comboboxes. That said, I have a combobox that I'd like to have the DropDownList width fit to the widest item in the data collection. Is this possible?
I realize I could simply make the combobox's width larger but 1) I'm trying to keep things visually looking nice and 2) the combobox's collection I'm trying to accommodate is actually sourced on form load from a dynamic json file (refreshed nightly).
Thanks for the help,
Joey
combobox control to allow dynamic width of longest item
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
- joeypiccola
- Posts: 9
- Last visit: Mon Jun 18, 2018 7:44 am
Re: combobox control to allow dynamic width of longest item
There is really no facility in Forms to do what you ask. You would have to custom code this and adjust the position of all items around the ComboBox.
Re: combobox control to allow dynamic width of longest item
Here is an example on now how to do this in C#:
https://stackoverflow.com/questions/484 ... xs-content
https://stackoverflow.com/questions/484 ... xs-content
David
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: combobox control to allow dynamic width of longest item
Here is a quick example of the conversion.
There is a more reliable way using "DrawItem" but it takes more code.
Code: Select all
$form1_Load={
$names = (dir).Fullname
$maxlength = ($names|select -expand length |Measure-Object -Maximum).Maximum
$lbl = [System.Windows.Forms.Label]::New()
$lbl.Text = 'X'*$maxlength
$combobox1.DropdownWidth = $lbl.PreferredWidth
$combobox1.Items.AddRange($names)
}