winforms - C# code (method) runs when application is closed -


i have button when pressed closes current form , opens new form of same class - i.e. opens new form in original state.

i have button has same functionality, try call function in code when new form opens runs importgantt() function of form.

the problem have when click button closes current form , opens new one, expected, not call importgantt() function until close application.

any ideas?

much appreciated.

private void browsefiletoolstripmenuitem_click(object sender, eventargs e)     {         clearandimport();     }  private void clearandimport()     {         this.hide();         dashboard dashboard = new dashboard();         dashboard.showdialog();         dashboard.importgantt();         this.close();     }  private void importgantt()     {         // edit interface         btnimport.visible = false;         datacapplan.visible = true;         datamilestones.visible = true;         pnlgantt.visible = true;         graphics ganttgraphics = pnlgantt.creategraphics();          // draw axis           // import files         filecapplan.title = "select capital plan file";         filecapplan.filter = "excel workbook (.xlsx)|*.xlsx";         dialogresult rescapplan = filecapplan.showdialog();         if (rescapplan == dialogresult.ok)         {             cnstr = cnstr + filecapplan.filename;         }         else         {             messagebox.show("error: unable import file");         }         filemilestones.title = "select milestones file";         filemilestones.filter = "excel workbook (.xlsx)|*.xlsx";         dialogresult resmilestones = filemilestones.showdialog();         if (resmilestones == dialogresult.ok)         {             cnstr2 = cnstr2 + filemilestones.filename;         }         else         {             messagebox.show("error: unable import file");         }          // use oledb connection import excel data         using (oledbconnection cn = new oledbconnection(@"provider=microsoft.ace.oledb.12.0;data source=" + cnstr + ";extended properties=" + "'excel 12.0 xml;hdr=yes'"))         {             using (oledbdataadapter adapter = new oledbdataadapter(sqlselectall, cn))             {                 adapter.fill(dtcapplan);                 datacapplan.datasource = dtcapplan;                 datacapplan.autoresizecolumns();             }         }         using (oledbconnection cn = new oledbconnection(@"provider=microsoft.ace.oledb.12.0;data source=" + cnstr2 + ";extended properties=" + "'excel 12.0 xml;hdr=yes'"))         {             using (oledbdataadapter adapter = new oledbdataadapter(sqlselectall, cn))             {                 adapter.fill(dtmilestones);                 datamilestones.datasource = dtmilestones;                 datamilestones.autoresizecolumns();             }         }          // draw gantt chart         foreach (datarow rowcapplan in dtcapplan.rows)         {             id = rowcapplan["program id"].tostring();              foreach (datarow rowmilestone in dtmilestones.rows)             {                 if (id == rowmilestone["program id"].tostring())                 {                     // calculate space in days todays date , milestone date                     msdate = convert.todatetime(rowmilestone["milestone date"]);                     mstimespan = msdate - caldate;                     msintdate = (int)mstimespan.totaldays + 1;                     ttimespan = tdate - caldate;                     tintdate = (int)ttimespan.totaldays + 1;                     ganttplotspace = msintdate - tintdate;                      // draw each milestone or gateway not yet complete                     if (rowmilestone["% complete"].tostring() != "100")                     {                         taskname = rowmilestone["task name"].tostring();                         if (taskname == "gateway 1" || taskname == "gateway 2" || taskname == "gateway 3" || taskname == "gateway 4" || taskname == "gateway 5")                         {                             rectangle gw = new rectangle(startx + ganttplotspace, starty - 4, 2, 11);                             ganttgraphics.drawrectangle(gwpen, gw);                             ganttgraphics.fillrectangle(gwbrush, gw);                         }                         else                         {                             rectangle ms = new rectangle(startx + ganttplotspace + 1, starty, 2, 2);                             ganttgraphics.drawrectangle(mspen, ms);                             ganttgraphics.fillrectangle(msbrush, ms);                         }                         ganttgraphics.drawline(linepen, startx - 10, starty - 11, pnlgantt.right, starty - 11);                     }                 }             }             starty = starty + 22;         }         ganttgraphics.drawline(linepen, startx - 10, starty + 11, pnlgantt.right, starty + 11);     } 

image gantt enter image description here

image after clearandimport method (fixed user) enter image description here


as per brij guidance:

okay, guidance works, code follows...

this opens new form , runs import method, however, seems running on loop. i.e. runs displaying gantt, tries run import gantt method again.

bool clear;    public dashboard(bool clear = false)     {         initializecomponent();         datacapplan.columnheadermouseclick += new datagridviewcellmouseeventhandler(datacapplan_columnheadermouseclick);          this.clear = clear;         this.load += new eventhandler(dashboard_load);     }     private void dashboard_load(object sender, eventargs e)     {         if (this.clear)         {             this.importgantt();         }     }  // clear , import method     private void clearandimport()     {         this.hide();         dashboard dashboard = new dashboard();         dashboard.clear = true;         dashboard.showdialog();         this.close();     } 

i think method referring below:

private void clearandimport() {         this.hide();         dashboard dashboard = new dashboard();         dashboard.showdialog();         dashboard.importgantt();         this.close(); } 

you calling dashboard.showdialog(). until close "dashboard" form, next line of code (dashboard.importgantt()) won't called. suggest call importgantt() in constructor or load event of dashboard form. change sequence of code moving dashboard.importgantt() above dashboard.showdialog().


as per comment, suggest, modify constructor of dashboard class accept boolean parameter , make optional (defaulting false). if true passed, call importgantt(). like:

public dashboard(bool clear = false) {    initializecomponent();    if(clear)    {        this.importgantt();    } } 

and clearandimport() method like:

private void clearandimport() {         this.hide();         dashboard dashboard = new dashboard(true);         dashboard.showdialog();         this.close(); } 

as per last comment, try this:

bool clear = false; public dashboard(bool clear = false) {         initializecomponent();         this.clear = clear;         this.load += new eventhandler(dashboard_load); }  void dashboard_load(object sender, eventargs) {     if(this.clear)     {        this.importgantt();     } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -