accessing form controls in Vb.net

D

Deepa Yamini S

Hi all,

I have created two forms namely form1 and form2 in my application. I
created a button "start" in form1. When this button is clicked form2
is initialised as a child object and an instance of form2 is displayed
at the same time hiding form1 from the user. In form2 I have an "exit"
button, which when clicked invokes "me.close()". This closes form2
which is active. but still the program is not stopped, Because form1
was the startup object and it is been hidden. So the hidden form1 is
alive and hence the program does not stop and the user is not seeing
anything on his screen.Please tell me a way to hide form1 when form2
is invoked and at the same time when form2 is closed the entire
program should be closed.

With smiles,
Deepa.
 
I

Imran Koradia

One option is to call Application.Exit in the Closed event of Form2 - but
thats more of a forced shutdown than an elegant one. The other option is to
pass a reference of Form1 to the constructor of Form2 and then in the Closed
event of Form2, call the Close method of Form1. Here's how:

' Form1 Code
Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnStart.Click
Dim frm2 As New Form2(Me)
Me.Hide()
frm2.Show()
End Sub

' Form2 Code
Private frm1 As Form1

' note the changed constructor of Form2
Public Sub New(ByVal frm As Form1)
MyBase.New()
frm1 = frm
'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub

Private Sub Form2_Closed(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Closed
frm1.Close()
End Sub


That should pretty much do it..

hope that helps..
Imran.
 
D

deepa yamini sunder ananda kumar

Hi Imran,

Thanks for your reply. I tried working with the second way of doing it
through construtors, but it gave me an error "object reference not set
to an instance of an object" in form2. what do you think the problem
is"?

With smiles,
deepa.
 
I

Imran Koradia

hmm..strange..seems to work for me. Could you post the code you have and
where the problem is occuring?

Imran.
 
D

deepa yamini sunder ananda kumar

Hi Imran,

Thanks for your prompt reply. I have pasted the code below:

form1:

Public Class Form1
Inherits System.Windows.Forms.Form
Dim childfrm2 As Form2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
childfrm2 = New Form2
Me.Hide()
childfrm2.Show()
End Sub
End Class

form2:

Public Class Form2
Inherits System.Windows.Forms.Form
Private frm1 As Form1
Public Sub New(ByVal frm As Form1)
MyBase.New()
InitializeComponent()
frm1 = frm
Dim i As Integer
i = 10
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub Form2_Closed(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Closed
frm1.Close()*** (error occured in this statement)
(error is : object reference not set to an instance of an object.
"system.nullreference exception")
End Sub

End Class

Let me know what was the mistake I made. I am pretty new to VB, and I am
sure it should be a minor mistake somewhere.

With smiles,
Deepa.
 
I

Imran Koradia

Deepa,
Public Class Form1
Inherits System.Windows.Forms.Form
Dim childfrm2 As Form2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

----->> childfrm2 = New Form2
here's where your problem lies. Since you didnt get a compile time error, I
believe you added another Sub New to Form2, right? Meaning you now have two
overloaded constructors - one with no arguments and one taking a reference
to Form1 as an argument. You should be using the one with the argument (as I
had in the code I posted) and pass a reference to your Form1 to Form2. Since
you were not passsing the reference, the variable frm1 in Form2 below was
still Nothing which is why you blew up when trying to call Close on frm1.
Just replace the above line with:

childfrm2 = New Form2(Me)

and that should solve your problem.
Me.Hide()
childfrm2.Show()
End Sub
End Class

form2:

Public Class Form2
Inherits System.Windows.Forms.Form
Private frm1 As Form1
Public Sub New(ByVal frm As Form1)
MyBase.New()
InitializeComponent()
frm1 = frm
Dim i As Integer
i = 10
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub Form2_Closed(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Closed
frm1.Close()*** (error occured in this statement)
(error is : object reference not set to an instance of an object.
"system.nullreference exception")
End Sub

End Class


hope that helps..
Imran.
 
D

deepa yamini sunder ananda kumar

Hi Imran,

Thanks a lot. It worked fine.

With smiles,
Deepa.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top