2015-09-17 37 views
0

添加ListViewItem的我寫了一個代碼爲2點的形式, 的主要形式建設 - 這得3串從子窗體(AddTask不能在功能

在主窗體(Form1):

public partial class Form1 : Form 
{ 
    int count = 0; 
    string taskName2, DateTime2, More2; 
    public Form1(string taskName1, string DateTime1, string More1, bool startworking) 
    { 
     InitializeComponent(); 

     taskName2 = taskName1; 
     DateTime2 = DateTime1; 
     More2 = More1; 
     if(startworking) 
     { 
      StartWorking(); 
     } 
    } 

你可以看到我創建3個字符串到全局使用,Form1獲取3個字符串和1個布爾變量。當布爾值爲真時,功能StartWorking開始。

在子窗體中,我有一個按鈕和3個文本框。該按鈕有一個click事件:

string taskName1 = textBox1.Text; 
string DateTime1 = textBox2.Text; 
string More1 = textBox3.Text; 
Form celender = new Form1(taskName1, DateTime1, More1, true); 
this.Close(); 

所以,當我按下子按鈕形式的布爾值被設置爲true和StartWorking功能啓動。

到這裏一切都好。

功能StartWorking:

public void StartWorking() 
{ 
    MessageBox.Show(taskName2 + " " + DateTime2 + " " + More2); 

    ListViewItem lvi = new ListViewItem(taskName2); 
    lvi.SubItems.Add(DateTime2); 
    lvi.SubItems.Add(More2); 
    listView1.Items.Add(lvi);  
} 

現在在函數中的MessageBox作品,並顯示字符串,但是當我看到ListView1的沒有什麼變化。它爲什麼不創造任何東西?

回答

0

實例化之後,您沒有顯示Form1。使用Show()方法celender.Show();顯示Form1,並且還可以像這樣更改您的代碼:

Hide(); 
string taskName1 = textBox1.Text; 
string DateTime1 = textBox2.Text; 
string More1 = textBox3.Text; 
Form1 celender = new Form1(taskName1,DateTime1,More1,true);   
celender.Show(); 
celender.Closed += (s, args) => this.Close();