Combo autosize dropdown lf/rt?

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;
}
 
C

Christof Nordiek

randy1200 said:
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?

You could recalculate the Left property of the control thereby fixing it at
the right (are some more sofisticated alignment rules). Still you'll get in
trouble if the controle getse wider then the form.

Christof
 
G

Guest

I tried recalculating the left property of the combobox. All it ended up
doing was walking the combo box to the left every time it was clicked. I need
to be able to recalculate the left position of just the dropdown portion of
the control, but as far as I can see, that isn't exposed by Microsoft to the
developer. I also tried changing the anchor point [left, top] to right, but I
still get the dropdown expanding to the right for long text entires.

I also don't see anything about this in Infragistics or other aftermarket
controls. That surprises me. I can't believe I'm the first person to place a
combobox with wide text entries on the right-hand edge of the screen.

Thanks for the response. Any other responses are more than welcome.

Randy
 
C

Christof Nordiek

randy1200 said:
I tried recalculating the left property of the combobox. All it ended up
doing was walking the combo box to the left every time it was clicked. I
need
to be able to recalculate the left position of just the dropdown portion
of
the control, but as far as I can see, that isn't exposed by Microsoft to
the
developer. I also tried changing the anchor point [left, top] to right,
but I
still get the dropdown expanding to the right for long text entires.

Did you try to set the DropDownWidth?

Christof
 
G

Guest

Christof Nordiek said:
Did you try to set the DropDownWidth?

Yes. I found that if you set the DropDownWidth to less than the width of the
combo box, the dropdown is still the width of the combo box. If I set the
DropDownWidth to more than the width of the combo box, the dropdown expands
to the right. Since this particular combo box is on the right edge of the
screen, the expanding dropdown text falls off the right edge of the screen.
My hope here is to figure out how to make it expand to the left.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top