locked
Value Close() cannot be called while doing CreateHandle() RRS feed

  • Question

  •  

    Hi,

    I am trying to close and MDI child from the _Load event but getting this error..

    Value Close() cannot be called while doing CreateHandle()


    how can i fix this?


     

    Monday, February 26, 2007 1:44 PM

All replies

  • can you explain it more detail?

    you want is: colse a mdi child form when form_load event occured?

    Tuesday, February 27, 2007 8:34 AM
  •  

    I am trying to check if the reocord was exist in the database but if not then I want to close the form.. here is an example..

     

    private void frmPatientDashboard_Load(object sender, EventArgs e)

    {

    txtFileNo.Text = "";

    txtIDType.Text = "";

    txtIDNumber.Text = "";

    txtBirth.Text = "";

    txtGender.Text = "";

    lblVIP.Visible = false;

    txtBloodGroup.Text = "";

    txtFullName.Text = "";

    lblMobile.Text = "";

    lblResidence.Text = "";

    lblArea.Text = "";

    lblGovernorate.Text = "";

    get_person();

    this.Close();

    }

    Tuesday, February 27, 2007 1:08 PM
  • yes , this.close() can not be used in form load event.

    my suggestion is: why not judge get_person() method beform the MDI created?
    if new MDIchild need to colse, it is no need to generate a new MDIchild instance.

    Thursday, March 1, 2007 9:43 AM
  •  

    what do you mean by (before)? I though Form_Load is the first event? where can I place it?

    Thursday, March 1, 2007 10:18 AM
  • I means judge it when your MDIParent generate MDIChild,

    and just cancel new MDIChild generate.

    I think it can give out  same logic as: generate new child form then judge, then close it 

    Thursday, March 1, 2007 10:41 AM
  • Hi,
    I know its quite late now but this post still comes up in google so here it is. Instead of calling me.close, do this
    Me.BeginInvoke(New MethodInvoker(AddressOf CloseIt))

    private sub CloseIt()
     me.close
    end sub
    • Proposed as answer by SooperMario Thursday, July 13, 2017 11:40 PM
    Friday, September 12, 2008 11:49 AM
  • Thanks CodeCruiser!  Never too late to post a better solution.  Otherwise, I would've had to move all my Form.Load Event code (except Button Enabling code) to the New Method and/or a separate init Method that I'd have explicitly call prior to each initial Show Method call.  It should've probably been designed that to begin with but I inherited dozens of Forms that don't.

    FYI, this only became an issue when I moved setting of the MdiParent to my main Form from the Load Event to the New Method which I had to do to fix the Form not Focusing when you click on a MenuStrip on the Form or any other non-Title Bar portion for that matter which was fixed by a WndProd Override but which is also no longer needed if MdiParentis set in the New Method.
    Friday, January 22, 2010 2:56 PM
  • I packaged CodeCruiser's solution into a form (get it?) more easily used across multiple Forms.  Place the following code in a shared module.  Then you can simply replace "Me.Close"'s with "Me.CloseFrmDuringLoad(Me)"'s.

       Public oFrmToClose As Form = Nothing

        ''' <summary>
        ''' Parameter-less Delegate to be Invoke'd by CloseFrmDuringLoad.  Closes Global.oFrmToClose
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub CloseFrm()
            oFrmToClose.Close()
        End Sub

        ''' <summary>
        ''' Allows closing of a specified during its Load Event.  Needed if its MDIParent is set prior to the Load Event.
        ''' </summary>
        ''' <param name="oFrm"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function CloseFrmDuringLoad(ByVal oFrm As Form) As IAsyncResult
            oFrmToClose = oFrm
            Return oFrm.BeginInvoke(New MethodInvoker(AddressOf CloseFrm))
        End Function

    Friday, January 22, 2010 6:07 PM
  • Hi,
    I know its quite late now but this post still comes up in google so here it is. Instead of calling me.close, do this
    Me.BeginInvoke(New MethodInvoker(AddressOf CloseIt))

    private sub CloseIt()
     me.close
    end sub
    I get same exception when I trying this solution. Why should it work any way? You are attempting to call same Close() method, only using invoker. The form might be still not loaded when the Close method is called.
    Thursday, May 3, 2012 7:29 PM
  • Hi,

    Bogdan Verbenets

    It's work for me fine.

    thanks.

    Thursday, August 9, 2012 9:24 AM
  • Thank you so much it's working perfectly
    Saturday, January 18, 2014 4:42 PM
  • This is really a funny thread.

    1. Somebody who programs in C# asks a question in 2007
    2. In 2010 he gets a reply but now with 3 rows of VB code
    3. In 2010 a discussion gets a follow up by another one
    4. In 2012 somebody else get the same exception but in probably a complete different situation
    5. now in 2013 somebody thanks somebody and tells it works perfect

    Maybe better an Moderator moves it to off thread, maybe not the question itself, but in my idea the replies.

    If not then we have the chance once a moderator marks a complete wrong reply as answer.



    Thanks
    Cor


    Saturday, January 18, 2014 5:16 PM
  • Maybe very late.....

    But, How about in the frmPatientDashboard_VisibleChanged??

    Also, you have to change the code from "this.Close()"  to "Me.Close()"

    Monday, December 19, 2016 7:07 AM
  • and in 2017 codecruiser's solution still saves the day! good job!
    Thursday, July 13, 2017 11:41 PM
  • lol fast forward to 2017...code cruiser did it again!
    Thursday, July 13, 2017 11:42 PM