Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Works in one project but not another. [modified]

Works in one project but not another. [modified]

Scheduled Pinned Locked Moved C#
csharpwinformshelpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    twohowlingdogs
    wrote on last edited by
    #1

    I have the following code in one project file. It works just fine and has for a long time:

            if (qrySearchResults.Rows.Count > 0)
            {
                this.Hide();
                SearchResults sr = new SearchResults();
                sr.ShowDialog();
                this.Show();
            }
    

    Then I added this to a new project (and solution) I'm doing and get an error message:

            if (queryResults.Rows.Count > 0)
            {
                this.Hide();
                SearchResults resultsWindow = new SearchResults();
                resultsWindow.ShowDialog();
                this.Show();
            }
    

    I'm essentially doing the same thing yet get an error on the new one saying that it can't access something that has already been disposed of. So I added:

                if (!this.IsDisposed)
                {
                    this.Show();
                }
    

    and now it works. Why would the first project work fine without this additional code but the second one has to have it? [EDIT] I should say that the 'resultsWindow' has two buttons. Either Search Again or Close. If they Close, I close this search criteria window in the results Window and if they choose search again, then I want this to show again so I just close the results window.[/EDIT] I'm using VS2008 in a Winforms project. It is possible the first project was created with VS2005 but not sure. I didn't do it. I promise!

    I have nothing more to say.

    B L 2 Replies Last reply
    0
    • T twohowlingdogs

      I have the following code in one project file. It works just fine and has for a long time:

              if (qrySearchResults.Rows.Count > 0)
              {
                  this.Hide();
                  SearchResults sr = new SearchResults();
                  sr.ShowDialog();
                  this.Show();
              }
      

      Then I added this to a new project (and solution) I'm doing and get an error message:

              if (queryResults.Rows.Count > 0)
              {
                  this.Hide();
                  SearchResults resultsWindow = new SearchResults();
                  resultsWindow.ShowDialog();
                  this.Show();
              }
      

      I'm essentially doing the same thing yet get an error on the new one saying that it can't access something that has already been disposed of. So I added:

                  if (!this.IsDisposed)
                  {
                      this.Show();
                  }
      

      and now it works. Why would the first project work fine without this additional code but the second one has to have it? [EDIT] I should say that the 'resultsWindow' has two buttons. Either Search Again or Close. If they Close, I close this search criteria window in the results Window and if they choose search again, then I want this to show again so I just close the results window.[/EDIT] I'm using VS2008 in a Winforms project. It is possible the first project was created with VS2005 but not sure. I didn't do it. I promise!

      I have nothing more to say.

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      Long variable names break your code! ;) Seriously though, you should use the return value of ShowDialog to decide whether to re-show the form. (Set the result by putting DialogResult properties on the buttons.) Closing it from within resultsWindow will result in the behaviour that you describe because that will happen before ShowDialog returns, so when you call Show, resultWindow has already killed the form. Pulling the rug out from underneath an executing form like that is rude.

      T 1 Reply Last reply
      0
      • B BobJanova

        Long variable names break your code! ;) Seriously though, you should use the return value of ShowDialog to decide whether to re-show the form. (Set the result by putting DialogResult properties on the buttons.) Closing it from within resultsWindow will result in the behaviour that you describe because that will happen before ShowDialog returns, so when you call Show, resultWindow has already killed the form. Pulling the rug out from underneath an executing form like that is rude.

        T Offline
        T Offline
        twohowlingdogs
        wrote on last edited by
        #3

        This is the code I used in the resultsWindow:

            private void cmdClose\_Click(object sender, EventArgs e)
            {
                SearchCriteria sc = (SearchCriteria)Application.OpenForms\["SearchCriteria"\];
                sc.Close();
                SearchCriteria.impDS.Dispose();
                this.Close();
            }
        

        This is the code from the previous project:

                        SearchInput si = (SearchInput)Application.OpenForms\["SearchInput"\];
                        si.Close();
                        SearchInput.ds1.Dispose();
                        this.Close();
        

        So you can see I do the exact same thing and the other code works fine without checking. I agree that there are better ways. Thanks for pointing out checking what ShowDialog returns. I'll look into doing that.

        BobJanova wrote:

        Pulling the rug out from underneath an executing form like that is rude

        I didn't mean to be rude. :^) :laugh:

        I have nothing more to say.

        L 1 Reply Last reply
        0
        • T twohowlingdogs

          This is the code I used in the resultsWindow:

              private void cmdClose\_Click(object sender, EventArgs e)
              {
                  SearchCriteria sc = (SearchCriteria)Application.OpenForms\["SearchCriteria"\];
                  sc.Close();
                  SearchCriteria.impDS.Dispose();
                  this.Close();
              }
          

          This is the code from the previous project:

                          SearchInput si = (SearchInput)Application.OpenForms\["SearchInput"\];
                          si.Close();
                          SearchInput.ds1.Dispose();
                          this.Close();
          

          So you can see I do the exact same thing and the other code works fine without checking. I agree that there are better ways. Thanks for pointing out checking what ShowDialog returns. I'll look into doing that.

          BobJanova wrote:

          Pulling the rug out from underneath an executing form like that is rude

          I didn't mean to be rude. :^) :laugh:

          I have nothing more to say.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          twohowlingdogs wrote:

          So you can see I do the exact same thing and the other code works fine without checking.

          If the behaviour is different, then there "must" be a difference. The check on "this" implies that "this" might be disposed, before the dialog is closed. Are you calling an explicit dispose on that particular form somewhere?

          I are Troll :suss:

          T 1 Reply Last reply
          0
          • L Lost User

            twohowlingdogs wrote:

            So you can see I do the exact same thing and the other code works fine without checking.

            If the behaviour is different, then there "must" be a difference. The check on "this" implies that "this" might be disposed, before the dialog is closed. Are you calling an explicit dispose on that particular form somewhere?

            I are Troll :suss:

            T Offline
            T Offline
            twohowlingdogs
            wrote on last edited by
            #5

            Eddy Vluggen wrote:

            If the behaviour is different, then there "must" be a difference.

            I will not argue with that! I just can't find it! :sigh: I don't think I'm calling a Dispose anywhere else. I'll look into that! What I said I'm doing in the first post is working and working correctly. So I'm not worried, but considering how I am, I would like to find out why!

            I have nothing more to say.

            1 Reply Last reply
            0
            • T twohowlingdogs

              I have the following code in one project file. It works just fine and has for a long time:

                      if (qrySearchResults.Rows.Count > 0)
                      {
                          this.Hide();
                          SearchResults sr = new SearchResults();
                          sr.ShowDialog();
                          this.Show();
                      }
              

              Then I added this to a new project (and solution) I'm doing and get an error message:

                      if (queryResults.Rows.Count > 0)
                      {
                          this.Hide();
                          SearchResults resultsWindow = new SearchResults();
                          resultsWindow.ShowDialog();
                          this.Show();
                      }
              

              I'm essentially doing the same thing yet get an error on the new one saying that it can't access something that has already been disposed of. So I added:

                          if (!this.IsDisposed)
                          {
                              this.Show();
                          }
              

              and now it works. Why would the first project work fine without this additional code but the second one has to have it? [EDIT] I should say that the 'resultsWindow' has two buttons. Either Search Again or Close. If they Close, I close this search criteria window in the results Window and if they choose search again, then I want this to show again so I just close the results window.[/EDIT] I'm using VS2008 in a Winforms project. It is possible the first project was created with VS2005 but not sure. I didn't do it. I promise!

              I have nothing more to say.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              when closing a Form, its survival depends on whether it was shown by calling Show() or ShowDialog(). Maybe that explains the difference you're seeing. :)

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups