2011-03-25 71 views
4

我最近從Visual Basic 6移到C#2010 .NET。如何在C#2010.NET中創建控件數組?

在Visual Basic 6中,通過更改「索引」就可以放置多少個控制數組。

我,不知道這是否可能在C#如果是的話我將如何去與像一類做:

func fc = new func(); 

但是隨着越來越多的不僅僅是一個陣列中的FC,這可能嗎?

而且將更加明朗化,

的Visual Basic 6,當你加載像一個文本框或用戶控制它在屬性窗口的「索引」一個選項和控制,如果您更改爲0,1等等......它可以讓你使用所有這些索引,而不用加載多個控件50次。

我認爲這可能與arraylist有關,但我不完全確定。

感謝您的任何幫助。

+0

.NET沒有VB6的控制數組。沒有簡單的方法來模擬它們。 – Oded 2011-03-25 16:34:16

回答

8

該代碼片段不會讓你走得很遠。創建控件數組沒有問題,只需在窗體構造函數中初始化它。然後,您可以將其作爲屬性公開,但這通常是一個壞主意,因爲您不想公開實現細節。事情是這樣的:

public partial class Form1 : Form { 
    private TextBox[] textBoxes; 

    public Form1() { 
     InitializeComponent(); 
     textBoxes = new TextBox[] { textBox1, textBox2, textBox3 }; 
    } 

    public ICollection<TextBox> TextBoxes { 
     get { return textBoxes; } 
    } 
} 

,然後讓你寫:

var form = new Form1(); 
form.TextBoxes[0].Text = "hello"; 
form.Show(); 

但是,不要讓形式管理自己的文本框中。

+0

沒有從'string'到'TextBox'的轉換,這不會被編譯。 – 2011-03-25 16:42:12

+0

+1修正了錯字 – 2011-03-25 16:44:41

+0

謝謝@Felice – 2011-03-25 16:49:07

1

不完全一樣VB6,但它是很容易寫的代碼你的自我在C#。

如果創建一個控制,如在設計一個Button可以從*.Designer.cs文件

它通常看起來像這樣

private System.Windows.Forms.Button button1; 
... 
this.button1.Location = new System.Drawing.Point(40, 294); 
this.button1.Name = "button1"; 
this.button1.Size = new System.Drawing.Size(75, 23); 
this.button1.TabIndex = 14; 
this.button1.Text = "Button1"; 
this.button1.UseVisualStyleBackColor = true; 
this.button1.Click += new System.EventHandler(this.button1_Click); 
... 
this.Controls.Add(this.button1); 

切的代碼進行復制代碼,並在方法粘貼相反,返回按鈕

private Button CreateButton() 
{ 
    private System.Windows.Forms.Button button1; 

    this.button1.Location = new System.Drawing.Point(40, 294); // <-- change location for each 
    this.button1.Name = "button1"; 
    this.button1.Size = new System.Drawing.Size(75, 23); 
    this.button1.TabIndex = 14; // <-- increase tab index or remove this line 
    this.button1.Text = "Button1"; 
    this.button1.UseVisualStyleBackColor = true; 
    this.button1.Click += new System.EventHandler(this.button1_Click); 

    this.Controls.Add(this.button1); 
    return button; 
} 

然後調用此方法像這樣

List<Button> buttons = new List<Button>(); 
for(int i = 0; i < 10; i++) 
{ 
    buttons.Add(CreateButton()); 
} 
2

在.NET則需要創建的控件的數組,然後你會實例爲陣列的每個元件的TextBox控件,設置控制的屬性,並把它定位在窗體上:

TextBox[] txtArray = new TextBox[500]; 
    for (int i = 0; i < txtArray.length; i++) 
    { 
     // instance the control 
     txtArray[i] = new TextBox(); 
     // set some initial properties 
     txtArray[i].Name = "txt" + i.ToString(); 
     txtArray[i].Text = ""; 
     // add to form 
     Form1.Controls.Add(txtArray[i]); 
     txtArray[i].Parent = Form1; 
     // set position and size 
     txtArray[i].Location = new Point(50, 50); 
     txtArray[i].Size = new Size(200, 25); 
    } 
. 
. 
. 
Form1.txt1.text = "Hello World!"; 

除非你的佈局更簡單(即行和列的文本框),你可能會發現使用設計器更容易,更省時,更易於維護。

0

基於特里戈的模板:

這裏的示例處理2維的textBox陣列
PANEL1必須與設計師
(我不得不自動滾動=真,尺寸= 858; 525)被創建

public partial class Form1 : Form 
{ 
    TextBox[,] txtBoxArray = new TextBox[2,100]; 
    public Form1() 
    { 
     InitializeComponent(); 

     for (int i = 0; i < txtBoxArray.GetLength(0); i++) 
     { 
      for (int j = 0; j < txtBoxArray.GetLength(1); j++) 
      { 
       // instance the control 
       txtBoxArray[i, j] = new TextBox(); 
       // set some initial properties 
       txtBoxArray[i, j].Name = "txtBox_" + i.ToString() + "_" + j.ToString(); 
       txtBoxArray[i, j].Text = txtBoxArray[i, j].Name; //""; 
       // add to form 
       this.Controls.Add(txtBoxArray[i,j]); 
       txtBoxArray[i, j].Parent = panel1; 
       // set position and size 
       txtBoxArray[i, j].Location = new Point(50+i*333, 50 + j * 25); 
       txtBoxArray[i, j].Size = new Size(200, 25); 
      } 
     } 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 
    //... 


} 

this will look like this

相關問題