1

当我从表单中调用它时,我的这段代码可以正常工作,但是,当我从父级调用它时,它会运行代码而没有结果:

    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";
        }    

有任何想法吗?

4

1 回答 1

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();
            }
于 2012-05-06T14:01:40.640 回答