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