Windows Form Won't Display in Debug Mode
Solution 1
Not sure why this solution works, but I was able to solve this issue in VS2013 by setting the visible property on the form I was trying to display to true and then false before calling ShowDialog.
VB.Net example code
Dim form as Form = new Form
form.Visible = True
form.Visible = False
form.ShowDialog
Solution 2
I was able to get the form to display using the following code instead of ShowDialog. I still have no idea why ShowDialog wasn't working but this does the trick:
InitializeForm initForm = new InitializeForm();
initForm.Visible = true;
initForm.Focus();
Application.Run(initForm);
Solution 3
Most likely a exception is happening during the initialization, Go in to the Debug->Exceptions
dropdown menu and be sure the checkbox thrown
for Common Language Runtime Exceptions
is checked, this will let your code break on the exception that is happening.
If you are still not catching the exception go to Debug->Option and Settings
then uncheck the box for Enable Just My Code
and check the box for Break when exceptions cross AppDomain or managed/native boundries
This may give you some "read herring" exceptions, as some .NET processes use exceptions for control of flow logic. So just be aware that the first exception you see may not be the cause of your problem.
user2635491
Updated on June 05, 2022Comments
-
user2635491 almost 2 years
I recently upgraded to VS 2012. I have a set of coded UI tests that I've coded in VS 2010 and I'm trying to spin them up in VS 2012. I have a windows form that I'm displaying at the beginning of the test run by using the AssemblyInitialize attribute. I use this form to allow users to select from sets of values and those values are used to data feed the tests. Here's a copy of my code that displays the form:
[AssemblyInitialize] public static void AssemblyInitialize(TestContext context) { ProcessUtility.TerminateAll(); if (!File.Exists(Directory.GetCurrentDirectory() + @"\RunInfo.ser")) { InitializeForm initForm = new InitializeForm(); initForm.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; initForm.ShowDialog(); } }
So, here's my headache: the form displays just fine in Run mode. However, if I try to spin it up in Debug mode, it never displays. I've stepped through the code. It's loading all of the controls for the form with no errors. I make it to the 'initForm.ShowDialog()' line of code. It runs that line of code but then nothing happens. I don't receive any errors and the status in the lower left of the IDE is 'Ready'. It's almost as if the IDE thinks the form is displayed but it's not. I've double-checked the Task Manager and it's just not there. I've verified the build configuration is set to Debug. I've tried cleaning the solution and re-building. This code continues to work in VS 2010. Please tell me someone out there has ran into a similar problem because I'm out of ideas. I'm new to stackoverflow so let me know if there is anything else I can provide to better explain the issue. Thank you in advance for taking a look at it.
-
user1132959 over 10 yearsYou should call .Dispose() on that form. Won't fix the problem but still necessary.
-
csrowell about 6 yearsPossible duplicate of Form.ShowDialog() does not display window with debugging enabled
-
-
user2635491 almost 11 yearsThanks for your respons. I enabled CLR exceptions to be thrown and changed a couple of my Debugging settings as you recommended. Unfortunately, it didn't throw any exceptions. I step through the initForm.ShowDialog(); line of code and it still just hangs....no exceptions and the form doesn't display. Any other ideas? I really appreciate your help. This is driving me crazy.
-
Scott Chamberlain almost 11 yearsThe only other thing I can suggest is keep a eye on the "output" window of visual studio, it can tell you sometimes that a "first chance exception" was caught somewhere and maybe give you a place to start looking.
-
user2635491 over 10 yearsThanks again, Scott. I was just able to get it to work using Application.Run instead of ShowDialog. This is the only time I've ever had an issue with ShowDialog. Oh well, the form is displaying now.