2016-07-27 162 views
-1

我正在用一個按鈕開發awindows表單應用程序。每次按下按鈕,它都會生成文本框。例如,如果我單擊該按鈕5次,則會有5個文本框。現在用戶將輸入數據到這些文本框。而當他按下回車鍵(另一個按鈕)時,它必須存儲到一個文本文件中。我在這一點結構,因爲我在運行時生成文本框。 任何人都可以幫助我。如何將數據保存到文本文件從幾個文本框到文本文件

我的代碼是

私人無效create_pos(對象發件人,EventArgs的) { counter_1 ++;

 if (counter_1 == count) 
     { 
      left += 300; 
      top = 50; 
      count = count + 25; 

     } 
     List<Button> buttons = new List<Button>(); 

     Button newButton = new Button(); 
     buttons.Add(newButton); 
     this.Controls.Add(newButton); 
     newButton.Left = left; 
     newButton.Top = top; 



      TextBox newtextbox = new TextBox(); 

     Controls.Add(newtextbox); 


     if (counter_1 == 100) 
     { 
      button1.Enabled = false; 
     } 

     newtextbox.Left = left + 100; 
     newtextbox.Name = "text" + counter_1; 

     // TextWriter tsw = new StreamWriter(@"d:\test.txt", true); 
     //tsw.WriteLine(newtextbox.Text); 
     // tsw.Close(); 

     newtextbox.Top = top; 
     top += newButton.Height + 2; 
     newButton.Text = "position" + counter_1; 


     textBox1.Text = newtextbox.Name; 
    } 
    private void Save_Click(object sender, EventArgs e) 
    { 
     foreach (Control item in Controls) 
     { 

      if (item.GetType() == typeof(TextBox)) 
      { 
       savetext[counter_1] = item.Text.ToString(); 
      } 



      System.IO.File.WriteAllText("d:\\test.txt", savetext.ToString()); 
     } 
    } 
+3

有人會幫你,如果你包括你已經嘗試過 –

+0

的代碼是從生成的文本框保存數據或讀取數據的問題? – Thennarasan

+0

保存數據的問題。 –

回答

0

使用this.FindControl方法找到窗體上的控件,並檢查它是否爲文本框。如果是文本框,則使用其.Text屬性讀取內容。使用循環遍歷控件。

如果不mainting生成控件的列表,然後做到這一點:

string filePath = "path to text file"; 
StringBuilder contentBuilder = new StringBuilder(); 
foreach (Control item in Controls) 
{ 
    //i don't remember if this check would work. if it doesn't work try item.GetType() == typeof(TextBox) 
    if (item is TextBox) 
    { 
      contentBuilder.Append(((TextBox)item).Text); 
    } 
} 

string content = contentBuilder.ToString(); 

if(!string.IsEmptyOrWhiteSpace(content)) 
{ 
    using(StreamWriter writer = new StreamWriter(filePath)) 
    { 
     //check if Write or WriteLine or something else would work. 
     writer.Write(content); 
    } 
} 

如果您正在使用列表來維護所產生的控制則只需更改爲循環的一部分。

+0

用一些示例代碼解釋他,他會更好地理解。 –

+0

不需要找控件。只需通過表單上的控件集合進行迭代即可。 –

+0

跟蹤動態控件(例如List)會更加優雅和高效。特別是,您可以區分已在表單上(或稍後添加)的文本控件與動態控件。此外,「做你的處理」是問題的核心部分。 –

0

你想要做的是,迭代TextBox類型的子控件,然後收集它的文本,並用這些收集的值創建一個字符串。現在你有文本寫入文件和文件的路徑。然後你可以使用File.WriteAllText方法要執行的操作:

StringBuilder contentBuilder = new StringBuilder(); 
foreach (TextBox dynamicText in this.Controls.OfType<TextBox>()) 
{ 
    contentBuilder.AppendFormat("Text in {0} is {1} \n", dynamicText.Name, dynamicText.Text); 
} 
string pathOfFile = @"path here"; 
System.IO.File.WriteAllText(pathOfFile,contentBuilder.ToString()); 
+0

假定表單(現在或將來)上沒有非動態TextBox控件。可能會或可能不是一個有效的假設,但它是一個脆弱的(在未來的代碼維護中很容易中斷)。 –

1

我會跟蹤你的動態文本框,以便您可以現在你所關心的那些和任何其他文本框可能已存在於區分表單,或者您可能稍後添加的表單。我假設,當您生成一個TextBox並將其添加到表單中時,您還可以在列表中存儲對它的引用,例如,

List<TextBox> dynamicTextBoxes = new List<TextBox>(); 

在了回車鍵(或要觸發寫任何其他事件處理函數)的事件處理程序,你可以收集來自控制文本和寫出來。目前尚不清楚究竟你的要求是這裏的東西,讓我們寫一個接受文本框和一個文件名的列表,同時

private void WriteTextBoxes(string path, List<TextBox> textBoxes) 
{ 
    // Here we use some Linq to quickly get all of the text, but you could 
    // also use an explicit loop 
    string text = string.Join(Environment.NewLine, textBoxes.Select(t => t.Text)); 
    File.WriteAllText(path, text); 
} 

然後,您可以調用寫的文字,一行一個功能從事件處理程序方法,傳遞的動態文本框的列表控件

WriteTextBoxes(@"C:\Temp\My.txt", dynamicTextBoxes); 
相關問題