ObjectSharp Blogs

You are currently viewing

Dave Lloyd's 2 Cents

A .NET Developer's Perspective


Closing a form before it shows

I'm working on a smart client application at the moment. One of the forms, under certain conditions, launches a wizard to gather information from the user.

From the users perspective they open the form and the wizard displays. If the user hits cancel, I want the wizard to close and the form not to show.

So what is the best way to do this. There is no cancel in the FormLoad event. If you try to close in formload an exception is raised:

Cannot call Close() while doing CreateHandle().

I asked Google and found one solution. Controls have a Public Event, VisibleChanged that fires when the visible property is changed. In this event you can call the forms Close method.

This works fine with one side effect, the form shows for a split second then closes.

This will suffice, but if anyone knows a better way to do this please let me know.

 

 

Comments

  • dave May 5, 2004 9:41 AM


    It's kind of a hack but you could start the form with the Opacity set to 0 and then it won't be visible to the user when it "shows" for that split secondbefore it closes.

  • dave November 24, 2004 1:43 AM

    Sorry sir this never works

  • dave November 24, 2004 9:43 PM

    I have it working see below.

    In the FormLoad event set the forms Opacity = 0

    Create a boolean property (in this example it's CancelSourcing) on the form and set it to true if you want the form to close without displaying.

    Then in the VisibleChanged event write the following code.

    If Me.CancelSourcing Then
    Me.OnMadeClean(Me, New EventArgs)
    Me.Close()
    Else
    Me.Opacity = 1
    End If

  • dave April 24, 2006 10:59 AM

    hi
    i have the same problem...
    i have tryed this code, but what is Me.OnMadeClean(Me, New EventArgs) ??

    tnx!

  • dave April 24, 2006 11:06 AM

    Sorry OnMadeClean is a method that makes sure the form is not Dirty in the framework. It makes sure there are no changes pending. You can ignore it.

Leave a Comment

(required) 
(optional)
(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS