﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » .NET Forums » General .NET Discussion  » Any way to easily cancel a Winform from opening?</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Wed, 10 Jun 2026 20:36:16 GMT</lastBuildDate><ttl>20</ttl><item><title>Any way to easily cancel a Winform from opening?</title><link>http://forum.strataframe.net/FindPost5618.aspx</link><description>How can I cancel the creation or opening of a form, from within the form?  I.e. I'd like to validate the configuration of the form and if it fails, cancel the opening of the form and I'd like to throw an exception. This is mostly so the form is opened correctly by code (not the user).  The validation might be done in a constructor or in the load event handler. Is there any way to do that?</description><pubDate>Mon, 08 Jan 2007 13:43:59 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Any way to easily cancel a Winform from opening?</title><link>http://forum.strataframe.net/FindPost5686.aspx</link><description>Yes, the method will be thrown from within the Show() or ShowDialog().&amp;nbsp; You are correct :)</description><pubDate>Mon, 08 Jan 2007 13:43:59 GMT</pubDate><dc:creator>StrataFrame Team</dc:creator></item><item><title>RE: Any way to easily cancel a Winform from opening?</title><link>http://forum.strataframe.net/FindPost5685.aspx</link><description>In the second case I would also need to use a try/catch block on the .Show or .ShowDialog call, right?</description><pubDate>Mon, 08 Jan 2007 13:41:29 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Any way to easily cancel a Winform from opening?</title><link>http://forum.strataframe.net/FindPost5683.aspx</link><description>Hrm... yes, I did miss this one... thanks for the bump.&lt;/P&gt;&lt;P&gt;Your best bet would be to add your validation code within the constructor of the form right after the InitializeComponent() method.&amp;nbsp; That way, everything is initialized (controls and form components).&amp;nbsp; If you throw an exception here, then you can use a try/catch around the creation of the object (when you call New MyForm()) to make sure it is created correctly.&amp;nbsp; &lt;/P&gt;&lt;P&gt;If you want to try and stop the form during the Show(), and not the instantiation of the object, then you'll want to override the SetVisibleCore() method of the form.&amp;nbsp; Do your validation at the top of the method and throw an exception if the form is not valid; then, at the end of the method, make sure you call MyBase.SetVisibleCore() or the form will never show.&amp;nbsp; This is the method that we used to prevent a form from showing when the user is denied security privilages.&lt;/P&gt;&lt;P&gt;You don't have to do both, just do the one that fits your needs the best.</description><pubDate>Mon, 08 Jan 2007 13:31:37 GMT</pubDate><dc:creator>StrataFrame Team</dc:creator></item><item><title>RE: Any way to easily cancel a Winform from opening?</title><link>http://forum.strataframe.net/FindPost5681.aspx</link><description>Did you guys miss this one?</description><pubDate>Mon, 08 Jan 2007 13:04:27 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item></channel></rss>