Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Simple question

Posted on 2002-03-24
12
Medium Priority
?
195 Views
Last Modified: 2010-04-04
Hi there,

How can I create objects (say buttons) at runtime? Normally, we create buttons by this syntax:
NEWBTN:=TButton.Create(Self);

But now I want the buttons to be created only when the user requests, and the names of the buttons are specified by the user, say through Edit1.Text

Two problems:
1. How can I create a button with the name given by the user?
2. Later, the user will give another name in Edit2.Text, how can I search whether there is a button with the name matched with Edit2.Text? If that button exists, how can I delete it?
0
Comment
Question by:nyt
[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
  • 3
  • 3
  • 3
  • +3
12 Comments
 
LVL 6

Accepted Solution

by:
DrDelphi earned 200 total points
ID: 6893327
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    BtnArray:array of Tbutton;
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetLength(btnarray,length(btnArray)+1);
  btnarray[length(btnarray)-1]:=Tbutton.create(self);
  with btnarray[length(btnarray)-1]do
  begin
    Top:=10;
    Caption:='DrDelphi';
    OnClick:=ClickHandler; //event handler
    Parent:=self;///very important to see the button!!
    name:='Button3';
  end;


  end;

procedure TForm1.Button2Click(Sender: TObject);
var But:Tcomponent;
begin
   but:=FindComponent('Button3');
   if but<>nil then FreeandNil(but);
end;

end.




Good luck!!
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6893330
You will have to set the Parent of the button or it will not show up.
You can simply assign the Name property of the button like any other property.
Form.Components and Form.ComponentCount allows to iterate all components on the Form.
0
 

Expert Comment

by:lightblue
ID: 6893338
about the second problem:

...
var
  I: Integer;
  Temp: TComponent;
begin
  for I := Form1.ComponentCount - 1 downto 0 do
  begin
    Temp := Form1.Components[I];
    if Temp is TButton then
    if Temp.Name = Edit2.Text then
    begin
      TButton(temp).Free;
    end;
  end;
end;
...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:DrDelphi
ID: 6893353
I addressed both the parent issue and the freeing of the button in my code. Plerase try it out and see if it is not what you're looking to do.


0
 
LVL 9

Expert Comment

by:ginsonic
ID: 6893359
var UserChoice:string;
......
NEWBTN:=TButton.Create(Self);
NEWBTN.Caption:=UserChoice;
.....


procedure TForm1.Button1Click(Sender: TObject);
var
 i:integer;
 Component : TComponent;
 z:string;
begin
i:=0;
while i<ComponentCount do
  begin
    Component := Components[i];
    if (Component is TButton) then begin
     if ((Component as TButton).Caption='Button2') then
      begin
        (Component as TButton).Free;
        i:=ComponentCount;
        end
       else
         inc(i);
      end
     else
       inc(i);
  end;
end;
0
 

Author Comment

by:nyt
ID: 6893483
DrDelphi,
Do you write your code in D5 or D6? My D4 can't compile them.
Undeclared identifier: 'ClickHandler'
Undeclared identifier: 'FreeandNil'
0
 

Author Comment

by:nyt
ID: 6893489
DrDelphi,
Do you write your code in D5 or D6? My D4 can't compile them.
Undeclared identifier: 'ClickHandler'
Undeclared identifier: 'FreeandNil'
0
 

Author Comment

by:nyt
ID: 6893502
Well...after seeing all the comments, I finally solved the problem.

Here is a new problem: how can I add something to the onClick event (and other events) of those newly created buttons?  Say, I want ShowMessage('testing') when the buttons are clicked.

More points will be added according to the complexity of the answers.
0
 
LVL 1

Expert Comment

by:slavikn
ID: 6893513
That's what DrDelphi wrote:

with btnarray[length(btnarray)-1]do
 begin
   Top:=10;
   Caption:='DrDelphi';
   OnClick:=ClickHandler; //event handler
   Parent:=self;///very important to see the button!!
   name:='Button3';
 end;

You should write this code:

with btnarray[length(btnarray)-1]do
 begin
   Top:=10;
   Caption:='DrDelphi';
   OnClick:=ClickHandler; //event handler
   Parent:=self;///very important to see the button!!
   name:='Button3';
   OnClick:=MyBtnClick;   { !!!!!! }
 end;

procedure MyBtnClick(Sender As TButton);
begin
 showmessage('this is a test');
end;
0
 
LVL 1

Expert Comment

by:slavikn
ID: 6893515
Sorry,

procedure MyBtnClick(Sender As TButton);
  should be
procedure MyBtnClick(Sender: TButton);
0
 
LVL 1

Expert Comment

by:slavikn
ID: 6893521
I mean that
   OnClick:=MyBtnClick;
      comes instead of
   OnClick:=ClickHandler;

So the final answer is:

with btnarray[length(btnarray)-1]do
begin
  Top:=10;
  Caption:='DrDelphi';
  Parent:=self;
  name:='Button3';
  OnClick:=MyBtnClick;
end;

procedure MyBtnClick(Sender As TButton);
begin
  showmessage('this is a test');
end;
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6894179
The assumption was that ClickHandler is a procedure to handle the OnClick event of the button. As for FreeAndNil, that came into being in Delphi 5... for D4, use Free and then Obj:=nil (whre obj is a pointer to the button).

Good luck!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

604 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