Skip to content

New to c# - how to create components, on the fly ??

Jan Lauenborg

Asked Aug 15, 2007, 2:46 AM EDT2 answers
Share
LinkedInX

Hi all, I am new to c#, comming from Delphi, I would like to ask if any of you guys out there can tell me how to create components on a form, on the fly.

I have some Delphi examples to show what I want c# to do.

First of all - creating a component on the fly, in the Delphi exampel I create a Panel and setup a few property, the most important is the component name, so I can find the component again when I want to change the property or free it. Also the parent and the owner is important to be able to display the component.

procedure TForm1.Button1Click(Sender: TObject);
begin
  With Tpanel.Create(Form1) do
  begin
    Parent:=Form1;
    Name:='Mypanel1';
    Caption:='My Cap';
    top:=20;
    Left:=20;
    Height:=100;
    Width:=100;
  end;
end;

The next problem is the way to find and use the component, the next to small procedures show what I mean..

The first one find component and use type cast to change the property..

procedure TForm1.Button2Click(Sender: TObject);
var
  P: TComponent;
begin
  P:=FindComponent('MyPanel1');
  if P<>nil then
  begin
    with TPanel(P) do
    begin
      top:=30;
      Left:=30;
      Height:=80;
      Width:=80;
      Caption:='New Cap';
    end;
  end;
end;

The last one does the same, but free the component from the memory..

procedure TForm1.Button3Click(Sender: TObject);
var
  P: TComponent;
begin
  P:=FindComponent('MyPanel1');
  if P<>nil then
  begin
    Tpanel(P).Free;
  end;
end;

It would make me very happy if some one could help me getting on with c#, thanks in advance.

Regards..

Jan

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    Hi Jan,

    Heres C# translation for the first 2 things using a TextBox control. In C# there is no need to destroy the component because of the GC.

    TypeScript
    private void button1_Click(object sender, EventArgs e)
    {
    TextBox t = new TextBox(); // creation
    // Note no with do construct in C#
    t.Name = "MyTextBox";
    t.Location = new Point(10, 10);
    t.Size = new Size(100, 40);
    t.Text = "Hello World";

    this.Controls.Add(t); // Equivilent to setting parent property

    TypeScript
    }
    private void button2_Click(object sender, EventArgs e)
    {
    /* The Find method return an array of all the controls with the Name provided. If the 2nd parameter is true then  all the child controls of each of the form's controls (and so on) are also searched */
    Control[] c = this.Controls.Find("MyTextBox", true);
    
    TextBox t = (TextBox)c[0];
    /* The down-cast isn't strictly necessary for the properties required but I've included it since you did*/
    t.Location = new Point(100, 100);
    t.Size = new Size(200, 80);
    t.Text = "Changed";
     }

    Hope this helps,

    Sam

    0
  • 0

    Thank You very much Sam, that helped for sure. :-)

    0