Activating a form from another form

Viktor

Member
Joined
Feb 7, 2007
Messages
8
Programming Experience
Beginner
Hi people,

I searched the forum for my questions and I found quite a bit similar questions but for some reason I didn't make it work, so I'll present my problem. Hopefully I'll get some explanation.

I have two forms: MainMenu and Aform and I have a friend variable Exists defined in a module.

The sub for calling the AForm from the Main form is
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] btnTypes_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] btnTypes.Click[/SIZE]
[SIZE=2][COLOR=#0000ff]If [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] Exist [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ANewForm [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] frmAForm[/SIZE]
[SIZE=2]Exist = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2]CourseTypeForm.Show()[/SIZE]
[SIZE=2]CourseTypeForm.Activate()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

and the sub for closing the AFORM is
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] btnExit_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] btnExit.Click[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Close()[/SIZE]
[SIZE=2]Exist = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

When I run the MenuForm everythings is well, the AForm starts as it should and I could activate both forms when I want. However, if I close the AForm, and then I try to start it again I get a mesage in line CourseTypeForm.Show() - An unhandled exception of type 'System.ObjectDisposedException' occurred in system.windows.forms.dll
Additional information: Cannot access a disposed object named "frmCourseTypes".

What am I missing here:confused:

Thanks,
Viktor
 
VB.NET:
PrivateSub btnTypes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTypes.Click
If Not Exist Then
Dim ANewForm As New frmAForm
Exist = True
EndIf
CourseTypeForm.Show()
CourseTypeForm.Activate()
EndSub

From the exception it is saying that somewhere in your code you have closed, and therefore disposed, of frmCourseTypes. Before the variable you assigned to this form can be used again a new instance of that form must be declared. Or rather than calling close on that form you can just hide it and then the form will never be disopsed of until close or dispose is called.
 
Back
Top