Recent

Author Topic: CheckComboBox Item Width  (Read 660 times)

seany

  • Jr. Member
  • **
  • Posts: 99
CheckComboBox Item Width
« on: January 31, 2023, 04:21:37 am »
When you click on a CheckComboBox, it creates a dropdown list.
The Dropdown list has the same width as the CheckCombobox itself, even if there are items where the textwidth is much higher.

How can we ensure that the dropdown list automatically expand to show the full text width of items? Thank you.


KodeZwerg

  • Hero Member
  • *****
  • Posts: 1180
  • Fifty shades of code.
    • Delphi & FreePascal
Re: CheckComboBox Item Width
« Reply #1 on: January 31, 2023, 04:29:31 am »
Since you give no clue about your target OS nor your FPC/Lazarus versions, here is my answer for Windows OS written in Delphi, but logic is same.
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

seany

  • Jr. Member
  • **
  • Posts: 99
Re: CheckComboBox Item Width
« Reply #2 on: January 31, 2023, 04:46:37 am »
My bad

Lazarus 2.2.4
FPC : 3.2.2

Linux x86_64 QT5

Joanna

  • Sr. Member
  • ****
  • Posts: 295
Re: CheckComboBox Item Width
« Reply #3 on: March 23, 2023, 10:52:23 pm »
You will need to constrain the width of the checkcombobox to fit the widest string. I’m usually in irc. If you wish to discuss further.
Come chat on IRC .. https://libera.chat/guides/
IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channels  #fpc #lazarus #pascal 
Private Message me if you want to be added to invite list, otherwise type /msg nickserv help register to get started

paweld

  • Hero Member
  • *****
  • Posts: 626
Re: CheckComboBox Item Width
« Reply #4 on: March 24, 2023, 07:50:52 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   i, w, max: Integer;
  4. begin
  5.   max := ComboBox1.Width;
  6.   for i := 0 to ComboBox1.Items.Count - 1 do
  7.   begin
  8.     w := ComboBox1.Canvas.TextWidth(ComboBox1.Items[i]);
  9.     if w > max then
  10.       max := w + 10;
  11.   end;
  12.   ComboBox1.ItemWidth := max;
  13. end;
Best regards / Pozdrawiam
paweld

paweld

  • Hero Member
  • *****
  • Posts: 626
Re: CheckComboBox Item Width
« Reply #5 on: March 24, 2023, 10:12:19 am »
and for CheckComboBox: 
Code: Pascal  [Select][+][-]
  1. uses
  2.   LCLIntf, LCLType;
  3.  
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. var
  6.   i, w, max: Integer;
  7. begin
  8.   max := CheckComboBox1.Width;
  9.   for i := 0 to CheckComboBox1.Items.Count - 1 do
  10.   begin
  11.     w := CheckComboBox1.Canvas.TextWidth(CheckComboBox1.Items[i]);
  12.     if w > max then
  13.       max := w + 10 + GetSystemMetrics(SM_CXMENUCHECK) {check width};
  14.   end;
  15.   CheckComboBox1.ItemWidth := max;
  16. end;    
  17.  
Best regards / Pozdrawiam
paweld

Joanna

  • Sr. Member
  • ****
  • Posts: 295
Re: CheckComboBox Item Width
« Reply #6 on: March 24, 2023, 12:26:25 pm »
Hi paweld  :D
Today  I made an interesting discovery about checkcombobox...

if you add strings to the checkcombobox items it will compile just fine but cause an amazing crash in the ondrawitem event because the Items.objects[index] part which represents the checkboxes? apparently nil.

The only way I know of to add things to checkcombobox is to use the additem procedure. Which adds things one by one. I’ll definitely need to improve upon that.

Another thing I’m unsure about is how checkboxes are enabled and disabled and how to control color of checkboxes
« Last Edit: March 24, 2023, 12:51:30 pm by Joanna »
Come chat on IRC .. https://libera.chat/guides/
IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channels  #fpc #lazarus #pascal 
Private Message me if you want to be added to invite list, otherwise type /msg nickserv help register to get started

jamie

  • Hero Member
  • *****
  • Posts: 5167
Re: CheckComboBox Item Width
« Reply #7 on: March 24, 2023, 10:42:29 pm »
Yes, we must have PINK check boxes.
 :o
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018