2012-07-21 43 views
0

我目前有一個表格,我動態地創建了一個文本框,按鈕等二維數組我只是發現我的程序的其他部分無法訪問我創建的文本框?有沒有辦法我可以做到這一點?如何從程序的其他部分訪問窗體中動態生成的文本框?

我的代碼去是這樣的:

public Form1() 
    { 
     int column = 4; 

     System.Windows.Forms.TextBox[,] textbox = new System.Windows.Forms.TextBox[column, row]; 

     for (int i = 0; i < column; i++) 
     { 
      for (int j = 0; j < row; j++) 
      { 
       textbox[i, j] = new System.Windows.Forms.TextBox(); 
       textbox[i, j].Size = new Size(80, 20); 
       textbox[i, j].Name = "textbox_" + i + "_" + j; 
       textbox[i, j].Location = new System.Drawing.Point((i * 80) + 20, (j * 20) + 30); 
       textbox[i, j].Visible = true; 
       Controls.Add(textbox[i, j]); 

      } 
     } 

     /////fill the textboxes with data////// 
    } 

我無法訪問外部方法的文本框,我該怎麼辦呢?你能提供一些工作編碼嗎?我仍然相對較新的C#,非常感謝你

+0

範圍?在方法外部聲明你的文本框? – KMC 2012-07-21 10:33:37

+0

是的,我剛剛有一個大腦放屁,它在當時並沒有發生在我身上。 – 2012-07-21 17:17:01

回答

0

你可以添加一個類級別的屬性,就像

public class Form1 
{ 
    public System.Windows.Forms.TextBox[,] textbox { get; set; } 

    public Form1() 
    { 
     int column = 4; 

     textbox = new System.Windows.Forms.TextBox[column, row]; 

     for (int i = 0; i < column; i++) 
     { 
      for (int j = 0; j < row; j++) 
      { 
       textbox[i, j] = new System.Windows.Forms.TextBox(); 
       textbox[i, j].Size = new Size(80, 20); 
       textbox[i, j].Name = "textbox_" + i + "_" + j; 
       textbox[i, j].Location = new System.Drawing.Point((i * 80) + 20, (j * 20) + 30); 
       textbox[i, j].Visible = true; 
       Controls.Add(textbox[i, j]); 

      } 
     } 
    } 
} 

然後,你可以簡單地做喜歡

Form1 myForm = GetMyForm(); 
System.Windows.Forms.TextBox[,] theTextboxArray = myForm.textbox; 
+0

謝謝你,你的回答看起來很優雅 – 2012-07-21 11:05:31

+0

我嘗試將你的代碼複製到我的程序中,但它有錯誤,「名稱'GetMyForm'在當前上下文中不存在」,我知道解決它很簡單,但我的大腦現在被擊中,我該如何解決這個問題? – 2012-07-21 11:11:50

+0

'GetMyForm'在當前上下文中不存在,導致它不在某處定義。你必須告訴編譯器'GetMyForm'方法是什麼。 在這個原因中,我使用它作爲佔位符函數,導致訪問需要對錶單進行引用的文本框。如果你不知道這是什麼,請查看'Program.cs'文件和'static void Main()'方法。應該有一個'新的Form1()'調用。 如果這對你沒有幫助,請考慮在c#中使用面向對象編程中的一些tutoials。 – GameScripting 2012-07-21 11:17:52

0

當然,你不能與textbox_i_j其中i & j是與智能感知的數字,因爲它不支持,但你可以讓他們這樣

TextBox GetTB(string name) 
{ 
return (Controls.FindControl(name) as TextBox); 
} 
訪問它們
0
Declare int column = 4; 
System.Windows.Forms.TextBox[,] textbox = new System.Windows.Forms.TextBox[column, row]; 

Form1以外的構造函數。你的代碼應該看起來像:

int column = 4; 
System.Windows.Forms.TextBox[,] textbox; 
public Form1() 
{ 


    textbox = new System.Windows.Forms.TextBox[column, row]; 

    for (int i = 0; i < column; i++) 
    { 
     for (int j = 0; j < row; j++) 
     { 
      textbox[i, j] = new System.Windows.Forms.TextBox(); 
      textbox[i, j].Size = new Size(80, 20); 
      textbox[i, j].Name = "textbox_" + i + "_" + j; 
      textbox[i, j].Location = new System.Drawing.Point((i * 80) + 20, (j * 20) + 30); 
      textbox[i, j].Visible = true; 
      Controls.Add(textbox[i, j]); 

     } 
    } 

    /////fill the textboxes with data////// 
} 
0

我有搜索網絡的答案,而我等待一些聰明的人回答我的問題。我使用的字典檢索的文本框中,是這樣的:

Get a Windows Forms control by name in C#

//聲明

Dictionary <String, System.Windows.Forms.TextBox> dictionaryTextBox = new Dictionary<string, System.Windows.Forms.TextBox>(); 

//創建數組的

System.Windows.Forms.TextBox[,] textbox = new System.Windows.Forms.TextBox[column, row]; 

     for (int i = 0; i < column; i++) 
     { 
      for (int j = 0; j < row; j++) 
      { 
       textbox[i, j] = new System.Windows.Forms.TextBox(); 
       textbox[i, j].Size = new Size(80, 20); 
       textbox[i, j].Name = "textbox_" + i + "_" + j; 
       textbox[i, j].Location = new System.Drawing.Point((i * 80) + 20, (j * 20) + 30); 
       textbox[i, j].Visible = true; 
       Controls.Add(textbox[i, j]); 


        //new added 
       dictionaryTextBox.Add (textbox[i, j].Name, textbox[i, j]); 

      } 
     } 

//檢索

 System.Windows.Forms.TextBox retrieve = dictionaryTextBox["textbox_3_3"]; 

     retrieve.Text = "apple"; 
相關問題