C# WinForms TabControl Remove Tab and close child controls










0















I have a parent container (form) that initializes a new TabPage with a user control inside of it (called from the Menu Strip). The Tab Control has a Context Menu that, when you right click and select "Close Selected Tab", I call a method that Removes that tab. However, it does not stop the code within that TabPage's user control from running. How can I clean this up so when I close the Tab (TabPages.Remove... etc) that it also closes the user control within that TabPage so that any background code stops executing?



The code below is what closes the tab:
The latter part just selects the next tab to the left after the tab is removed.



 public static void closeCurrentTab(TabControl tc)

int curTabIndex = tc.SelectedIndex;
TabPage tp = tc.SelectedTab;
tc.TabPages.Remove(tp);
if(curTabIndex > 0)

tc.SelectedTab = tc.TabPages[(curTabIndex - 1)];











share|improve this question






















  • What code do you mean? What events are tab page bound to?

    – DevEstacion
    Nov 16 '18 at 0:56











  • I have a collection of user controls used in this program, each user control loads up inside of a new TabPage within this form. When I call TabPages.Remove(TabPage I want to remove), the code fromthe user control inside this tab page continues executing.

    – Ian Fafard
    Nov 16 '18 at 0:59











  • So oncethe user control is loaded, a new Tab Page is created with the control added to it (TabPage.Controls.Add(New user control)). Removing the tab will take it off of the Tab Control, but leave teh user control running.

    – Ian Fafard
    Nov 16 '18 at 1:02












  • TabPage has a reference to the .Dispose property. Try calling it like tp.Dispose()

    – DevEstacion
    Nov 16 '18 at 1:06











  • Unfortunately I had originally tried this and still did not get the code to stop executing. I added it back in : int curTabIndex = tc.SelectedIndex; TabPage tp = tc.SelectedTab; tc.TabPages.Remove(tp); if (curTabIndex > 0) tc.SelectedTab = tc.TabPages[(curTabIndex - 1)]; tp.Dispose(); MessageBox.Show("Tab removed!"); I ran code within the user control to give me a message box every 5 seconds, which it continues to do so after disposing of the tab.

    – Ian Fafard
    Nov 16 '18 at 1:16
















0















I have a parent container (form) that initializes a new TabPage with a user control inside of it (called from the Menu Strip). The Tab Control has a Context Menu that, when you right click and select "Close Selected Tab", I call a method that Removes that tab. However, it does not stop the code within that TabPage's user control from running. How can I clean this up so when I close the Tab (TabPages.Remove... etc) that it also closes the user control within that TabPage so that any background code stops executing?



The code below is what closes the tab:
The latter part just selects the next tab to the left after the tab is removed.



 public static void closeCurrentTab(TabControl tc)

int curTabIndex = tc.SelectedIndex;
TabPage tp = tc.SelectedTab;
tc.TabPages.Remove(tp);
if(curTabIndex > 0)

tc.SelectedTab = tc.TabPages[(curTabIndex - 1)];











share|improve this question






















  • What code do you mean? What events are tab page bound to?

    – DevEstacion
    Nov 16 '18 at 0:56











  • I have a collection of user controls used in this program, each user control loads up inside of a new TabPage within this form. When I call TabPages.Remove(TabPage I want to remove), the code fromthe user control inside this tab page continues executing.

    – Ian Fafard
    Nov 16 '18 at 0:59











  • So oncethe user control is loaded, a new Tab Page is created with the control added to it (TabPage.Controls.Add(New user control)). Removing the tab will take it off of the Tab Control, but leave teh user control running.

    – Ian Fafard
    Nov 16 '18 at 1:02












  • TabPage has a reference to the .Dispose property. Try calling it like tp.Dispose()

    – DevEstacion
    Nov 16 '18 at 1:06











  • Unfortunately I had originally tried this and still did not get the code to stop executing. I added it back in : int curTabIndex = tc.SelectedIndex; TabPage tp = tc.SelectedTab; tc.TabPages.Remove(tp); if (curTabIndex > 0) tc.SelectedTab = tc.TabPages[(curTabIndex - 1)]; tp.Dispose(); MessageBox.Show("Tab removed!"); I ran code within the user control to give me a message box every 5 seconds, which it continues to do so after disposing of the tab.

    – Ian Fafard
    Nov 16 '18 at 1:16














0












0








0








I have a parent container (form) that initializes a new TabPage with a user control inside of it (called from the Menu Strip). The Tab Control has a Context Menu that, when you right click and select "Close Selected Tab", I call a method that Removes that tab. However, it does not stop the code within that TabPage's user control from running. How can I clean this up so when I close the Tab (TabPages.Remove... etc) that it also closes the user control within that TabPage so that any background code stops executing?



The code below is what closes the tab:
The latter part just selects the next tab to the left after the tab is removed.



 public static void closeCurrentTab(TabControl tc)

int curTabIndex = tc.SelectedIndex;
TabPage tp = tc.SelectedTab;
tc.TabPages.Remove(tp);
if(curTabIndex > 0)

