delete objectS at runtime :-| - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

delete objectS at runtime :-|

porodoroporodoro Posts: 230Member
[b][red]This message was edited by porodoro at 2006-1-1 22:18:11[/red][/b][hr]
[b][red]This message was edited by porodoro at 2006-1-1 22:17:40[/red][/b][hr]
[b][red]This message was edited by porodoro at 2006-1-1 22:15:5[/red][/b][hr]
Heres what i got :

1 form with objects in it (labels , editboxes etc..)

2 buttons (Delete & Load)

1 combobox
the goal :
I want to be able to delete a component at runtime
using a name listed on combobox.

Here's the code :

Load button (no problem here) :

COMBOBOX1.Items.Clear;
COMBOBOX1.Clear;
for i := 0 to ComponentCount-1 do
begin
//i need these 2 buttons,so dont add them
// on delete list
// btn1 :get components list
//btn2 :delete combobox index sel item
IF not (Components[i].Name ='Button1')
and NOT (Components[i].Name ='Button2')
// and ofcourse the box!
and NOT (Components[i].Name ='ComboBox1')
then BEGIN
//get all runtime objects
combobox1.Items.Add(
Components[i].Name
);
COMBOBOX1.ItemIndex:=0;
//
end;
//
end;

----------------------------------
The delete button (it doesnt work):
with combobox1.Text as TComponentName do
begin
destroy;
end;
------------------------------------

Whats wrong with delete button code?






Comments

  • zibadianzibadian Posts: 6,349Member
    : [b][red]This message was edited by porodoro at 2006-1-1 22:18:11[/red][/b][hr]
    : [b][red]This message was edited by porodoro at 2006-1-1 22:17:40[/red][/b][hr]
    : [b][red]This message was edited by porodoro at 2006-1-1 22:15:5[/red][/b][hr]
    : Heres what i got :
    :
    : 1 form with objects in it (labels , editboxes etc..)
    :
    : 2 buttons (Delete & Load)
    :
    : 1 combobox
    : the goal :
    : I want to be able to delete a component at runtime
    : using a name listed on combobox.
    :
    : Here's the code :
    :
    : Load button (no problem here) :
    :
    : COMBOBOX1.Items.Clear;
    : COMBOBOX1.Clear;
    : for i := 0 to ComponentCount-1 do
    : begin
    : //i need these 2 buttons,so dont add them
    : // on delete list
    : // btn1 :get components list
    : //btn2 :delete combobox index sel item
    : IF not (Components[i].Name ='Button1')
    : and NOT (Components[i].Name ='Button2')
    : // and ofcourse the box!
    : and NOT (Components[i].Name ='ComboBox1')
    : then BEGIN
    : //get all runtime objects
    : combobox1.Items.Add(
    : Components[i].Name
    : );
    : COMBOBOX1.ItemIndex:=0;
    : //
    : end;
    : //
    : end;
    :
    : ----------------------------------
    : The delete button (it doesnt work):
    : with combobox1.Text as TComponentName do
    : begin
    : destroy;
    : end;
    : ------------------------------------
    :
    : Whats wrong with delete button code?
    :
    :
    The Text property is a string and not an object. The As-operator only works on objects. You need to use FindComponent() to get a component if you know the name.
  • jobromediajobromedia Posts: 168Member
    [b][red]This message was edited by jobromedia at 2006-1-2 0:34:28[/red][/b][hr]
    : : ----------------------------------
    : : The delete button (it doesnt work):
    : : with combobox1.Text as TComponentName do
    : : begin
    : : destroy;
    : : end;
    : : ------------------------------------
    : :
    : : Whats wrong with delete button code?
    : :
    : :
    : The Text property is a string and not an object. The As-operator only works on objects. You need to use FindComponent() to get a component if you know the name.
    :
    You need to specify what item to delete, and the easiest way to do this is:

    [code]
    [b]procedure[/b] TForm1.Button1Click(Sender: TObject);
    [b]begin[/b]
    combobox1.Items.Delete([blue]0[/blue]);
    combobox1.ItemIndex := [blue]0[/blue];
    [b]end[/b];
    [/code]
    Note that the above code will cause an out of list if you try to delete a none existing component, so add this routine to prevent that from happening.

    [code]
    [b]procedure[/b] TForm1.Button1Click(Sender: TObject);
    [b]begin[/b]
    [b]if[/b] combobox1.Items.Count <> [blue]0[/blue] [b]then[/b]
    [b]begin[/b]
    combobox1.Items.Delete([blue]0[/blue]);
    combobox1.ItemIndex := [blue]0[/blue];
    [b]end[/b];
    [b]end[/b];
    [/code]
    Still there is more safety issues that could be implemented. For instance setting the itemindex property should be in a separate if ... then block

    [code]
    [b]procedure[/b] TForm1.Button1Click(Sender: TObject);
    [b]begin[/b]
    [b]if[/b] combobox1.Items.Count <> [blue]0[/blue] [b]then[/b]
    [b]begin[/b]
    combobox1.Items.Delete([blue]0[/blue]);
    [b]end[/b];
    [b]if[/b] combobox1.Items.Count <> [blue]0[/blue] [b]then[/b]
    [b]begin[/b]
    combobox1.ItemIndex := [blue]0[/blue];
    [b]end[/b];
    [b]end[/b];
    [/code]
  • porodoroporodoro Posts: 230Member
    [b][red]This message was edited by porodoro at 2006-1-2 5:47:24[/red][/b][hr]
    @Zibadian :
    Thanks for one more time..

    Thats the code :

    FindComponent(COMBOBOX1.Text).Destroy;
    //since the object is deleted we dont need it..
    combobox1.Items.Delete(combobox1.ItemIndex);
    combobox1.itemIndex := 0;



Sign In or Register to comment.