2015-10-22 42 views
-3

我一直在尋找解決方案,但仍一無所獲
這裏是我的代碼:如何重裝形式

private void Form1_Load(object sender, EventArgs e) 
{ 
    RichTextBox rtb = new RichTextBox(); 
    rtb.Text = File.ReadAllText(@"C:\Users\Admin\Desktop\myfile\customers.txt"); 
    int i = 0; 
    foreach (string line in rtb.Lines) 
    { 
     if (line == "--") 
     { 
      ListViewItem item = new ListViewItem(); 
      item.Text = rtb.Lines[i + 1]; 
      item.SubItems.Add(rtb.Lines[i + 2]); 
      item.SubItems.Add(rtb.Lines[i + 3]); 
      item.SubItems.Add(rtb.Lines[i + 4]); 
      listView1.Items.Add(item); 
     } 

     i += 1; 
    } 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    Form2 pop = new Form2(); 
    pop.ShowDialog(); 

    string name = pop.name; 
    int age = int.Parse(pop.Age); 
    string dob = pop.DateOfBirth; 
    string addr = pop.Address; 

    StreamWriter write = new StreamWriter(@"C:\Users\Admin\Desktop\myfile\customers.txt",true); 

    write.Write("--\n"); 
    write.Write("{0}\n",name); 
    write.Write("{0}\n",dob); 
    write.Write("{0}\n",age); 
    write.Write("{0}\n",addr); 
    write.Close(); 
} 

的問題是我怎麼重新加載列表視圖我寫數據到文本後文件?

+0

的WinForms的?的WebForms? WPF? ASP.NET MVC? –

回答

3

提取邏輯出Form1_Load的

private void Form1_Load(object sender, EventArgs e) 
{ 
    RefreshListView(); 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    Form2 pop = new Form2(); 
    pop.ShowDialog(); 

    string name = pop.name; 
    int age = int.Parse(pop.Age); 
    string dob = pop.DateOfBirth; 
    string addr = pop.Address; 


        StreamWriter write = new StreamWriter(@"C:\Users\Admin\Desktop\myfile\customers.txt",true); 

        write.Write("--\n"); 
        write.Write("{0}\n",name); 
        write.Write("{0}\n",dob); 
        write.Write("{0}\n",age); 
        write.Write("{0}\n",addr); 
        write.Close(); 

    RefreshListView(); 
} 

private void RefreshListView() 
{ 
    listView1.Items.Clear(); 

    RichTextBox rtb = new RichTextBox(); 
    rtb.Text = File.ReadAllText(@"C:\Users\Admin\Desktop\myfile\customers.txt"); 
    int i = 0; 
    foreach (string line in rtb.Lines) 
    { 
     if (line == "--") 
     { 
      ListViewItem item = new ListViewItem(); 
      item.Text = rtb.Lines[i + 1]; 
      item.SubItems.Add(rtb.Lines[i + 2]); 
      item.SubItems.Add(rtb.Lines[i + 3]); 
      item.SubItems.Add(rtb.Lines[i + 4]); 
      listView1.Items.Add(item); 
     } 

     i += 1; 
    } 
} 
+2

很簡單!多謝兄弟 !!! – ZeroX