C#子表单不爽快

时间:2012-05-06 13:52:12

标签: c# winforms refresh parent

我有这个代码,当我从表单中调用它时工作正常,但是,当我从Parent调用它时,它运行代码没有结果:

    public void hideHelp()
    {
        //Check in db if panel1 is visible
        SqlCeCommand checkHelp = new SqlCeCommand("Select Show_Help from Options where Opt_Id = 1", this.optionsTableAdapter.Connection);
        if (this.optionsTableAdapter.Connection.State == ConnectionState.Closed) 
         { this.optionsTableAdapter.Connection.Open(); }

        try
        {
            bool showHelp = (bool)(checkHelp.ExecuteScalar());

            this.panel1.Visible = showHelp;
            this.Refresh();

        }
        catch (Exception ex) { MessageBox.Show(ex.Message); }

    }

在主表单上,我有一个切换按钮,其中包含以下代码:

    private void tglHelp_Click(object sender, EventArgs e)
    {

        if (tglHelp.ToggleState.ToString() == "On")
        {
            HRDataSet.OptionsRow updateHelp = hRDataSet.Options.FindByOpt_Id(1);
            try
            {
                updateHelp.Show_Help = true;
                this.optionsTableAdapter.Update(this.hRDataSet);

                Form activeChild = this.ActiveMdiChild;



                if (activeChild.Name == "frmAddEmployees")
                {
                    frmAddEmployees chForm = new frmAddEmployees();
                    chForm.MdiParent = this;


                    chForm.hideHelp();
                }

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName);
            }
            tglHelp.Text = "Help Panel \nOFF";
        }    

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

在这段代码中

            if (activeChild.Name == "frmAddEmployees") 
            { 
                frmAddEmployees chForm = new frmAddEmployees(); 
                chForm.MdiParent = this; 
                chForm.hideHelp(); 
            } 

您打开另一个frmAddEmployees并添加到MDI,但您无法显示它。 如果你的意图是调用activeChild标识的当前frmAddEmployees中的代码,你应该使用类似的东西

            if (activeChild.Name == "frmAddEmployees") 
            { 
                ((frmAddEmployees)activeChild).hideHelp();
            }