[Basic] Closing a form, from another form.

Michaelk

Well-known member
Joined
Jun 3, 2004
Messages
58
Location
Australia
Programming Experience
3-5
Hi, i can't seem to work out how i can close Form1, from Form2. I've been hiding the forms, but this isn't very effective.
For example, Once Form1 is complete, it closes itself, and loads Form2.
I am using:
VB.NET:
[size=2][/size][size=2][color=#0000ff]Me[/color][/size][size=2].Hide()[/size]
[size=2][color=#0000ff]Dim[/color][/size][size=2] Form2 [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] Form2[/size]
[size=2]Form2.ShowDialog()
[/size]
But with that, i still have Form1 open in the background.
I'd imagine the answer is fairly basic.
Thanks.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
'make a new module and put this line in:

Private Form1 as Form1
Private Form2 as Form2

'in the code of form1:

Form2 = New Form2
Form2.Show '(Not ShowDialog)
Form1.close

that should do the trick
 

Michaelk

Well-known member
Joined
Jun 3, 2004
Messages
58
Location
Australia
Programming Experience
3-5
Thanks for the reply. I keep getting the following error:
Object reference not set to an instance of an obkect.
Also, thats i was when i put
Private Form1 as Form1
Private Form2 as Form2
into Form1.
If i put it into a new Module, i get the error:
Name 'Form1' is not declared.
Thanks.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
make a class (this class is/will be the startup object) in the new constructor open form1 (form1 = new form1) then in form 1 you have form2=new form2 and ya close form1
 

Michaelk

Well-known member
Joined
Jun 3, 2004
Messages
58
Location
Australia
Programming Experience
3-5
Hi, i've been trying quite a few combinations of what you suggested. But i just can't get it to work! To be more precise, what i've got is a splash screen (frmSplash), and i't runs a timer, once the timer is complete, i want the form to close, and to load the Main form (frmMain). Like i said, i can only get it to hide at the moment.
I havn't really used a class before, how do i make/use one?
Thanks for your help :)
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
ahh, quick solution idea here.

have 2 forms (frmMain, frmStartUp) and a code module:

Module:
Private MainForm as frmMain

Main Form:
module level variables: priavte SplashScreen as frmStartup
frmMain load event:
MainForm = Me 'Sets varible to reference this existing form
SplashScreen = new frmStartUp 'New instance
SplashScreen.Timer1.Enabled=True
SplashScreen.show
me.visible=false

SplashScreen's timer tick event
Mainform.visible=true
me.close

I know this is pretty much all the code ya need, but i was too tired ta run through just the logic, i dont usually post the code to do stuff unless it's a function i already wrote that is (or similar to) what task needs to be accomplished, feel lucky this time ;)
 

Michaelk

Well-known member
Joined
Jun 3, 2004
Messages
58
Location
Australia
Programming Experience
3-5
Thanks. So to Add a new module, i simply go "Project" - "Add Module...". I then call it whatever i want, put the Private MainForm as frmMain into it, and it should work?

I tried that, but i still get "Name 'MainForm' is not declared".

I also tried putting the Private MainForm as frmMain into the form, but then i get the error "Object reference not set to an instance of an object".
Sorry to be a bother :)
Thanks heaps.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
err yea, i was tired last night in the module you want public MainForm as frmMain sorry about that
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
VB.NET:
Option Explicit On 
Option Strict On

Module MainModule
     Public MainWindow As frmMain
end module

VB.NET:
private SplashScreen as frmStartUp

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MainWindow = Me
        SplashScreen = New frmStartUp
        SplashScreen.Timer1.Enabled = True
        SplashScreen.Show()
        Me.Visible = False
End Sub

this works for me
 
Top Bottom