By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,360 Members | 2,145 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,360 IT Pros & Developers. It's quick & easy.

Open Another form and close itself in vb.net

P: n/a
For example, I create Login form first. When user login, it open the main
form and close the login form itself.
Nov 21 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"Alex" <Al**@discussions.microsoft.com> schrieb:
For example, I create Login form first. When user login, it open the main
form and close the login form itself.


\\\
Public Module Program
Public Sub Main()
Dim f As New Form1()
f.Show()
Application.Run()
End Sub
End Module
///

In the project properties, select 'Sub Main' as startup object. Place the
code below in a button's 'Click' event handler:

\\\
Dim f2 As New Form2()
f2.Show()
Me.Close()
///

You can exit the application by calling 'Application.ExitThread'. Take a
look at the 'ApplicationContext' class too.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #2

P: n/a
Why does the following slightly modified code cause an exception to be thrown
on the Application.Run Line?

Public Module Program
Public Sub Main()
Dim f As New Form1()
f.Show()
Application.Run()
End Sub
End Module

In Form1 button 'Click' event handler:

Dim f2 As New Form2()
f2.ShowDialog() Note after return from this Dialog, an exception is thrown.
"Herfried K. Wagner [MVP]" wrote:
"Alex" <Al**@discussions.microsoft.com> schrieb:
For example, I create Login form first. When user login, it open the main
form and close the login form itself.


\\\
Public Module Program
Public Sub Main()
Dim f As New Form1()
f.Show()
Application.Run()
End Sub
End Module
///

In the project properties, select 'Sub Main' as startup object. Place the
code below in a button's 'Click' event handler:

\\\
Dim f2 As New Form2()
f2.Show()
Me.Close()
///

You can exit the application by calling 'Application.ExitThread'. Take a
look at the 'ApplicationContext' class too.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3

P: n/a
Dennis,

"Dennis" <De****@discussions.microsoft.com> schrieb:
Why does the following slightly modified code cause an exception to be
thrown
on the Application.Run Line?

Public Module Program
Public Sub Main()
Dim f As New Form1()
f.Show()
Application.Run()
End Sub
End Module

In Form1 button 'Click' event handler:

Dim f2 As New Form2()
f2.ShowDialog() Note after return from this Dialog, an exception is
thrown.


I am not able to reproduce that on my machine (.NET 1.1 SP1, Windows XP
Professional SP2). What exception is thrown? Can you post the complete
error message?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4

P: n/a
Herfried,

What is the deeper reason that you use this code forever.
\\\
Public Module Program
Public Sub Main()
Dim f As New Form1()
f.Show()
Application.Run()
End Sub
End Module
///


(This is a serious question)

Saying it in otherwords what is in your opinion the advantage above the
build VBNet method in a windowform project.

Cor
Nov 21 '05 #5

P: n/a
Cor,

"Cor Ligthert" <no************@planet.nl> schrieb:
What is the deeper reason that you use this code forever.
\\\
Public Module Program
Public Sub Main()
Dim f As New Form1()
f.Show()
Application.Run()
End Sub
End Module
///


(This is a serious question)

Saying it in otherwords what is in your opinion the advantage above the
build VBNet method in a windowform project.


The built-in possibility of setting the startup form doesn't provide a way
to call 'Application.Run' without passing a form to it. In other words, the
'Sub Main's code is generated by the compiler automatically and doesn't give
you the control to do that. The code above does not suffer from the problem
that closing the main form closes the whole application because it's lacking
a message pump after the startup form has been closed.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #6

P: n/a
Herfried,

The only argument I read here is the aspect of clossing the mainform, where
I see no reasons for, but I will not tell direct I disagree about that.

However than has the main really to be the main of the project and than
should there not be any form call from another place in my opinion. I think
that any tree structure inside a form node than is not right.

The load event in a form does so nice everything before the showing of the
form. Your arguments about this looks for me more something of behaviour
from an old VB6 programmer who is afraid that they will change that nice
behaviour again (Or maybe it was already like that, I don't remember it me
anymore).

However just my thought of course.
And feel free to do it your way.

Cor
Nov 21 '05 #7

P: n/a
Cor,

"Cor Ligthert" <no************@planet.nl> schrieb:
The only argument I read here is the aspect of clossing the mainform,
where I see no reasons for, but I will not tell direct I disagree about
that.

However than has the main really to be the main of the project and than
should there not be any form call from another place in my opinion. I
think that any tree structure inside a form node than is not right.


It seems to me that you didn't get the point of what I am saying.

The application is started.
'Form1' is shown.
'Form1' is closed.
'Form2' is shown.
'Form2' is closed.
...
The application terminates.

How would you do that without a custom 'Sub Main' and without using ugly
'ShowDialog' hacks?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #8

P: n/a
Herfried,

It seems to me that you didn't get the point of what I am saying.

The application is started.
'Form1' is shown.
'Form1' is closed.
'Form2' is shown.
'Form2' is closed.
...
The application terminates.


When this is consequently done, I said that I agreed (however than no shows
inside a form).

:-)

Cor
Nov 21 '05 #9

P: n/a
If I try a simple example it seems to work OK. However, my application is
too complex to post here. I did find out that if I delete a line of Code I
had, "Application.EnableVisualStyles()" in my sub main, my application works
ok but with this line of code in sub main, it throws an exception when Form2
closes. It does seem to work OK with a simple example however. Maybe it's
one of the controls that I have on my Form1 such as splitter bar, panel,
toolbar, listbox, treeview, etc. Anyway, I'll live with out VisualStyles for
now until I get time to see exactly what's casuing the error.

Sub Main
Application.EnableVisualStyles
Form1.Show
Application.Run()
end Sub
"Herfried K. Wagner [MVP]" wrote:
Dennis,

"Dennis" <De****@discussions.microsoft.com> schrieb:
Why does the following slightly modified code cause an exception to be
thrown
on the Application.Run Line?

Public Module Program
Public Sub Main()
Dim f As New Form1()
f.Show()
Application.Run()
End Sub
End Module

In Form1 button 'Click' event handler:

Dim f2 As New Form2()
f2.ShowDialog() Note after return from this Dialog, an exception is
thrown.


I am not able to reproduce that on my machine (.NET 1.1 SP1, Windows XP
Professional SP2). What exception is thrown? Can you post the complete
error message?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #10

P: n/a
"Dennis" <De****@discussions.microsoft.com> schrieb:
Anyway, I'll live with out VisualStyles for
now until I get time to see exactly what's casuing the error.

Sub Main
Application.EnableVisualStyles
\\\
Application.DoEvents()
///
Form1.Show
Application.Run()
end Sub


Maybe this fixes the problem.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.