當前位置:成語大全網 - 新華字典 - 關於C#自動生成的代碼

關於C#自動生成的代碼

可以改動,妳說的是:Form1.Designer.cs 這裏面的自動生成的代碼吧?

這裏面只是將空間初始化,然後付給他默認值,比如控件所在位置之類的。

類似這些:

this.label1 = new System.Windows.Forms.Label();

this.textBox1 = new System.Windows.Forms.TextBox();

this.textBox2 = new System.Windows.Forms.TextBox();

this.label2 = new System.Windows.Forms.Label();

this.textBox3 = new System.Windows.Forms.TextBox();

this.label3 = new System.Windows.Forms.Label();

this.textBox4 = new System.Windows.Forms.TextBox();

this.label4 = new System.Windows.Forms.Label();

this.textBox5 = new System.Windows.Forms.TextBox();

this.label5 = new System.Windows.Forms.Label();

this.button1 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// label1

//

this.label1.AutoSize = true;

this.label1.Location = new System.Drawing.Point(12, 14);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(53, 12);

this.label1.TabIndex = 0;

this.label1.Text = "認證碼:";

如果妳要手寫代碼的話,基本不會這麽去寫。大多是在用到的時候才去寫,這部分是在窗體被初始化的時候就加載的,妳也可以在使用的時候手動創建,比如點擊壹個按鈕,就在某個位置顯示壹個文本框:

private void button1_Click(object sender, EventArgs e)

{

TextBox tb = new TextBox();

tb.Size = new System.Drawing.Size(20,160);

tb.Location = new Point(100,100);

tb.Text = "默認值";

this.Controls.Add(tb);

}

以上就是動態的創建壹個文本框,大小為 20 × 160 像素,位置在 橫豎坐標都是 100 的位置,文本內容是 ”默認值“ 的文本框。最後 this.Controls.Add(tb); 就是將它添加到窗體上。

手寫代碼就是這樣的,當然,妳也可以動態的綁定事件。

只要遵照壹個原則就可以完全自己手寫了,就是先構造後使用就行了。

其他的就是註意壹下,控件作用域的問題了,比如以上代碼,在想拿到那個文本框的值的時候,只能在 button1_Click 的作用域中寫(註:當然妳也可以用窗體的控件查找方法在任何位置獲得。)

如果放到 From 的直接作用域下,那麽在整個類裏面都可以直接使用。