9 Replies - 1382 Views - Last Post: 16 July 2014 - 09:56 AM Rate Topic: -----

#1 rfresh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 146
  • Joined: 23-August 12

Why does MessageBox call create a new form?

Posted 15 July 2014 - 04:41 PM

I have a form that has been loaded and I click on a quit link to terminate my app but then I display a messageBox to ask the user if they want to save a file before quitting. When the messageBox pops up, another new instance of this form displays. What causes this and how can I prevent it?

Thanks...
Is This A Good Question/Topic? 0
  • +

Replies To: Why does MessageBox call create a new form?

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,178
  • Joined: 12-June 08

Re: Why does MessageBox call create a new form?

Posted 15 July 2014 - 05:14 PM

Throw a breakpoint and see what happens when you step through it.
Was This Post Helpful? 0
  • +
  • -

#3 rfresh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 146
  • Joined: 23-August 12

Re: Why does MessageBox call create a new form?

Posted 15 July 2014 - 05:19 PM

I found out that this piece of code was causing the problem:

MainForm mainForm = new MainForm();
mainForm.Show();



The odd thing (to me anyway) is that this isn't the form that I'm quitting the app from. I have another form that I quit from and it called that code to quickly display the mainForm so I could grab its window parameters to save in Settings.settings but when the form displayed, it had the other form components on it, not the mainForm components.

I remarked out those two lines and the 'ghost' form popping up went away.

I don't understand what is happening here.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,178
  • Joined: 12-June 08

Re: Why does MessageBox call create a new form?

Posted 15 July 2014 - 05:23 PM

It's hard to guess without seeing the entire flow of events.

Kudos on finding the issue, though.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Why does MessageBox call create a new form?

Posted 15 July 2014 - 07:46 PM

View Postrfresh, on 15 July 2014 - 06:19 PM, said:

I don't understand what is happening here.


Its your code. You wrote it. What don't you understand about it? Its doing what you told it to do, isn't it?
Was This Post Helpful? 0
  • +
  • -

#6 rfresh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 146
  • Joined: 23-August 12

Re: Why does MessageBox call create a new form?

Posted 15 July 2014 - 08:14 PM

No, I don't think its doing what I told it to do:

The code below should not display my second form components, yet it does. This is my MainForm, yet when it shows, my CheckForm (the second form) components are on it. I don't understand why that is.

MainForm mainForm = new MainForm();
mainForm.Show();


This post has been edited by rfresh: 15 July 2014 - 08:22 PM

Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Why does MessageBox call create a new form?

Posted 16 July 2014 - 04:01 AM

Quote

No, I don't think its doing what I told it to do:


Computers always do what you tell them to do. They can ONLY ever do what you tell them to do. They can do nothing else. I guarantee that you did not tell the computer to do 'A' and it said '"Screw you programmer, I'm going to do B*'

So, its doing what you told it to do. If you aren't getting the expected behavior, you have to re-think your plan/process and tell it something new to do.

Quote

The code below should not display my second form components, yet it does. This is my MainForm, yet when it shows, my CheckForm (the second form) components are on it. I don't understand why that is.


There isn't enough code for us to tell you. But I can take some guesses.
  • "MainForm" actually is the CheckForm, and you've gotten confused about which contains what.
  • "MainForm" has code on it that hides itself and shows CheckForm, upon the creation of a new MainForm instance.
  • This isn't really the line that is opening your CheckForm instance, and you've gotten lost as to which line is really executing.


Debugging isn't glamorous or exciting. It is slow and methodical. All you really need to do is be patient. You put a breakpoint on a line and follow execution one line at a time using the F10 key. Eventually you'll find the line that performs the action in question - then you have this 'ah ha' moment when you realize how it *is* doing what you told it, only there is an interaction that you didn't plan for.

Any time someone says "I don't know what my program is doing xyz" I know its time to point them at the debugging tutorials. It is worth spending a couple hours on how to debug rather than spend 20 hours scratching your head.

See FAQ # 2 below

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 6: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2014
Spoiler

This post has been edited by tlhIn`toq: 16 July 2014 - 04:03 AM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,229
  • Joined: 12-December 12

Re: Why does MessageBox call create a new form?

Posted 16 July 2014 - 08:05 AM

Quote

Debugging isn't glamorous or exciting.

I think it is.. I must be doing it wrong ;)
Was This Post Helpful? 1
  • +
  • -

#9 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,178
  • Joined: 12-June 08

Re: Why does MessageBox call create a new form?

Posted 16 July 2014 - 08:23 AM

Heh.. do you put on giant heels, a wig, and a nice pair of fishnets too while debugging? Not only do I make it look glamorous, but I make it look FABULOUS!
Was This Post Helpful? 1
  • +
  • -

#10 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,229
  • Joined: 12-December 12

Re: Why does MessageBox call create a new form?

Posted 16 July 2014 - 09:56 AM

I put on a hat :pimp:, it didn't occur to me to take this further ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1