G
Guest
The code below goes into the dropdown event handler for a standard combo box.
It makes the dropdown as wide as the widest text entry. Unfortunately, this
combo box is on the right edge of the screen, so if the user has a really
wide entry, the dropdown expands off the right edge of the screen.
Any suggestions on how to modify the code below to make the dropdown width
expand to the left, instead of to the right?
Thanks,
Randy
private void AdjustWidthToText_CBCycleLabel(object sender, EventArgs e)
{
ComboBox _cycleLabel = (ComboBox)sender;
int width = _cycleLabel.DropDownWidth;
System.Drawing.Graphics g = _cycleLabel.CreateGraphics();
System.Drawing.Font font = _cycleLabel.Font;
int vertScrollBarWidth = (_cycleLabel.Items.Count >
_cycleLabel.MaxDropDownItems) ? SystemInformation.VerticalScrollBarWidth : 0;
int newWidth = 0;
foreach (object o in ((ComboBox)sender).Items)
{
DataRowView drv = (DataRowView)o;
string s = drv["DESCRIPTION"].ToString();
newWidth = (int)g.MeasureString(s, font).Width + vertScrollBarWidth;
if (width < newWidth)
width = newWidth;
}
_cycleLabel.DropDownWidth = width;
}
It makes the dropdown as wide as the widest text entry. Unfortunately, this
combo box is on the right edge of the screen, so if the user has a really
wide entry, the dropdown expands off the right edge of the screen.
Any suggestions on how to modify the code below to make the dropdown width
expand to the left, instead of to the right?
Thanks,
Randy
private void AdjustWidthToText_CBCycleLabel(object sender, EventArgs e)
{
ComboBox _cycleLabel = (ComboBox)sender;
int width = _cycleLabel.DropDownWidth;
System.Drawing.Graphics g = _cycleLabel.CreateGraphics();
System.Drawing.Font font = _cycleLabel.Font;
int vertScrollBarWidth = (_cycleLabel.Items.Count >
_cycleLabel.MaxDropDownItems) ? SystemInformation.VerticalScrollBarWidth : 0;
int newWidth = 0;
foreach (object o in ((ComboBox)sender).Items)
{
DataRowView drv = (DataRowView)o;
string s = drv["DESCRIPTION"].ToString();
newWidth = (int)g.MeasureString(s, font).Width + vertScrollBarWidth;
if (width < newWidth)
width = newWidth;
}
_cycleLabel.DropDownWidth = width;
}