I just discovered unit test projects in Visual Studio .NET and am using test methods to set up examples of global code I have developed.
Some global methods involve displaying reusable dialogs like a date-picker and input box. Trouble is, sometimes forms will display and sometimes they won't.
I thought it was related to modality, because I have a report preview form that can be shown modally or non-modally. When I show that non-modally, it does not display. When I show it modally, it does.
Trying my input box never works:
string input = "";
using (InputBox box = new InputBox(Title, Prompt, Default))
{
DialogResult result = box.ShowDialog();
input = box.txtInput.Text.Trim();
}
return input;
Execution stops at the "box.ShowDialog()" line, and at that point I can inspect box and see that its dimensions, location, and visibility are all properly configured -- yet I cannot see the form. I've got to cancel the test to stop everything.
I'd love to use a unit testing project to act as a playground and showcase of existing code, but it seems very limited if I can't display certain forms. I realize this is not really what unit testing is meant for, but I hoped I could build a fun little sandbox this way to help get my developers up to speed.
Press F5 to build and run the application. Click on the button in the main form to display the sub form. Now, when you press the button in the sub form, the form will be hidden.
Load: This event occurs before a form is displayed for the first time. VisibleChanged: This event occurs when the Visible property value changes. Activated: This event occurs when the form is activated in code or by the user.
Just call Show method on the instance. You can check Form. Visible property to check if the form open at the moment.
With the Windows Forms PictureBox control, you can load and display a picture on a form at design time by setting the Image property to a valid picture.
I finally had some consistent success (and lack thereof) based on a single form property: ShowInTaskbar.
When a form had that property set to true, such forms would NOT display from a unit test method. When that property is false, all forms display. So, here are the rules as far as I know them to make sure a form can display from a unit test:
This has let me display and test all of my utility forms like date selectors and login screens.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With