Basic learning of C # architecture----adding forms and controls dynamically

Source: Internet
Author: User

Write from the form application:

Task one: Create a Form object of form class frm

Using System.Windows.Forms; Using directives create the required namespaces with the form object

If the using directive is unsuccessful, you should add a reference,

Using System.Drawing;

Namespace WindowsFormsApplication6

{
public partial class Form1:form//form is a blank form
{

Form1 inherit from a blank form, add a new control button by constructor , etc.
Public Form1 ()//constructor
{
InitializeComponent (); initialization function
}

private void InitializeComponent ()//initialization function
{
This.textbox1 = new System.Windows.Forms.TextBox (); To create a new textbox1 control
This.                                                     SuspendLayout (); Suspend control updates so that they are updated together later
//
TextBox1
//
This.textBox1.Location = new System.Drawing.Point (108, 128); property settings
This.textBox1.Size = new System.Drawing.Size (100, 21);
This.textBox1.TabIndex = 0;
//
This.textBox1.Name = "TextBox1";
Form1
//
This. Autoscaledimensions = new System.Drawing.SizeF (6F, 12F);
This. AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
This. ClientSize = new System.Drawing.Size (284, 261);
This. Controls.Add (This.textbox1);//Add a newly generated control textbox1
This. Name = "Form1";
This. Text = "Form1";
This. Load + = new System.EventHandler (this. Form1_Load);
This. ResumeLayout (FALSE);
This. PerformLayout ();

}

}

      Static class program
     {
       ///<summary>     // The main entry point of the application.
       ///</summary>
         [ STAThread]
        static void Main ()
       {
      &NBSP ; Application.enablevisualstyles ();
        Application.setcompatibletextrenderingdefault (false);
        Application.Run (new Form1 ()),     //Generate objects of the Form1 class, and Application.Run provides some necessary methods
       }
   }

}

By observing the above analysis

Dynamically generating forms and adding controls

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows.Forms;
Using System.Drawing;

Namespace ConsoleApplication6
{
Class Program
{
static void Main (string[] args)
{
form frm = new form ();
frm.  Show (); This sentence can not

Create a new TextBox control and add it to the form.

TextBox textBox1 = new TextBox ();

Name the control in order to remove it later. The name must be specified
If a control is added at run time.
Textbox1.name = "TextBox1";

Add the control to the form ' s control collection.
frm. Controls.Add (TextBox1);
Textbox1.size = new Size (100, 10);
Textbox1.location = new Point (10, 10);
Application.Run (frm); //This sentence must be last written

}
}
}

Basic learning of C # architecture----adding forms and controls dynamically

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.