c#关闭表单后更改按钮的状态?

时间:2015-05-11 13:56:38

标签: c# forms button

这是我的问题:我有一个主窗体,我有一个包含一些按钮的面板,当用户点击一个按钮时,一个窗体被打开。(我有一些按钮,点击这些按钮,用户可以打开不同的窗体)如果用户再次单击相同按钮,则可以关闭表单。 这就是我的所作所为:

在主窗体中,我有一个方法,当用户点击其中一个按钮时调用该方法,该方法检查与该按钮相关联的文本,以确定点击的按钮是哪个。一旦我发现哪个是单击的按钮,它就会启动与按钮关联的表单。

这是代码

private void tlStBtn_Click(object sender, EventArgs e)
{
        //// loop through all items in the ToolStrip
        //foreach (Object item in toolStripMain.Items)
        //{
        //    // if this item is a ToolStripButton object, check it
        //    if (item is ToolStripButton)
        //    {
        //        // cast the item to a ToolStripButton object and check it if the sender of the event is the currently looked at button in the loop
        //        ToolStripButton button = (ToolStripButton)item;
        //        button.Checked = (button == sender);
        //    }
        //}

        foreach (ToolStripItem item in this.VerticalToolBox.Items)
        {
            if ((item != sender) &&
                (item is ToolStripButton))
            {
                ((ToolStripButton)item).Checked = false;
            }
        }

        if (sender_old != sender)
        {
            sender_old = sender;
            if ((sender as ToolStripButton).Text == "Protection")
            {
                if (!Application.OpenForms.OfType<frm_Protection>().Any())
                {
                    frm_Protection Newf = new frm_Protection(ref CurrentProject);
                    Newf.Show();

               }
            }
            else
            {
                if (Application.OpenForms.OfType<frm_Protection>().Any())
                {
                    Application.OpenForms.OfType<frm_Protection>().First().Close();
                    Properties.Settings.Default.Save();
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

                if ((sender as ToolStripButton).Text == "Info")
                {
                    if (!Application.OpenForms.OfType<Frm_ObjectInfo>().Any())
                    {
                        Frm_ObjectInfo Newform = new Frm_ObjectInfo();
                        Newform.Show();
                    }
                }
                else
                {
                    if (Application.OpenForms.OfType<Frm_ObjectInfo>().Any())
                    {
                        Application.OpenForms.OfType<Frm_ObjectInfo>().First().Close();
                        Properties.Settings.Default.Save();
                    }

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }

            if ((sender as ToolStripButton).Text == "Layers")
            {
                if (!Application.OpenForms.OfType<Frm_LayersManage>().Any())
                {
                    Frm_LayersManage Newform = new Frm_LayersManage();
                    Newform.Show();
                    Application.OpenForms.OfType<Frm_LayersManage>().First().UpdateLayers(null, CurrentProject.layers);
                }
            }
            else
            {
                if (Application.OpenForms.OfType<Frm_LayersManage>().Any())
                {
                    Application.OpenForms.OfType<Frm_LayersManage>().First().Close();
                    Properties.Settings.Default.Save();
                    UpdateScreen = true;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

            if (Properties.Settings.Default.Grip2Enabled && (sender as ToolStripButton).Text == "Insert Grip")
            {
                gbx_SelectGrip.Visible = true;

            }
            else
            {
                gbx_SelectGrip.Visible = false;
            }

            //SelectedPoints.Clear();
            //MousePointList.Clear();
            //myIDs.Clear();
            //IdxPointsEnt.Clear();
            //RatiosLines.Clear();
            //CadSource.cuts_tmp.Clear();
            //IDAddedCutList.Clear();
            //ZoomPort.SetValue(0, 0);
            //ZoomPort.SetValue(0, 1);
            //ZoomPort.SetValue(0, 2);
            //ZoomPort.SetValue(0, 3);

            //// Reset index of scrap selected by moving gripper
            //idxScrap = -1;
            //pnl_OpenTK.Refresh();

            //// Se ho evidenziato uno SCRAP , annullo l'evidenziazione.
            //if (IdsScrapDisablePath[0] != -1)
            //{
            //    int identifiedScrap = CadSource.IdToIdx_Scrap(IdsScrapDisablePath[0]);

            //    if (CadSource.scraps[identifiedScrap].GripExists())
            //    {
            //       CadSource.scraps[identifiedScrap].Enabled = ScrapAbilitation.Enabled;     // Disable clicked scrap
            //    }
            //    else
            //    {
            //       CadSource.scraps[identifiedScrap].Enabled = ScrapAbilitation.WithoutGrip;     // Disable clicked scrap
            //    }
            //}
            //numScrap = 0;
            //IdsScrapDisablePath = new List<int>() { -1, -1 };

        }
        else
        {
            (sender as ToolStripButton).Checked = false;
            (sender as ToolStripButton).BackColor = Color.Transparent;

            sender_old = new object() { };
            if (Application.OpenForms.OfType<frm_Protection>().Any())
            {

                Application.OpenForms.OfType<frm_Protection>().First().Close();
                Properties.Settings.Default.Save();

            }


            if (Application.OpenForms.OfType<Frm_ObjectInfo>().Any())
            {
                Application.OpenForms.OfType<Frm_ObjectInfo>().First().Close();
                Properties.Settings.Default.Save();
            }

            if (Application.OpenForms.OfType<Frm_LayersManage>().Any())
            {
                Application.OpenForms.OfType<Frm_LayersManage>().First().Close();
                Properties.Settings.Default.Save();
            }

            gbx_SelectGrip.Visible = false;

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

        SelectedPoints.Clear();
        MousePointList.Clear();
        myIDs.Clear();
        IdxPointsEnt.Clear();
        RatiosLines.Clear();
        CurrentProject.cuts_tmp.Clear();
        IDAddedCutList.Clear();
        ZoomPort.SetValue(0, 0);
        ZoomPort.SetValue(0, 1);
        ZoomPort.SetValue(0, 2);
        ZoomPort.SetValue(0, 3);

        // Reset index of scrap selected by moving gripper
        idxScrap = -1;
        pnl_OpenTK.Refresh();

        // Se ho evidenziato uno SCRAP , annullo l'evidenziazione.
        if (IdsScrapDisablePath[0] != -1)
        {
            int identifiedScrap = CurrentProject.IdToIdx_Scrap(IdsScrapDisablePath[0]);

            if (CurrentProject.scraps[identifiedScrap].GripExists())
            {
                CurrentProject.scraps[identifiedScrap].Enabled = ScrapAbilitation.Enabled;     // Disable clicked scrap
            }
            else
            {
                CurrentProject.scraps[identifiedScrap].Enabled = ScrapAbilitation.WithoutGrip;     // Disable clicked scrap
            }
        }
        numScrap = 0;
        IdsScrapDisablePath = new List<int>() { -1, -1 };
  }

单击按钮时选择的表单是我将控件箱设置为false的表单,因为我不希望用户能够关闭表单而不再单击已单击打开它的按钮,但我发现问题是因为如果用户以这种方式关闭表单(见下图),按钮的状态仍保持选中但表格已手动关闭

this is how the user can close the from manually

为了解决这个问题,我想添加这个与关闭我的表单的事件关联的方法这是代码

 private void frm_Protection_FormClosed(object sender, FormClosedEventArgs e)
    {
        ////// if user closes manually the window without using the button I have to change the state of the button

       Frm_Main f = new Frm_Main();
        f = Application.OpenForms.OfType<Frm_Main>().Last();
        f.tlsBut_Protection.Checked = false;

    }

使用此代码如果我手动关闭窗体状态按钮,它将在主窗体中再次变为false 但我发现它给我的程序带来了一些问题,其中一个问题就是关闭表单后如果我再次单击按钮,似乎没有调用与click事件相关的方法而且表单没有打开我有在它再次工作之前点击它两次。

你知道为什么我做错了吗?

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

要阻止表单关闭,您可以订阅{<1}}事件,该事件会在表单关闭之前触发,并拦截(并取消)用户的操作。

FormClosing

不幸的是,单击“X”(在任务栏预览中或在程序本身中)并调用private void frm_Protection_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) e.Cancel = true; } 都被视为“UserClosing”关闭原因,因此我们需要稍微修改它。

this.Close()添加属性,并使用该属性确定是否可以关闭表单:

frm_Protection

然后,当您要允许关闭表单时,只将属性设置为public bool CanClose { private get; set; } private void frm_Protection_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = !CanClose; }

true