2015-05-11 75 views
-2

這是我的問題:我有一個主窗體,其中包含一些按鈕的面板,當用戶單擊按鈕時,表單打開(我有一些按鈕並單擊這些按鈕,用戶可以打開不同的窗體)用戶再次點擊相同的按鈕,他可以關閉表單。 這就是我所做的:c#在關閉表單後更改按鈕的狀態?

在主窗體中我有一個方法,當用戶點擊其中一個按鈕時調用該方法,該方法檢查與該按鈕相關的文本以決定哪個按鈕點擊。一旦我發現哪個按鈕被點擊了,它就會啓動與按鈕相關的表單。

這是代碼

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

被opned點擊按鈕的形式是,我已經設置了控制盒的虛假形式,因爲我不希望用戶能夠關閉形式,而不點擊再次點擊打開它的按鈕,但我發現一個問題,因爲如果用戶以這種方式關閉窗體(請參閱下圖),該按鈕的狀態仍然被檢查,但窗體已被手動關閉

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; 

    } 

與此代碼在主要形式 ,但我發現它導致我的程序的一些問題,其中一個問題是,在關閉表單後,如果我再次點擊按鈕,似乎與點擊事件關聯的方法不會被調用,表單沒有打開我不得不再次點擊它兩次,然後再次運行。

你知道我爲什麼做錯了嗎?

感謝您的幫助

回答

3

爲了防止關閉表單,您可以訂閱FormClosing事件,觸發窗體關閉之前,和攔截(並取消)用戶的動作。

private void frm_Protection_FormClosing(object sender, FormClosingEventArgs e) 
{ 
    if (e.CloseReason == CloseReason.UserClosing) 
     e.Cancel = true; 
} 

不幸的是,點擊「X」(在任務欄預覽或程序本身),並調用this.Close()都被視爲「UserClosing」關閉的原因,所以我們需要稍微修改它。

屬性添加到frm_Protection,並用它來確定表單是否可以關閉:

public bool CanClose { private get; set; } 

private void frm_Protection_FormClosing(object sender, FormClosingEventArgs e) 
{ 
    e.Cancel = !CanClose; 
} 

然後當你希望允許的形式被關閉屬性設置爲僅true

var frmP = Application.OpenForms.OfType<frm_Protection>().FirstOrDefault(); 

if (frmP != null) 
{ 
    frmP.CanClose = true; 
    frmP.Close(); 
} 
+0

嗨謝謝你的答案,但它不起作用,因爲這樣我就無法關閉它,即使當我想要 –

+0

我明白你在說什麼。更新了答案。 –