Jump to content
Henry Olive

Combobox1.Items.AddObject(....)

Recommended Posts

Good Day,

 

I add Customer No & Customer Name in a combobox like below

Combobox1.Items.AddObject(SQLQuery1.Fields[1].asString,  Pointer(SQLQuery1.Fields[1].asInteger));

I want to make CutNo=1  record in the combobox selected  (I mean, combobox1.text = CustNo=1 record's Customer Name)

but i get,   Identifier expected but 'OBJECT' found error msg.
Combobox1.ItemIndex := Integer(Combobox1.Items.Object[Combobox1.ItemIndex]);

Could someone please help

Thank You


 

Edited by Henry Olive

Share this post


Link to post

I think this is a very bad idea. You code doesn't compiles as you're missing a letter s, the property nam is objects:

 

 Combobox1.ItemIndex := Integer(Combobox1.Items.Objects[Combobox1.ItemIndex]);

Share this post


Link to post
1 hour ago, Henry Olive said:

Combobox1.ItemIndex := Integer(Combobox1.Items.Object[Combobox1.ItemIndex]);

 

43 minutes ago, Lajos Juhász said:

I think this is a very bad idea. You code doesn't compiles as you're missing a letter s, the property nam is objects:

Combobox1.ItemIndex := Integer(Combobox1.Items.Objects[Combobox1.ItemIndex]); 

 

 

May be you mean:

 

var tempointer: Pointer;
tempointer := Combobox1.Items.Objects[Combobox1.ItemIndex];
Combobox1.ItemIndex := Integer(tempointer^);

 But even if this is correct, the starting point is incorrect:

 

1 hour ago, Henry Olive said:

Combobox1.Items.AddObject(SQLQuery1.Fields[1].asString,  Pointer(SQLQuery1.Fields[1].asInteger));

PAY ATTENTION: SqlQuery1 I think dynamically allocates Fields[1] and therefore your POINTER will be invalid after a new QUERY is executed.

The pointer used with the AddObjects function should be permanent and "last" for the entire existence of the program (until you know what you are doing :classic_biggrin:)
 

Bye

Edited by DelphiUdIT

Share this post


Link to post
2 hours ago, DelphiUdIT said:

PAY ATTENTION: SqlQuery1 I think dynamically allocates Fields[1] and therefore your POINTER will be invalid after a new QUERY is executed.

You should read the code: SQLQuery1.Fields[1].asInteger integer value of the field converted to a pointer.

 

The problem here is that the id is not going to be a continous enumeration starting from 0.

  • Thanks 1

Share this post


Link to post

try some like this:

  • NOTE:   TObject( 0 ) = NIL
type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    FDMemTable1: TFDMemTable;
    FDMemTable1MyID: TIntegerField;
    FDMemTable1MyText: TStringField;
    procedure Button1Click(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  // 0..10
  // for var i: integer := 0 to 10 do
  // ListBox1.AddItem('Item' + i.ToString, TObject(i));
  //
  // FDMemTable
  FDMemTable1.First;
  //
  while not FDMemTable1.Eof do
    begin
      ListBox1.AddItem(FDMemTable1MyText.AsString, TObject(FDMemTable1MyID.AsInteger)); // Warning: TObject( 0 ) = nil
      //
      FDMemTable1.Next;
    end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  i: integer;
begin
  { 0..10
    i := (ListBox1.Count - 1);
    //
    if (i < 0) then
    exit;
    //
    i := random(i);
    //
    ListBox1.ItemIndex := integer(ListBox1.Items.Objects[i]);
  }
  // -------------------
  // FDMemTable
  i := (ListBox1.Items.Count - 1);
  i := random(i);
  //
  Memo1.Lines.Add(i.ToString);
  //
  if (ListBox1.Items.Objects[i] = nil) then // ...Objects[ 0 ] = nil
    begin
      Memo1.Lines.Add('nil');
      i := -1;  //  == "0"
    end
  else
    i := ListBox1.Items.IndexOfObject(ListBox1.Items.Objects[i]);
  //
  ListBox1.ItemIndex := i;
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
  if (ListBox1.ItemIndex > -1) then
    Caption := integer(ListBox1.Items.Objects[ListBox1.ItemIndex]).ToString;
end;

initialization

ReportMemoryLeaksOnShutdown := true;

end.

 

  

prjVCL_TObject_TListBox_IndexOfObject_find_integer_ID_on_Items_Objects_ykjP55UAjC.gif    image.thumb.png.342c3d3207137743c1d9a8c6bccace4e.png

Edited by programmerdelphi2k

Share this post


Link to post
procedure TForm1.Button3Click(Sender: TObject);
var
  x: TObject;
begin
  x := TObject(0); //1...
  //
  if x = nil then
    ShowMessage('nil')
  else
    ShowMessage('not nil');
end;

 

Share this post


Link to post
9 hours ago, Henry Olive said:

Good Day,

 

I add Customer No & Customer Name in a combobox like below

Combobox1.Items.AddObject(SQLQuery1.Fields[1].asString,  Pointer(SQLQuery1.Fields[1].asInteger));
 

 

Since the same field value (a numerical string) is being used in both places, all this code is doing is saving you a StrToInt conversion later one, and it's not necessary.

 

It makes me think ... who in their right mind would put a bunch of numbers into a ComboBox? I'm guessing you have oversimplified your example. But perhaps not.

 

Assuming not, and given that the ComboBox just has a bunch of numerical strings in it, you just need to say, "Combobox1.Text.AsInteger" to get its numerical VALUE ... when you need it.


Why waste memory by doing the conversion earlier and sticking it into the Obect's array?

 

Besides, the values in Objects are NOT Pointers, they're references to TObject instances. So you should cast them to TObjects, not Pointers. That still doesn't make much sense to me.

 

Doesn't the basic TComboBox allow Names and Values to be specified? Or is that just the Raize version? Because that's what I'd use. (If not, then I'd use name=value pairs and a custom OnShow method to only display the Name part and then extract the Value part in the OnSelect handler.)
 

Edited by David Schwartz

Share this post


Link to post

Thank you so much Lajos, DelphiUdit, Programmer, David

 

David,  Combobox1.Text.AsInteger doesnt compile in D10.3

Share this post


Link to post
On 3/21/2023 at 4:12 AM, Henry Olive said:

 

David,  Combobox1.Text.AsInteger doesnt compile in D10.3

As Stano said ... there's always StrToInt( Combobox1.Text )

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

×