2012-10-19 47 views
0

我有以下兩段代碼,請看看它,我指出它出錯的地方。 我刪除了我稱之爲第二個窗口的功能,他們在這裏沒有意義。公共功能不正常

首先,主要的形式,這種形式調用第二種形式:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 

namespace STP_Design 
{ 
    public partial class STP2Main : Form 
    { 
     public STP2Main() 
     { 
      InitializeComponent(); 
      tabControl1.SelectedTab = tabPageDeviceManager; 
     } 
     private void pictureBox1_Click(object sender, EventArgs e) 
     { 
      MenuForm MDIMF = new MenuForm(); 
      MDIMF.MdiParent = this; 
      MDIMF.StartPosition = FormStartPosition.Manual; 
      MDIMF.Location = new Point(3, 50); 
      MDIMF.Show(); 
      tabControl1.Visible = false; 
     } 

     public void set() 
     { 
      tabControl1.Visible = true; // This statement executes, but does not result in anything I'd expect. The debugger tells me the visibility is false. 
      tabControl1.BringToFront(); 
     } 
    } 
} 

和第二形式,我會關閉並應更新第一種形式:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace STP_Design 
{ 
    public partial class MenuForm : Form 
    { 
     public MenuForm() 
     { 
      InitializeComponent(); 

      this.BringToFront(); 
     } 


     private void button1_Click(object sender, EventArgs e) 
     { 
      STP2Main stp = new STP2Main(); 
      stp.set(); 
      this.Close(); 
     } 
    } 
} 
+0

你看到的行爲是什麼? – itsmatt

+0

「調用第二個窗口」代碼可能非常重要。 – Rawling

+0

實際顯示STP2Main的代碼在哪裏? –

回答

1

你調用set方法在主窗體的新版本版本上,而不是大概已經顯示給用戶的版本。

你需要做的的就是從菜單形式的MdiParent財產當前主要形式,並調用該方法上代替。

// In menu form 
private void button1_Click(object sender, EventArgs e) 
{ 
    var mainForm = this.MdiParent as STP2Main; 
    if (mainForm != null) 
     mainForm.set(); 
    this.Close(); 
} 
+0

好吧,所以我應該改變「STP2Main stp = new STP2Main();」到「public STP2Main MainForm {get; set;}」。當我這樣做,我得到一個null參考錯誤「MainForm.Set();」 – 2pietjuh2

+1

@ 2pietjuh2我改變了我的代碼來匹配你發佈的代碼,希望這應該適合你。 – Rawling

+0

真棒這個作品:D謝謝! – 2pietjuh2