c# Change the status of a button after closing a form? -


this problem: have main form have panel contains buttons, when user clicks button form opened.( have buttons , clicking these buttons user can open different forms )if user click again same button can close form. do:

in main form have method invoked when 1 of these buttons clicked user, method checks text associated button in order decide button clicked. once have discovered button has been clicked launches form associated button.

this code

private void tlstbtn_click(object sender, eventargs e) {         //// loop through items in toolstrip         //foreach (object item in toolstripmain.items)         //{         //    // if item toolstripbutton object, check         //    if (item toolstripbutton)         //    {         //        // cast item toolstripbutton object , check if sender of event looked @ button in loop         //        toolstripbutton button = (toolstripbutton)item;         //        button.checked = (button == sender);         //    }         //}          foreach (toolstripitem item in this.verticaltoolbox.items)         {             if ((item != sender) &&                 (item toolstripbutton))             {                 ((toolstripbutton)item).checked = false;             }         }          if (sender_old != sender)         {             sender_old = sender;             if ((sender 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 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 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 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 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 toolstripbutton).checked = false;             (sender 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 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 };   } 

the forms opned clicking buttons forms have set controlbox @ false because don't want user able close forms without clicking again button has clicked open it, have found problem because if user closes form in way (see picture below) status of button remains checked form has been closed manually

this how user can close manually

to solve problem have thought add method associated event closing of forms code

 private void frm_protection_formclosed(object sender, formclosedeventargs e)     {         ////// if user closes manually window without using button have change state of button         frm_main f = new frm_main();         f = application.openforms.oftype<frm_main>().last();         f.tlsbut_protection.checked = false;      } 

with code if close manually form status of button becomes false again in main form have discovered causes problems program , 1 of problem after closing form if click again button seems method associated clicking event not called , form not opened have click twice before works again.

do know why do in wrong way???

thanks help

to prevent form closing, can subscribe formclosing event, fires before form closes, , intercept (and cancel) user's action.

private void frm_protection_formclosing(object sender, formclosingeventargs e) {     if (e.closereason == closereason.userclosing)         e.cancel = true; } 

unfortunately, clicking "x" (in taskbar preview or in program itself) , calling this.close() both treated "userclosing" close reasons, need modify slightly.

add property frm_protection, , use determine whether form can closed:

public bool canclose { private get; set; }  private void frm_protection_formclosing(object sender, formclosingeventargs e) {     e.cancel = !canclose; } 

and set property true when want allow form closed:

var frmp = application.openforms.oftype<frm_protection>().firstordefault();  if (frmp != null) {     frmp.canclose = true;     frmp.close(); } 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -