C#構架之基礎學習----動態添加表單和 控制項

來源:互聯網
上載者:User

標籤:

仿照表單應用程式編寫:

任務一:產生一個Form類的表單對象frm

using System.Windows.Forms;         //using指令使用Form對象建立所需的命名空間          

 //如果using指令不成功,則應該去添加引用,

using System.Drawing;

namespace WindowsFormsApplication6

{
      public partial class Form1 : Form  //Form是空白表單
    {  

           //Form1繼承於空白表單,又通過建構函式添加新的控制項button等
            public Form1()               //建構函式
           {
              InitializeComponent();    //初始化函數
            }

       private void InitializeComponent()     //初始化函數
       {
        this.textBox1 = new System.Windows.Forms.TextBox();    //建立新的textbox1控制項
        this.SuspendLayout();                                                     //掛起控制項更新,使得以後一同更新
        //
        // textBox1
        //
        this.textBox1.Location = new System.Drawing.Point(108, 128);  //屬性設定
        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);             //添加新產生的控制項textbox1
          this.Name = "Form1";
           this.Text = "Form1";
          this.Load += new System.EventHandler(this.Form1_Load);
         this.ResumeLayout(false);
           this.PerformLayout();

         }

     }

      static class Program
     {
       /// <summary>
      /// 應用程式的主進入點。
       /// </summary>
         [STAThread]
        static void Main()
       {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());     //產生Form1類的對象,並且Application.Run提供一些必要的方法
       }
   }

}

///////////////////通過觀察上述分析

動態產生表單和添加控制項

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();  //這一句可以沒有

           // 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);                     //這一句務必最後寫

         }
    }
}

C#構架之基礎學習----動態添加表單和 控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.