From Question

  • Thread starter Thread starter Newbie!
  • Start date Start date
N

Newbie!

Hi Group,

Sorry I know this has probably been asked loads of time, and sorry also for
my stupidity (Im a Newbie!) but ......

I have a Main Form with a button on it. When a user Clicks on the Button I
want it to Open Form2 and Close Form one but for the life of me I carn`t
find out where to do it.

I have the following Code to bring up a Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim Form1 As New Form1()
Form1.Show()
End Sub

I`ve Also Tryed

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim Form1 As New Form1()
Form1.Show()
Me.Close
End Sub

But this opens up Form2 and close`s it again straight Away.

I`ve also tryed Form1.ShowDialog() but that dosn`t seem to help either?

Any bosy got any suggestions on Links?

Ta
Si
 
Hi thanks for you help,

I`ve tryed that but it dosn`t work, here goes - I have a 2 Forms (Form1 &
Form 2) on Form 1. I have a Button (Button1) in Button1`s code as follows

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim f2 as Form2
f2 = New Form2()
f2.Show()
Me.Close()
End Sub

How ever when i Click on the Button, Form2 loads then closes it`s self
stright away, If i take out the Me.Close it stays open but so does Form1

How do I get Form1 to Close When Form2 is loaded?

Do I need any Code else where other than Button1_Click?

Ta
Si
 
* "Newbie! said:
I`ve tryed that but it dosn`t work, here goes - I have a 2 Forms (Form1 &
Form 2) on Form 1. I have a Button (Button1) in Button1`s code as follows

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim f2 as Form2
f2 = New Form2()
f2.Show()
Me.Close()
End Sub

How ever when i Click on the Button, Form2 loads then closes it`s self
stright away, If i take out the Me.Close it stays open but so does Form1

How do I get Form1 to Close When Form2 is loaded?

Do I need any Code else where other than Button1_Click?

No, your code is correct, but your application is loosing its message
pump when closing the form. You will have to use, for example, the code
I referenced in my last post. What doesn't work? Are you sure the
project's startup object is set to 'Sub Main' (in the project
properties)?
 
Hi Newbie,

I have another approach for this than Herfried an Armin and I think it is
not good to interfier with my method. But
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim f2 as Form2
f2 = New Form2()
f2.Show()
Me.Close()
End Sub

That me.close is not good, I use in that case me.hide but I am not sure if
that is also right in the method from Herfried and Armin.

But me.Close closes your program as soon as the button is pushed normaly.

When they are not quick back you can try to add this code in form1 then
also

Private Sub f2_VisibleChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles f2.VisibleChanged
Me.Show()
End Sub
End Class

And dont not know if this is enough in the method you now are using.
But as I said, you can try it.

Cor
 
OK, i`ve created my TestApp - Form1, & Form2, on Form1 to the top i`ve
added;

Public Class Form1
Inherits System.Windows.Forms.Form

Public Sub Main()
Dim f As Form1
f = New Form1()
Application.Run()
End Sub

And then created a button and added the following code to the button:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim f2 As Form2
f2 = New Form2()
f2.Show()
Me.Close()
End Sub

I`ve change my Project to Run Sub Main at Start-up and also tryed it running
Form1 at Start-Up but i keep getting the following error:

When I try to run my porject I get a Error Saying:

"No accessible 'Main' method with an appropriate signature was found in
'TestAPP'."

Thanks Everyone for your Help So Far.

Ta
Si
 
Hi Newbie,

Did you look to my message or posted this imidiatly nothing is changed in
your code as far as I can see is the "me.close" still there.

If you are only posting without looking to the answers, please tell us, than
we don't have to answer you?

Just a thought,

cor
 
Sorry Cor i din`t see you post. I`ve Just tryed you suggestion but to no
avail, im so confused.

could you possible e-mail me a testapp with 2 forms on on form1 have a
command button which closes the form1 and opens form2?

Or i could send you my code so far?

My e-mail is (e-mail address removed)

Ta
Si
 
Hi Newbie,
I send you 2 beneath pasted even two examples. The last one I never used,
but made once because I saw so many questions about this. (Make those
examples real as two new projects, than you can paste what you like to your
project).


The first example is using a splash screen at start up.
You have to create two forms for it and on both a button.
And than paste this code in both
\\\Main form
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim frm As New Form2
Me.Hide()
frm.ShowDialog()
Me.Show()
frm.dispose
End Sub
///
\\\Sub form 2
Private Sub Form2_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.CancelButton = Me.Button1
Me.ControlBox = False
End Sub
///
--------------------------------------------------------
\\\For this one you have to create a project with 3 forms with on the first
one two buttons and a label \\\and on the other two one button.
Private WithEvents frm2 As New Form2
Private WithEvents frm3 As New Form3
Private myActiveForm As Integer
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
myActiveForm = 2
frm2.Show()
me.Hide()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
myActiveForm = 3
frm3.Show()
me.Hide()
End Sub
Private Sub frm2_VisibleChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles frm2.VisibleChanged,
frm3.VisibleChanged
Me.Show()
Me.Label1.Text = "Return form = " & myActiveForm.ToString
End Sub
End Class
///
\\\
Public Class Form2
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Me.Hide()
End Sub
End Class
///
\\\
Public Class Form3
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Me.Hide()
End Sub
End Class
///

I hope you can find your solution with this?

Cor
 
Newbie! said:
Public Sub Main()
Dim f As Form1
f = New Form1()
Application.Run()
End Sub

When I try to run my porject I get a Error Saying:

"No accessible 'Main' method with an appropriate signature was found in
'TestAPP'."

Add the shared keyword:

Public Shared Sub Main
 
Cor,

Many, Many Thanks for you help that worked a treat. Again Many Thanks

And Also Thanks to others that have helped

Ta
Si
 
Back
Top