Board index » delphi » How to create an TButton and assign its name to the contents of an Edit Box

How to create an TButton and assign its name to the contents of an Edit Box

How do i create an TButton and assign its name to the contents of an Edit
Box

e.g editbox1 contains word "but1" how do i

var
EditBox1.Text: TButton;
begin
EditBox1.Text := TButton1.Create;

Thanks

 

Re:How to create an TButton and assign its name to the contents of an Edit Box


Ian -

Quote

>var
>EditBox1.Text: TButton;
>begin
>EditBox1.Text := TButton1.Create;

well, I am not sure what you are trying to do, but this will get the
job done --

var
  MyButton: TButton;
begin
  MyButton := TButton.Create(Self);
  Edit1.Text := MyButton.Caption;
...

end;

Nick Hodges - TeamB
Search the newsgroups --
http://www.xapxone.com/html/xaptips_search.htm

Re:How to create an TButton and assign its name to the contents of an Edit Box


Quote
Ian Groves <i...@igroves.f9.co.uk> wrote in message news:3a5ba021_1@dnews...
> How do i create an TButton and assign its name to the contents of an Edit
> Box

Edit1.Text := Button1.Name;??

--
Breakthrough: It finally booted on the first try.
Mines are equal opportunity weapons.
ICQ: 91418899
http://www.planeted.co.uk
http://members.nbci.com/edduffe>

Re:How to create an TButton and assign its name to the contents of an Edit Box


I need to be able to create buttons at runtime, using name specified by me,
i will type the name i want to call the button and the want the app to
create the button and call it by that name, sorry if still does not help.

Thanks again

Ian Groves.

"Nick Hodges (TeamB)" <nickhod...@yahoo.com> wrote in message
news:o3rn5ts5t5kedc5vviok5gt64d70veauvt@4ax.com...

Quote
> Ian -

> >var
> >EditBox1.Text: TButton;
> >begin
> >EditBox1.Text := TButton1.Create;

> well, I am not sure what you are trying to do, but this will get the
> job done --

> var
>   MyButton: TButton;
> begin
>   MyButton := TButton.Create(Self);
>   Edit1.Text := MyButton.Caption;
> ...

> end;

> Nick Hodges - TeamB
> Search the newsgroups --
> http://www.xapxone.com/html/xaptips_search.htm

Re:How to create an TButton and assign its name to the contents of an Edit Box


Quote
Ian Groves wrote:
> I need to be able to create buttons at runtime, using name specified by me,
> i will type the name i want to call the button and the want the app to
> create the button and call it by that name, sorry if still does not help.

I'm afraid you cant do this!  And I think you may possibly be going about this
the wrong way.  Names are created at design time by Delphi (when you type in
the object inspector) and cannot be changed at runtime.  This makes sense since
you can only reference named variables that you have specifically coded for in
design time.

However there are plenty of ways of doing the sort of thing you want.  If you
wish to be able to assign a name to a button, and later refer back to the
button by that name, use a TStringlist and its Objects property.

eg.

var
    MyButtons:     TStringlist;

Procedure TForm1.AddButton(ButtonName:    String);
var
        TempButton:    TStringlist;
begin
    TempButton:=    TButton.Create( Self);
    TempButton.Parent:=    Self;  //This will make the button appear on the
form.
    Tempbutton.Visible:=     True;

    //Add to our stringlist, so we can find it later.
    MyButtons.AddObject( ButtonName, TempButton);

end;

Call Addbutton each time you want to add a button, and then use a function like
this to get the button.

Function TForm1.FindButton(ButtonName: String): TButton;
var
    Index:    Integer;
begin
    Index:=    MyButtons.IndexOf(ButtonName);  //Case Sensitive
    if Index > -1 then
    begin
        Result:=    TButton( MyButtons.Objects[ Index]);
    end;
end;

Then you can write something like this.

AddButton( Edit1.Text);

btn:=    FindButton(Edit1.Text);

btn.Caption:=    'This button has been found';

Hope this does what your looking for.

Toby Allen

Re:How to create an TButton and assign its name to the contents of an Edit Box


On Wed, 10 Jan 2001 09:41:06 -0000, "Ian Groves"

Quote
<i...@igroves.f9.co.uk> wrote:
>I need to be able to create buttons at runtime, using name specified by me,
>i will type the name i want to call the button and the want the app to
>create the button and call it by that name, sorry if still does not help.

Maybe this is what you want?

var MyButton: TButton;
begin
  MyButton := TButton.Create(Self);
  MyButton.Parent := Form1;
  MyButton.Caption := Edit1.Text;
...
end;

ain

