2011-10-08 69 views
1

我創建了一個array list,我想在文本文件中打印數組列表的內容。在文本文件中打印數組列表的項目

的PROGRAMM從文本框取值並將其存儲在一個ArrayList,並與 添加按鈕,我想將它保存在指定位置的文本文件..

我正在使用Microsoft Visual C#2010 Express

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 

    private ArrayList StoreItems; 


    public Form1() 
    { 
     this.StoreItems = new ArrayList(); 

     InitializeComponent(); 
    } 

    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 

    } 
    private void textBox2_TextChanged(object sender, EventArgs e) 
    { 

    } 
    private void textBox3_TextChanged(object sender, EventArgs e) 
    { 

    } 
    private void textBox4_TextChanged(object sender, EventArgs e) 
    { 

    } 
    private void textBox5_TextChanged(object sender, EventArgs e) 
    { 

    } 
    private void textBox6_TextChanged(object sender, EventArgs e) 
    { 

    } 


    //add button 
    private void button1_Click(object sender, EventArgs e) 
    { 

     // Make sure an item is complete before adding it to the inventory 
     if (this.textBox1.Text.Equals("")) 
     { 
      MessageBox.Show("You must enter a code"); 
      this.textBox1.Focus(); 
      return; 
     } 

     if (this.textBox2.Text.Equals("")) 
     { 
      MessageBox.Show("You must provide the name of the item"); 
      this.textBox2.Focus(); 
      return; 
     } 

     if (this.textBox3.Text.Equals("")) 
     { 
      MessageBox.Show("You must provide the name of the supplier"); 
      this.textBox3.Focus(); 
      return; 
     } 

     if (this.textBox4.Text.Equals("")) 
     { 
      MessageBox.Show("You must enter the price per unit"); 
      this.textBox4.Focus(); 
      return; 
     } 

     if (this.textBox5.Text.Equals("")) 
     { 
      MessageBox.Show("You must enter the number of the required products"); 
      this.textBox5.Focus(); 
      return; 
     } 

     if (this.textBox6.Text.Equals("")) 
     { 
      MessageBox.Show("You must enter the number of the current stock level"); 
      this.textBox6.Focus(); 
      return; 
      //string csl = this.textBox6.Text; 
     } 



     string inValue1, inValue2, inValue3, inValue4, inValue5, inValue6; 
     inValue1 = textBox1.Text; 
     inValue2 = textBox2.Text; 
     inValue3 = textBox3.Text; 
     inValue4 = textBox4.Text; 
     inValue5 = textBox5.Text; 
     inValue6 = textBox6.Text; 

     /* string result = (inValue1 + " " + inValue2 + " " + inValue3 + " " 
      + inValue4 + " " + inValue5 + " " + inValue6); */ 

     string result=(inValue1+inValue2+inValue3+inValue4+inValue5     +inValue6); 
     StoreItems.Add(result); 



     System.IO.File.AppendAllText(@"C:\Users\v\Desktop\text.txt", Environment.NewLine + result); 

    } 

    private void button2_Click(object sender, EventArgs e) 
    { 

    } 
+0

ArrayList是一個遺留類,從.NET支持的泛型開始。您應該避免使用它來代替普通列表,或者,在這種情況下,列表中的代碼爲

+1

請刪除所有與您的問題無關的代碼。你有很多東西,很難知道你希望我們看到什麼部分。那些空的方法太分散注意力。 –

+0

另外,.Net的確切版本? –

回答

2

什麼版本的.Net?

public Form1() 
{ 
    InitializeComponent(); 

    private TextBox[] TextBoxes = {textBox1, textBox2, textBox3, textBox4, textBox5, textBox6}; 

    private List<string> storeItems = new List<string>(); 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    var buffer = new StringBuilder(); 
    foreach(var textBox in textBoxes) 
    { 
     if (string.IsNullOrEmpty(textBox.Text)) 
     { 
      textBox.BackColor = Color.FromName("LightSalmon"); 
      MessageBox.Show("This item cannot be left blank"); 
      textBox.Focus(); 
      return; 
     } 
     textBox.BackColor = Colors.FromName("Window"); 
     buffer.Append(textBox.Text); 
    } 

    var result = buffer.ToString(); 
    storeItems.Add(result); 
    System.IO.File.AppendAllText(@"C:\Users\v\Desktop\text.txt", Environment.NewLine + result); 
} 
+0

謝謝你的回答,但我仍然有一個問題,它打擊了我在這行文本框的錯誤:foreach(文本框中的var textBox) –

+0

Microsoft Visual C#2010 Express –

+0

任何人都可以幫助我解決這個問題嗎?這行文本框的錯誤:foreach(文本框中的var textBox) –