2012-03-05 12 views
1

Here are all my classes.が</p> <p>CustomerFrame =テキストボックス</p> <p>私は私のプログラムをコンパイルし、

のMainForm =リストビューをリストビューにアイテムを挿入することはできません、私のMainFormは空のリストビューで表示され、私は追加ボタンを押したときアイテムを挿入すると、CustomerFrameクラスが表示されます。テキストボックスに書き込みして[OK]をクリックすると、何の項目は私のリストビュー(MainForm)に挿入されません。どうして?

いくつかのコード:

MainFormを

using(var customerframe = new CustomerFrame()) 
{ 
    if (customerframe.DialogResult == DialogResult.OK) 
    { 
     CustomerFiles.Contact contact = customerframe.GetContact(); 
     CustomerFiles.Address address = customerframe.GetAddress(); 
     CustomerFiles.Phone phone = customerframe.GetPhone(); 
     CustomerFiles.Email email = customerframe.GetEmail(); 

     //Items in my listview 
     listviewitem = new ListViewItem(); 
     listviewitem.SubItems.Add(contact.FirstName); 
     listviewitem.SubItems.Add(contact.LastName); 
     listviewitem.SubItems.Add(phone.Home); 
     listviewitem.SubItems.Add(phone.Mobile); 
     listviewitem.SubItems.Add(address.Country); 
     listviewitem.SubItems.Add(address.ZipCode); 
     listviewitem.SubItems.Add(address.City); 
     listviewitem.SubItems.Add(address.Street); 
     listviewitem.SubItems.Add(email.Personal); 

     this.listView1.Items.Add(listviewitem); 

    } 
} 

MainFormを

private void addToolStripMenuItem_Click_1(object sender, EventArgs e) 
{ 
    customerframe.Show(); 
    CustomerManager cm = new CustomerManager(); 
} 

CustomerFrame

ところで

、私はCustomerFrameフォームがMainForm前に現れる(私はしたくないもの)と、それは一度だけアイテムを挿入しますが、作る

if (customerframe.ShowDialog() == DialogResult.OK) 

これを使用しています。

+0

スコープの問題のように私に見えます。ステップバイステップのデバッグを行い、メソッド/クラスの追加後にリストビューの内容を確認します。 – Zenwalker

+0

私はこれを何度も繰り返してきましたが、問題は "(var customerframe = new CustomerFrame()) { if(customerframe.DialogResult == DialogResult.OK) {"この行またはこの行 " this.listView1.Items.Add(listviewitem); " – user1067973

答えて

1

は、なぜあなたはダイアログボックスから別のメインフォームを開くのですか?私はあなたがこの行を削除するべきだと思います。

MainForm main = new MainForm(); 

そして簡単にするために、この

DialogResult = DialogResult.OK; 
Close(); 

カワイイを追加 - ButtonOK内のコードは次のようになります。

private void btnOk_Click(object sender, EventArgs e) 
{ 
    DialogResult = DialogResult.OK; 
    Close();  
} 

EDIT:応答新しい問題に

まず、新規作成CustomerFrame、それを表示し、それが閉じるのを待つ;新しいデータをListViewに転送します。あなたの追加ハンドラーは次のようになります:

private void addToolStripMenuItem_Click_1(object sender, EventArgs e) 
{ 
    using(var customerframe = new CustomerFrame()) 
    { 
     // I don't know what this line does 
     CustomerManager cm = new CustomerManager(); 

     if (customerFrame.ShowDialog() == DialogResult.OK) 
     { 
      CustomerFiles.Contact contact = customerframe.GetContact(); 
      CustomerFiles.Address address = customerframe.GetAddress(); 
      CustomerFiles.Phone phone = customerframe.GetPhone(); 
      CustomerFiles.Email email = customerframe.GetEmail(); 

      //Items in my listview 
      listviewitem = new ListViewItem(); 
      listviewitem.SubItems.Add(contact.FirstName); 
      listviewitem.SubItems.Add(contact.LastName); 
      listviewitem.SubItems.Add(phone.Home); 
      listviewitem.SubItems.Add(phone.Mobile); 
      listviewitem.SubItems.Add(address.Country); 
      listviewitem.SubItems.Add(address.ZipCode); 
      listviewitem.SubItems.Add(address.City); 
      listviewitem.SubItems.Add(address.Street); 
      listviewitem.SubItems.Add(email.Personal); 

      this.listView1.Items.Add(listviewitem); 
     } 
    } 
} 
+0

私はまだ同じ問題を抱えている、ことを修正して、それが二度目に挿入することはできません:/ – user1067973

+0

user1067973 @ - 私は私の答えを更新した、見てください。 –

+0

私は非常に長い時間のためにこれでトラブルがあった、とme.Iは問題がこれを知らなかったために、あなたがそれを解決しました。大変ありがとうございます – user1067973

関連する問題