Recent

Author Topic: select item in TComboBoxEx  (Read 2006 times)

RainerHamann

  • New Member
  • *
  • Posts: 16
select item in TComboBoxEx
« on: October 21, 2021, 06:11:06 pm »
I want to set and select items of TComboBoxEx dependend from other user action like TComboBox.ItemIndex:=... and ShowMessage(TComboBox.Text), but there is nothing like e.g. ItemIndexEx or TextEx to set or read both properties.

I'm using Lazarus 2.2.0RC1 with QT5 property set for my project on Linux Mint.

jamie

  • Hero Member
  • *****
  • Posts: 5020
Re: select item in TComboBoxEx
« Reply #1 on: October 21, 2021, 09:28:31 pm »
I am not sure if I understand you. ?

Do you mean ItemIndexEx[aIndex_value].Caption := 'ijuioiodijoie' or something. ?
The only true wisdom is knowing you know nothing

RainerHamann

  • New Member
  • *
  • Posts: 16
Re: select item in TComboBoxEx
« Reply #2 on: October 21, 2021, 10:35:24 pm »
No, I want to show one line (icon and text) in the extended ComboBox. With a standard ComboBox I would do
Code: Pascal  [Select][+][-]
  1. cb.ItemIndex:=3
and I want to read the text of the by the user selected line like
Code: Pascal  [Select][+][-]
  1. some var := cb.Text
(standard ComboBox)

jamie

  • Hero Member
  • *****
  • Posts: 5020
Re: select item in TComboBoxEx
« Reply #3 on: October 21, 2021, 10:43:53 pm »
No, I want to show one line (icon and text) in the extended ComboBox. With a standard ComboBox I would do
Code: Pascal  [Select][+][-]
  1. cb.ItemIndex:=3
and I want to read the text of the by the user selected line like
Code: Pascal  [Select][+][-]
  1. some var := cb.Text
(standard ComboBox)

Ok, I got you now..

That control has design issues. The normal text has nothing in it..

I did a rework of that control way back but never posted it because I just kept finding more problems.

The Captions of each TItemList is not populated in the normal Tcombobox so things like the auto complete etc do not work.

You may do better if you simply use a normal TComboBox in owner draw mode and do your own drawing instead.
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 9021
Re: select item in TComboBoxEx
« Reply #4 on: October 21, 2021, 10:46:04 pm »
The ItemIndex works for TComboBoxEx in the same way as for TComboBox:
Code: Pascal  [Select][+][-]
  1.   ComboBoxEx1.ItemIndex := 1;  // Select the 2nd item (having index 1)
Retrieving the item caption is a bit more complicated since the property ItemsEx[index] returns the full item instance at the given index. But there's a property Caption for the text of of that item:
Code: Pascal  [Select][+][-]
  1.  ShowMessage(ComboBoxEx1.ItemsEx[CombBoxEx1.ItemIndex].Caption);
Other properties of the ItemsEx[index] are:
- ImageIndex
- OverlayImageIndex
- SelectedImageIndex
- Indent
(Just what is available when you populate the items by the '...' button next to ItemsEx in the object inspector).

Unlike a TCombobox which is populated by calling ComboBox1.Add(text_of_the_item) you populate the TComboBoxEx at runtime  this way:
Code: Pascal  [Select][+][-]
  1. with ComboBoxEx1.ItemsEx.Add do
  2. begin
  3.   Caption := text_of_the_item;
  4.   ImageIndex := 15;
  5.   SelectedImageIndex := 15;
  6. end;
  7.  
  8. // or quicker, if you only want to set the caption
  9. ComboBox1.ItemsEx.Add.Caption := text_of_the_item;

The property Text is not available since the displayed text cannot be edited. I don't know why it is published.
« Last Edit: October 21, 2021, 10:58:19 pm by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

RainerHamann

  • New Member
  • *
  • Posts: 16
Re: select item in TComboBoxEx
« Reply #5 on: October 22, 2021, 10:21:21 am »
Okay, thank you very much!

 

TinyPortal © 2005-2018