From: Tom P. on
I am trying to make a ComboBox that will AutoResize to the text being
displayed. The steps I take are:
OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
ResizeTextBox();
}

private void ResizeTextBox()
{
Graphics CurrentGraphics;
int StringWidth;
int MinWidth;
int MaxWidth;
int MyWidth;
string thisText;
Font thisFont;
SizeF thisSizeF;
Size thisSize;

CurrentGraphics = this.CreateGraphics();

thisText = this.Text;
thisFont = new Font(this.Font, FontStyle.Regular);
thisSizeF = CurrentGraphics.MeasureString(thisText,
thisFont);
thisSize = thisSizeF.ToSize();
StringWidth = thisSize.Width;

if (StringWidth == 0)
{
// If they delete the text don't bother to resize.
this.Width = this.MinimumSize.Width;
//this.DropDownWidth = this.MinimumSize.Width;
return;
}

MinWidth = Math.Max(StringWidth + 20,
this.MinimumSize.Width);

MaxWidth = this.Parent.Width - this.Location.X - 3;
MyWidth = Math.Min(MinWidth, MaxWidth);

this.Width = MyWidth;

}

The problem I run into is when I reset the width of the control it
fires a Resize event. somewhere between that and the end of the
TextChanged event the text is getting reset to the original... but
only when the text is shortened. In other words, if I go from C:\ to C:
\Temp it works fine. If I go from C:\Temp to C:\Tem the text gets
reset to C:\Temp

Why is the text getting reset? How can I interrupt this process?

Thanks for the help,
Tom P,