Works in one project but not another. [modified]
-
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.
-
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.
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.
-
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.
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.
-
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.
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:
-
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:
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.
-
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.
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.