-2

I am trying to make custom forms with unique sets of controls on each form... I can create the dynamic form, but I can't seem to put any controls on it..

using (Form formA = new Form())
{
  Button btn = new Button();
  formA.Text = "Form A";
  formA.Name = "FormA";
  this.MaximizeBox = false;
  this.MinimizeBox = false;
  this.BackColor = Color.White;
  this.ForeColor = Color.Black;
  this.Size = new System.Drawing.Size(155, 265);
  this.Text = "Run-time Controls";
  this.FormBorderStyle = FormBorderStyle.FixedDialog;
  this.StartPosition = FormStartPosition.CenterScreen;
  formA.Show();
  formA.Controls.Add(btn);
}

The form creates ok, but no luck on the buttons... (I edited the code displayed here, to make it easier to see what I am trying to do, but my form still destroys itself as soon as it is created. I have no idea why.

Tushar
  • 481
  • 6
  • 26
  • 1
    How can you tell? The form is disposed of immediately after adding *one* button and after the form was shown. – Ňɏssa Pøngjǣrdenlarp Jun 12 '19 at 01:37
  • What am I doing wrong? I changed the formA.ShowDialog() to formA.Show(), and now I see the form destructing immediately, but I have no Idea why that is happening... BTW I would like to thank everyone who took the time to point out how horrible I am at this... Thank you for your support. – william_perry Jun 12 '19 at 02:53
  • You are disposing the form with the **using()** function. Whatever resource is occupied into the **using()** enclose will be disposed as soon as it finises its enclosed statements. – Engr Syed Rowshan Ali Jun 12 '19 at 03:08
  • I have it now. Thank you for your help, Engr Syed Rowshan Ali. – william_perry Jun 12 '19 at 03:14
  • Never use a piece of code that you dont understand how it works. If you dont know what `using` does, look it up, the documentation is online and free. – Ňɏssa Pøngjǣrdenlarp Jun 12 '19 at 03:23
  • Today I have a better understanding of 'using' and of the process I was trying to figure out from the "code that I don't understand" @Pongjardenlarp. If I followed your advice what would I have learned? – william_perry Jun 12 '19 at 15:41

2 Answers2

0

Solved it with the following:

Button btn = new Button();
Form formA = new Form();
formA.Text = "Form A";
formA.Name = "FormA";
this.MaximizeBox = false;
this.MinimizeBox = false;
this.BackColor = Color.White;
this.ForeColor = Color.Black;
this.Size = new System.Drawing.Size(155, 265);
this.Text = "Run-time Controls";
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.StartPosition = FormStartPosition.CenterScreen;
formA.Show();
formA.Controls.Add(btn);
Tushar
  • 481
  • 6
  • 26
-1

First the formA.ShowDialog(); will freeze there until the form is closed. Second the button you created only one and moved it around.

Change with the following :

using (Form formA = new Form()) 
{
   formA.Text = "Form A";
   formA.Name = "FormA";
   this.MaximizeBox = false;
   this.MinimizeBox = false;
   this.BackColor = Color.White;
   this.ForeColor = Color.Black;
   this.Size = new System.Drawing.Size(155, 265);
   this.Text = "Run-time Controls";
   this.FormBorderStyle = FormBorderStyle.FixedDialog;
   this.StartPosition = FormStartPosition.CenterScreen;
   formA.Show();

   for (int x = 0; x <= 3; x++)
   {
      Button btn = new Button();
      btn.Location = new System.Drawing.Point(10 + (x * 5), 10 + (x * 5));
      btn.Text = "Button" + x.ToString();
      btn.Name = "Button_" + x.ToString(); 
      formA.Controls.Add(btn);
   }
}
Tushar
  • 481
  • 6
  • 26
Franck
  • 4,438
  • 1
  • 28
  • 55