Jump to content
Sign in to follow this  
Gustav Schubert

About missing property TComboBox.Text

Recommended Posts

In FMX: Is TComboBox.Text is a missing property?

 

This post is about the best possible way to handle a little problem, with as little change to existing code as possible, just after porting old code from VCL to FMX.

 

Test code to play with:

var
  Counter: Integer = 0;

procedure TForm1.ComboBox1Change(Sender: TObject);
var
  s: string;
begin
//  s := ComboBox1.Text; // does not compile

  { ComboBox1.Selected.Text is what you want, but Selected can be nil! }
  if Combobox1.Selected = nil then
  begin
    Counter := Counter + 1;
    Caption := IntToStr(Counter);
    Exit;
  end;

  s := ComboBox1.Selected.Text;

  { Old code uses string comparision to decide what to do. }
  { ... }
//  Caption := s;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ComboBox1.Items.Text := 'Hello World';
  ComboBox1.ItemIndex := 0;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  SL: TStrings;
begin
  SL := TStringList.Create;
  SL.Add('Foo');
  SL.Add('Bar');
  SL.Add('Vote for RSP-29811');

  ComboBox1.Items := SL;
  ComboBox1.ItemIndex := 2;

  SL.Free;
end;

Consider an existing application that you port from VCL. Not only will you have to change code to make it compile, you will also have to write extra code to keep it from crashing.
 

Share this post


Link to post

I suggest to create a class helper:

type
  TComboBoxHelper = class helper for TComboBox
  private
    function GetText: string;
  public
    property Text: string read GetText;
  end;

function TComboBoxHelper.GetText: string;
begin
  Result := '';
  if Selected <> nil then
    Result := Selected.Text;
end;

 

  • Thanks 1

Share this post


Link to post

The Problem (ComboBox.Selected = nil in OnChange) does not happen the first time you swap the content of Items. It is happening from the second time onwards. Can be shown easily with another test button that calls ComboBox1.Clear. This will reset the test cycle.

TComboBoxHelper works as expected and is 3rd best option for me. Second best option in my ranking is to inherit from TComboBox, preferred because I do create all components at runtime. Best option so far and by far is of course to add the property directly.

No one came out with the idea for 8 years or so. And then it took me two days (too long) after running into the problem, before I refactored and used my own combobox - a little success story. So this is why I made an exception to the rule and went on record with a feature request for something otherwise very simple.

 

We are now up to two votes. A big number for me, a small step for Delphi. Thanks to Emba for opening the 'new feature' request. Let's see how it goes. 👈


 

Share this post


Link to post
17 hours ago, Gustav Schubert said:

The Problem (ComboBox.Selected = nil in OnChange) does not happen the first time you swap the content of Items.

It can be confusing, so I double-checked:
ComboBox.Clear does not trigger OnChange
ComboBox.Items.Clear will trigger OnChange (with Selected = nil)
It is after calling ComboBox.Clear that you can observe the first-attempt-anomaly.

 

Assigning to Items will involve Items.Clear, and the Selected = nil problem, which the feature request is aiming to deal with in a better way.

 

The fact that OnChange is not called after Combo.Clear may be a problem for the logic of your program.

 

But that does not invalidate the feature request. The attached form contains the updated test code.

 

FrmMain.zip

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×