tc.SelectedTab = tc.TabPages[(curTabIndex - 1)];











share|improve this question














I have a parent container (form) that initializes a new TabPage with a user control inside of it (called from the Menu Strip). The Tab Control has a Context Menu that, when you right click and select "Close Selected Tab", I call a method that Removes that tab. However, it does not stop the code within that TabPage's user control from running. How can I clean this up so when I close the Tab (TabPages.Remove... etc) that it also closes the user control within that TabPage so that any background code stops executing?



The code below is what closes the tab:
The latter part just selects the next tab to the left after the tab is removed.



 public static void closeCurrentTab(TabControl tc)

int curTabIndex = tc.SelectedIndex;
TabPage tp = tc.SelectedTab;
tc.TabPages.Remove(tp);
if(curTabIndex > 0)

tc.SelectedTab = tc.TabPages[(curTabIndex - 1)];








c# tabcontrol tabpage






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 0:49









Ian FafardIan Fafard

367




367












  • What code do you mean? What events are tab page bound to?

    – DevEstacion
    Nov 16 '18 at 0:56











  • I have a collection of user controls used in this program, each user control loads up inside of a new TabPage within this form. When I call TabPages.Remove(TabPage I want to remove), the code fromthe user control inside this tab page continues executing.

    – Ian Fafard
    Nov 16 '18 at 0:59











  • So oncethe user control is loaded, a new Tab Page is created with the control added to it (TabPage.Controls.Add(New user control)). Removing the tab will take it off of the Tab Control, but leave teh user control running.

    – Ian Fafard
    Nov 16 '18 at 1:02












  • TabPage has a reference to the .Dispose property. Try calling it like tp.Dispose()

    – DevEstacion
    Nov 16 '18 at 1:06











  • Unfortunately I had originally tried this and still did not get the code to stop executing. I added it back in : int curTabIndex = tc.SelectedIndex; TabPage tp = tc.SelectedTab; tc.TabPages.Remove(tp); if (curTabIndex > 0) tc.SelectedTab = tc.TabPages[(curTabIndex - 1)]; tp.Dispose(); MessageBox.Show("Tab removed!"); I ran code within the user control to give me a message box every 5 seconds, which it continues to do so after disposing of the tab.

    – Ian Fafard
    Nov 16 '18 at 1:16


















  • What code do you mean? What events are tab page bound to?

    – DevEstacion
    Nov 16 '18 at 0:56











  • I have a collection of user controls used in this program, each user control loads up inside of a new TabPage within this form. When I call TabPages.Remove(TabPage I want to remove), the code fromthe user control inside this tab page continues executing.

    – Ian Fafard
    Nov 16 '18 at 0:59











  • So oncethe user control is loaded, a new Tab Page is created with the control added to it (TabPage.Controls.Add(New user control)). Removing the tab will take it off of the Tab Control, but leave teh user control running.

    – Ian Fafard
    Nov 16 '18 at 1:02












  • TabPage has a reference to the .Dispose property. Try calling it like tp.Dispose()

    – DevEstacion
    Nov 16 '18 at 1:06











  • Unfortunately I had originally tried this and still did not get the code to stop executing. I added it back in : int curTabIndex = tc.SelectedIndex; TabPage tp = tc.SelectedTab; tc.TabPages.Remove(tp); if (curTabIndex > 0) tc.SelectedTab = tc.TabPages[(curTabIndex - 1)]; tp.Dispose(); MessageBox.Show("Tab removed!"); I ran code within the user control to give me a message box every 5 seconds, which it continues to do so after disposing of the tab.

    – Ian Fafard
    Nov 16 '18 at 1:16

















What code do you mean? What events are tab page bound to?

– DevEstacion
Nov 16 '18 at 0:56





What code do you mean? What events are tab page bound to?

– DevEstacion
Nov 16 '18 at 0:56













I have a collection of user controls used in this program, each user control loads up inside of a new TabPage within this form. When I call TabPages.Remove(TabPage I want to remove), the code fromthe user control inside this tab page continues executing.

– Ian Fafard
Nov 16 '18 at 0:59





I have a collection of user controls used in this program, each user control loads up inside of a new TabPage within this form. When I call TabPages.Remove(TabPage I want to remove), the code fromthe user control inside this tab page continues executing.

– Ian Fafard
Nov 16 '18 at 0:59













So oncethe user control is loaded, a new Tab Page is created with the control added to it (TabPage.Controls.Add(New user control)). Removing the tab will take it off of the Tab Control, but leave teh user control running.

– Ian Fafard
Nov 16 '18 at 1:02






So oncethe user control is loaded, a new Tab Page is created with the control added to it (TabPage.Controls.Add(New user control)). Removing the tab will take it off of the Tab Control, but leave teh user control running.

– Ian Fafard
Nov 16 '18 at 1:02














TabPage has a reference to the .Dispose property. Try calling it like tp.Dispose()

– DevEstacion
Nov 16 '18 at 1:06





TabPage has a reference to the .Dispose property. Try calling it like tp.Dispose()

