Results 1 to 5 of 5

Thread: error appearing randomly

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474

    error appearing randomly

    I have tow forms(form1,form2) and want to show form2 from form1 as dialog form and hide form1 while form2 is shown. It seems easy:
    Code:
    dim frm as form=new form2
    Me.Hide
    frm.ShowDialog
    BUT, the problem is that form2 takes quite a long time to load cause its filled up with lots of controls, so if i use the above method form1 will hide immediately and leaves the user wondering what has happened to the program. I can think of two solutions:
    First: Change the windows(not the application) cursor to 'waitcursor' to show the user that something is happening (I really dont know how ).

    Second: Hide form1 when form2 load is finished. In order to do this i put the following code in the from2_load sub:

    Code:
     Private Sub from2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ...
        Me.ParentForm.ActiveForm.Hide()
    End Sub
    or as an alternative:

    Code:
     Private Sub from2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ...
       dim frm as Form=form2.ActiveForm
       frm.Hide()
    End Sub
    Both methods work but they stop RANDOMLY !!! with the error : Object Refrence not set to an instance .....

    Whats wrong with it? and if it is totaly wrong why it does not make error every time?

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I tried this , I worked fine.
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, _
    2.     ByVal e As System.EventArgs) Handles Button1.Click
    3.         Dim frm2 As New Form2()
    4.         Me.Hide()
    5.         frm2.Show()
    6.     End Sub

  3. #3
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Add an argument to the constructor of the dialog form, something like this....

    Code:
        Dim frmMain As Form
        Public Sub New(ByVal frm As Form)
            MyBase.New()
    
            frmMain = frm
            'This call is required by the Windows Form Designer.
            InitializeComponent()
            'Add any initialization after the InitializeComponent() call
    
        End Sub
    Send the Mainform to the constructor when showing the form....

    Code:
    Dim frm As New Form2(Me)
    frm.ShowDialog()
    Then hide the mainform as the last you do in the load event of the dialog.

    Code:
    frmMain.Hide()

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I agree with Athley but you can use the Owner property of the form instead of making your own. Here is how I'd do it:

    VB Code:
    1. 'FORM2 CODE
    2.     Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.         'this will help since the app still kind of stalls while loading
    4.         Me.Cursor = Cursors.WaitCursor
    5.     End Sub
    6.  
    7.     Private Sub Form2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
    8.         Static AlreadyRan As Boolean
    9.         'make sure it only fires once
    10.         If Not AlreadyRan Then
    11.             Me.Owner.Hide()
    12.             Me.Cursor = Cursors.Default
    13.             AlreadyRan=True
    14.         End If
    15.     End Sub
    16.  
    17.     Private Sub Form2_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    18.         'bring back old form
    19.         Me.Owner.Show()
    20.     End Sub
    21.  
    22. 'FORM1 CODE
    23.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    24.         'show busy
    25.         Me.Cursor = Cursors.WaitCursor
    26.         Dim frm As New Form2()
    27.         'pass me as the owner
    28.         frm.ShowDialog(Me)
    29.         'remove busy
    30.         'note cursor for this form is not shown while the form is hidden
    31.         Me.Cursor = Cursors.Default
    32.     End Sub

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Thank you all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width