Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

tObjComboBox - How to create and use

Posted on 1999-06-23
2
Medium Priority
?
178 Views
Last Modified: 2010-04-06
//I want to create a combobox tObjComboBox which will hold a list of the
//names of each object in a list of objects as well as the list of objects, itself (wow!).
//For this purpose I have descended the component listed below from tComboBox:



unit ObjComboBox;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;
type
  TObjComboBox = class(TComboBox)
  private
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }
  published
    objects : tList;
    { Published declarations }
  end;
procedure Register;
implementation
procedure Register;
begin
  RegisterComponents('CWE', [TObjComboBox]);
end;
end.




//When I try to add the names of the objects to the items
//property, no problem.  However, when I try to add the objects,
//themselves, to the tList, objects,  I get an access violation.
//Continuing execution, I find that only the first object in the
//array was instantiated and its name added to the tObjCombobox.
//I know I am doing something wrong in the way I am getting the
//pointer to each object, but I am not sure what.  Any ideas?


unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ObjComboBox;
type
  TForm1 = class(TForm)
    ObjComboBox1: TObjComboBox;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.DFM}
type tMyObject = class(tObject)
  name : string;
end; //type tMyObjec
var
  obj : array [0..5] of tMyObject;
procedure TForm1.FormCreate(Sender: TObject);
var   i : integer;
begin
  for i:=0 to 5 do
    begin
      obj[i]:=tMyObject.Create;
      obj[i].name :='Crusty' + IntToStr(i);
      ObjComboBox1.Items.Add(obj[i].name);
      ObjComboBox1.Objects.Add(Pointer(obj[i]));
      obj[i].destroy; //don't actually destroy, here.  Do it
                      //after the objects are no longer useful.
    end;
end;
end.
0
Comment
Question by:cestes001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
Felixin earned 60 total points
ID: 1384285
Hi cestes001,

If the code you posted is the one you're trying to compile:

should it be

ObjComboBox1.ITEMS.Objects.Add(Pointer(obj[i]));

instead of

ObjComboBox1.Objects.Add(Pointer(obj[i])); ???

On the other hand, forget about the Pointer conversion:

It should be

ObjComboBox1.ITEMS.Objects.Add(obj[i]);

I would recomend you usinf the AddObject procedure:

function AddObject(const S: string; AObject: TObject): Integer; virtual; (from TStrings -> Items in the TComboBox)

For you would be:

ObjComboBox1.ITEMS.AddObject (obj[i].name, obj[i]);


Hope this will help you.

Greetings.
Felixin
0
 

Author Comment

by:cestes001
ID: 1384286
Felixin:

yes, the last proposal was the magic one.  In the original code, I tried to just add the object, but the complier balked about not passing a pointer (which is what I thought I was passing when
ObjComboBox1.Objects.Add(obj[i]);

Anyway, thanx.  That was it.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question