– DevEstacion
Nov 16 '18 at 1:06













Unfortunately I had originally tried this and still did not get the code to stop executing. I added it back in : int curTabIndex = tc.SelectedIndex; TabPage tp = tc.SelectedTab; tc.TabPages.Remove(tp); if (curTabIndex > 0) tc.SelectedTab = tc.TabPages[(curTabIndex - 1)]; tp.Dispose(); MessageBox.Show("Tab removed!"); I ran code within the user control to give me a message box every 5 seconds, which it continues to do so after disposing of the tab.

– Ian Fafard
Nov 16 '18 at 1:16






Unfortunately I had originally tried this and still did not get the code to stop executing. I added it back in : int curTabIndex = tc.SelectedIndex; TabPage tp = tc.SelectedTab; tc.TabPages.Remove(tp); if (curTabIndex > 0) tc.SelectedTab = tc.TabPages[(curTabIndex - 1)]; tp.Dispose(); MessageBox.Show("Tab removed!"); I ran code within the user control to give me a message box every 5 seconds, which it continues to do so after disposing of the tab.

– Ian Fafard
Nov 16 '18 at 1:16













1 Answer
1






active

oldest

votes


















0














I found the issue. I found that the asynchronous processes within the child control were running astray even after the parent control (in this case being the TabPage) was disposed of.



A simple example of how I solved it is below.



Also, for more complex task chaining , I can introduce cancellation tolkens once the parent becomes null, thus cleaning up whatever needs to be once a user closes a tab hosting that control, but doesnt close the overall application.



using this.Parent and testing whether it is null or not whevenever id like to.



 protected override void OnParentChanged(EventArgs e)

base.OnParentChanged(e);
if(this.Parent == null)

// Clean up





and or this



 private async Task popUp()

do

MessageBox.Show("Im running!");
await Task.Delay(5000);

while (this.Parent != null);






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329904%2fc-sharp-winforms-tabcontrol-remove-tab-and-close-child-controls%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I found the issue. I found that the asynchronous processes within the child control were running astray even after the parent control (in this case being the TabPage) was disposed of.



    A simple example of how I solved it is below.



    Also, for more complex task chaining , I can introduce cancellation tolkens once the parent becomes null, thus cleaning up whatever needs to be once a user closes a tab hosting that control, but doesnt close the overall application.



    using this.Parent and testing whether it is null or not whevenever id like to.



     protected override void OnParentChanged(EventArgs e)

    base.OnParentChanged(e);
    if(this.Parent == null)

    // Clean up





    and or this



     private async Task popUp()

    do

    MessageBox.Show("Im running!");
    await Task.Delay(5000);

    while (this.Parent != null);






    share|improve this answer



























      0














      I found the issue. I found that the asynchronous processes within the child control were running astray even after the parent control (in this case being the TabPage) was disposed of.



      A simple example of how I solved it is below.



      Also, for more complex task chaining , I can introduce cancellation tolkens once the parent becomes null, thus cleaning up whatever needs to be once a user closes a tab hosting that control, but doesnt close the overall application.



      using this.Parent and testing whether it is null or not whevenever id like to.



       protected override void OnParentChanged(EventArgs e)

      base.OnParentChanged(e);
      if(this.Parent == null)

      // Clean up





      and or this



       private async Task popUp()

      do

      MessageBox.Show("Im running!");
      await Task.Delay(5000);

      while (this.Parent != null);






      share|improve this answer

























        0












        0








        0







        I found the issue. I found that the asynchronous processes within the child control were running astray even after the parent control (in this case being the TabPage) was disposed of.



        A simple example of how I solved it is below.



        Also, for more complex task chaining , I can introduce cancellation tolkens once the parent becomes null, thus cleaning up whatever needs to be once a user closes a tab hosting that control, but doesnt close the overall application.



        using this.Parent and testing whether it is null or not whevenever id like to.



         protected override void OnParentChanged(EventArgs e)

        base.OnParentChanged(e);
        if(this.Parent == null)

        // Clean up





        and or this



         private async Task popUp()

        do

        MessageBox.Show("Im running!");
        await Task.Delay(5000);

        while (this.Parent != null);






        share|improve this answer













        I found the issue. I found that the asynchronous processes within the child control were running astray even after the parent control (in this case being the TabPage) was disposed of.



        A simple example of how I solved it is below.



        Also, for more complex task chaining , I can introduce cancellation tolkens once the parent becomes null, thus cleaning up whatever needs to be once a user closes a tab hosting that control, but doesnt close the overall application.



        using this.Parent and testing whether it is null or not whevenever id like to.



         protected override void OnParentChanged(EventArgs e)

        base.OnParentChanged(e);
        if(this.Parent == null)

        // Clean up





        and or this



         private async Task popUp()

        do

        MessageBox.Show("Im running!");
        await Task.Delay(5000);

        while (this.Parent != null);







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 5:19









        Ian FafardIan Fafard

        367




        367





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329904%2fc-sharp-winforms-tabcontrol-remove-tab-and-close-child-controls%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            Evgeni Malkin