2016-07-18 6 views
-3

4つのフォームでプログラムを作成しています。 "finalResultTab"というフォームは最後の子フォームです。メッセージボックスのokボタンが押された場合、これと "briefResult"と呼ばれるフォームを閉じ、 "Form1"と呼ばれるメインフォームを再度アクティブにする必要があります。 しかし、私は現在のものの横のいずれかを制御できません。私は何をすべきか ? インクルードは次のとおりです。前のフォームを制御できません。C#

namespace WindowsFormsApplication1 
{ 
    public partial class finalResultTab : Form 
    {  
//....... 
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) 
     { 
      string infoVar1 = dataGridView1.CurrentRow.Cells[0].Value.ToString(); //title 
      string infoVar2 = dataGridView1.CurrentRow.Cells[1].Value.ToString(); //tracknumber    
      string infoVar3 = dataGridView1.CurrentRow.Cells[2].Value.ToString(); //genre 
      string infoVar10 = dataGridView1.CurrentRow.Cells[3].Value.ToString(); //album 
      DateTime infoVar4 = Convert.ToDateTime(dataGridView1.CurrentRow.Cells[4].Value);//year 
      UInt32 yearCorrected = Convert.ToUInt32(infoVar4.Year); 
      string infoVar5 = dataGridView1.CurrentRow.Cells[5].Value.ToString();//artist 
      string infoVar6 = dataGridView1.CurrentRow.Cells[6].Value.ToString();//diskcount 
      string infoVar7 = dataGridView1.CurrentRow.Cells[7].Value.ToString();//disknumber 
      string infoVar8 = dataGridView1.CurrentRow.Cells[8].Value.ToString();//trackCount    

      Form1 mainFrm = new Form1(); 
      briefResult frm1 = new briefResult(); 
      TagLib.File tagFile = TagLib.File.Create(Form1.passFile); 
       tagFile.Tag.Title = infoVar1; 
       tagFile.Tag.Album = infoVar10; 
       tagFile.Tag.AlbumArtists = infoVar5.Split(','); 
       tagFile.Tag.Year = yearCorrected; 
       tagFile.Tag.DiscCount = Convert.ToUInt32(infoVar6); 
       tagFile.Tag.Disc = Convert.ToUInt32(infoVar7); 
       tagFile.Tag.TrackCount = Convert.ToUInt32(infoVar8); 
       tagFile.Tag.Track = Convert.ToUInt32(infoVar2); 
       tagFile.Tag.Genres = infoVar3.Split(','); 
       tagFile.Save(); 

      if (MessageBox.Show("Tags saved successfully", "Success", MessageBoxButtons.OK) == DialogResult.OK) 
      { 
       frm1.Close();     
       //this.Close(); 
       mainFrm.Enabled = true; 
       mainFrm.BringToFront(); 

      } 
     } 
} 
} 
+0

[Application.OpenForms](https://msdn.microsoft.com/en-us/library/system.windows.forms.application.openforms.aspx)を使用してみましたか? – stuartd

+0

'Form1 mainFrm = new Form1();'コードが読むと、あなたは「新しい」フォームを作成しています。あなたは再表示しようとしている既存のメインフォームを参照していません。 – LarsTech

+0

番号。それは何をするためのものか?レヴェラント機能はありますか? Application.OpenForms。 ??? –

答えて

0

あなたがClose()を呼び出す(代わりHide()を使用)していないと仮定すると、あなたのForm1に新しいフォームを開いたときにオーバーループすることによって、あなたはまだ、既存のForm1への参照を取得することができるはずstuartdによって示されるようにApplication.OpenFormsコレクション:

if (MessageBox.Show("Tags saved successfully", "Success", MessageBoxButtons.OK) == DialogResult.OK) 
{ 
    foreach(var form in Application.OpenForms) 
    { 
     // is form of type Form1? 
     if(form is Form1) 
     { 
      // cast the form to Form1 
      var mainFrm = (Form1)form; 
      mainFrm.Show(); 
      mainFrm.Enabled = true; 
      mainFrm.BringToFront(); 
     } 
    }   
} 

それが戻って前のインスタンスにあなたをもたらすことはありませんので、あなたがForm1の新しいインスタンスを作成しないでください。

関連する問題