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();
        }
    }
}
4

1 回答 1

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();
}
于 2012-10-19T11:53:54.427 回答