公共职能不能正常运作

时间:2012-10-19 11:49:40

标签: c# winforms function public

我有以下两段代码,请看一下,我指出哪里出错了。 我删除了我称之为第二个窗口的函数,它们在这里没有意义。

首先,主表单,此表单调用第二种形式:

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

1 个答案:

答案 0 :(得分:1)

您正在主表单的版本上调用set方法,而不是您可能已经向用户显示的版本。

您需要做的是从菜单表单的MdiParent属性中获取当前主表单,然后在 上调用该方法。< / p>

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