|
-
Dec 17th, 2002, 11:46 AM
#1
Thread Starter
Frenzied Member
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?
-
Dec 17th, 2002, 11:51 AM
#2
Sleep mode
I tried this , I worked fine.
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim frm2 As New Form2()
Me.Hide()
frm2.Show()
End Sub
-
Dec 17th, 2002, 11:58 AM
#3
Registered User
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.
-
Dec 17th, 2002, 12:07 PM
#4
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:
'FORM2 CODE
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'this will help since the app still kind of stalls while loading
Me.Cursor = Cursors.WaitCursor
End Sub
Private Sub Form2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
Static AlreadyRan As Boolean
'make sure it only fires once
If Not AlreadyRan Then
Me.Owner.Hide()
Me.Cursor = Cursors.Default
AlreadyRan=True
End If
End Sub
Private Sub Form2_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
'bring back old form
Me.Owner.Show()
End Sub
'FORM1 CODE
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'show busy
Me.Cursor = Cursors.WaitCursor
Dim frm As New Form2()
'pass me as the owner
frm.ShowDialog(Me)
'remove busy
'note cursor for this form is not shown while the form is hidden
Me.Cursor = Cursors.Default
End Sub
-
Dec 17th, 2002, 12:42 PM
#5
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|