Re:How to create an TButton and assign its name to the contents of an Edit Box


Well... for me that's the way I do it:

var
    EditBox1: TEditBox;
    MyButton: TButton;

begin
      MyButton := TButton1.Create(self);
      with MyButton do begin
            Caption := 'Click Me!';
            Name := EditBox1.Text; // Make sure that the EditBox.Text
contents had no  spaces..
            OnClick := MyButtonOnClieckEvent;
            ... // You can set additionnal properties here...
      end;
end;

procedure MyButtonOnClickEvent(Sender: TObject);
begin
    showmessage(Format('You have clicked the button %s',[(Sender as
TButton).Name]));
end;
end;

Quote
"Ian Groves" <i...@igroves.f9.co.uk> wrote in message

news:3a5c2e39_2@dnews...
Quote
> I need to be able to create buttons at runtime, using name specified by
me,
> i will type the name i want to call the button and the want the app to
> create the button and call it by that name, sorry if still does not help.

> Thanks again

> Ian Groves.

> "Nick Hodges (TeamB)" <nickhod...@yahoo.com> wrote in message
> news:o3rn5ts5t5kedc5vviok5gt64d70veauvt@4ax.com...
> > Ian -

> > >var
> > >EditBox1.Text: TButton;
> > >begin
> > >EditBox1.Text := TButton1.Create;

> > well, I am not sure what you are trying to do, but this will get the
> > job done --

> > var
> >   MyButton: TButton;
> > begin
> >   MyButton := TButton.Create(Self);
> >   Edit1.Text := MyButton.Caption;
> > ...

> > end;

> > Nick Hodges - TeamB
> > Search the newsgroups --
> > http://www.xapxone.com/html/xaptips_search.htm

Re:How to create an TButton and assign its name to the contents of an Edit Box


I forgot to mention that you can acces your button like code like that:

(FindComponent(EditBox1.Text) as TButton).Caption := 'New Caption';

Quote
"Mike" <mho...@nordexonline.com> wrote in message

news:93icq4$nvb9@bornews.inprise.com...
Quote
> Well... for me that's the way I do it:

> var
>     EditBox1: TEditBox;
>     MyButton: TButton;

> begin
>       MyButton := TButton1.Create(self);
>       with MyButton do begin
>             Caption := 'Click Me!';
>             Name := EditBox1.Text; // Make sure that the EditBox.Text
> contents had no  spaces..
>             OnClick := MyButtonOnClieckEvent;
>             ... // You can set additionnal properties here...
>       end;
> end;

> procedure MyButtonOnClickEvent(Sender: TObject);
> begin
>     showmessage(Format('You have clicked the button %s',[(Sender as
> TButton).Name]));
> end;
> end;

> "Ian Groves" <i...@igroves.f9.co.uk> wrote in message
> news:3a5c2e39_2@dnews...
> > I need to be able to create buttons at runtime, using name specified by
> me,
> > i will type the name i want to call the button and the want the app to
> > create the button and call it by that name, sorry if still does not
help.

> > Thanks again

> > Ian Groves.

> > "Nick Hodges (TeamB)" <nickhod...@yahoo.com> wrote in message
> > news:o3rn5ts5t5kedc5vviok5gt64d70veauvt@4ax.com...
> > > Ian -

> > > >var
> > > >EditBox1.Text: TButton;
> > > >begin
> > > >EditBox1.Text := TButton1.Create;

> > > well, I am not sure what you are trying to do, but this will get the
> > > job done --

> > > var
> > >   MyButton: TButton;
> > > begin
> > >   MyButton := TButton.Create(Self);
> > >   Edit1.Text := MyButton.Caption;
> > > ...

> > > end;

> > > Nick Hodges - TeamB
> > > Search the newsgroups --
> > > http://www.xapxone.com/html/xaptips_search.htm

Re:How to create an TButton and assign its name to the contents of an Edit Box


Ian --

Have the other folks helped you out, or do you have more questions.

Nick Hodges - TeamB
Search the newsgroups --
http://www.xapxone.com/html/xaptips_search.htm

Re:How to create an TButton and assign its name to the contents of an Edit Box


Thanks that is what i was after, the second post from Mike,
Thanks all.

It is really very simple, not sure how i missed that ;?)

Ian Groves.

"Nick Hodges (TeamB)" <nickhod...@yahoo.com> wrote in message
news:q5rp5tgl84alo59eh3tmcbnbe223esa9e2@4ax.com...

Quote
> Ian --

> Have the other folks helped you out, or do you have more questions.

> Nick Hodges - TeamB
> Search the newsgroups --
> http://www.xapxone.com/html/xaptips_search.htm

Other Threads