Board index » delphi » ComboBox with expanding width for drop-down list

ComboBox with expanding width for drop-down list

Does anyone know where to get a ComboBox that will autoexpand the
width of the drop-down list for longer strings?
 

Re:ComboBox with expanding width for drop-down list


The following code should work, but for some reason it makes the dropdown to
big. Maybe somebody else could point out the error in my code:

procedure TForm1.ComboBox1DropDown(Sender: TObject);
var
  DC: HDC;
  i: Integer;
  W: Integer;
  Size: TSize;
begin
  with Sender as TComboBox do
  begin
    DC := GetDC(Handle);
    try
      W := 0;
      for i := 0 to Items.Count - 1 do
      begin
        GetTextExtentPoint32(DC, PChar(Items[i]), Length(Items[i]), Size);
        if W < Size.cx then
          W := Size.cx;
      end;
      Perform(CB_SETDROPPEDWIDTH, W + 4, 0);
    finally
      ReleaseDC(Handle, DC);
    end;
  end;
end;

"Ken Phipps" <kphipp...@netscape.net> schreef in bericht
news:9214issgnc03j9duu7f21joe111usik39o@4ax.com...

Quote
> Does anyone know where to get a ComboBox that will autoexpand the
> width of the drop-down list for longer strings?

Re:ComboBox with expanding width for drop-down list


Hi!

Choose the font of the combo box first!

Se this:

    //Peter Glimstedt

procedure TForm1.ComboBox1DropDown(Sender: TObject);
var
  DC: HDC;
  i: Integer;
  W: Integer;
  Size: TSize;
  Old: THandle;
begin
  with Sender as TComboBox do
  begin
    DC := GetDC(Handle);
    Old:= SelectObject(DC, Font.Handle);
    try
      W := 0;
      for i := 0 to Items.Count - 1 do
      begin
        GetTextExtentPoint32(DC, PChar(Items[i]), Length(Items[i]), Size);
        if W < Size.cx then
          W := Size.cx;
      end;
      Perform(CB_SETDROPPEDWIDTH, W + 4, 0);
    finally
      SelectObject(DC, Old);
      ReleaseDC(Handle, DC);
    end;
  end;
end;

Quote
"M.H. Avegaart" wrote:
> The following code should work, but for some reason it makes the dropdown to
> big. Maybe somebody else could point out the error in my code:

> procedure TForm1.ComboBox1DropDown(Sender: TObject);
> var
>   DC: HDC;
>   i: Integer;
>   W: Integer;
>   Size: TSize;
> begin
>   with Sender as TComboBox do
>   begin
>     DC := GetDC(Handle);
>     try
>       W := 0;
>       for i := 0 to Items.Count - 1 do
>       begin
>         GetTextExtentPoint32(DC, PChar(Items[i]), Length(Items[i]), Size);
>         if W < Size.cx then
>           W := Size.cx;
>       end;
>       Perform(CB_SETDROPPEDWIDTH, W + 4, 0);
>     finally
>       ReleaseDC(Handle, DC);
>     end;
>   end;
> end;

> "Ken Phipps" <kphipp...@netscape.net> schreef in bericht
> news:9214issgnc03j9duu7f21joe111usik39o@4ax.com...
> > Does anyone know where to get a ComboBox that will autoexpand the
> > width of the drop-down list for longer strings?

Re:ComboBox with expanding width for drop-down list


Thank !!!

"Peter Glimstedt" <p...@chl.chalmers.se> schreef in bericht
news:39228BDB.DCB24425@chl.chalmers.se...

Quote
> Hi!

> Choose the font of the combo box first!

> Se this:

>     file://Peter Glimstedt

> procedure TForm1.ComboBox1DropDown(Sender: TObject);
> var
>   DC: HDC;
>   i: Integer;
>   W: Integer;
>   Size: TSize;
>   Old: THandle;
> begin
>   with Sender as TComboBox do
>   begin
>     DC := GetDC(Handle);
>     Old:= SelectObject(DC, Font.Handle);
>     try
>       W := 0;
>       for i := 0 to Items.Count - 1 do
>       begin
>         GetTextExtentPoint32(DC, PChar(Items[i]), Length(Items[i]), Size);
>         if W < Size.cx then
>           W := Size.cx;
>       end;
>       Perform(CB_SETDROPPEDWIDTH, W + 4, 0);
>     finally
>       SelectObject(DC, Old);
>       ReleaseDC(Handle, DC);
>     end;
>   end;
> end;

> "M.H. Avegaart" wrote:

> > The following code should work, but for some reason it makes the
dropdown to
> > big. Maybe somebody else could point out the error in my code:

> > procedure TForm1.ComboBox1DropDown(Sender: TObject);
> > var
> >   DC: HDC;
> >   i: Integer;
> >   W: Integer;
> >   Size: TSize;
> > begin
> >   with Sender as TComboBox do
> >   begin
> >     DC := GetDC(Handle);
> >     try
> >       W := 0;
> >       for i := 0 to Items.Count - 1 do
> >       begin
> >         GetTextExtentPoint32(DC, PChar(Items[i]), Length(Items[i]),
Size);
> >         if W < Size.cx then
> >           W := Size.cx;
> >       end;
> >       Perform(CB_SETDROPPEDWIDTH, W + 4, 0);
> >     finally
> >       ReleaseDC(Handle, DC);
> >     end;
> >   end;
> > end;

> > "Ken Phipps" <kphipp...@netscape.net> schreef in bericht
> > news:9214issgnc03j9duu7f21joe111usik39o@4ax.com...
> > > Does anyone know where to get a ComboBox that will autoexpand the
> > > width of the drop-down list for longer strings?

Other Threads