Solved

vb.net winform  / visual basic sub main

Posted on 2008-10-23
15
1,884 Views
Last Modified: 2013-12-17
hi,
in a visual studio.net 2008 Winform application I couldnt find .bas file like in visual basic where the code is executed
automatically before any form is loaded.

Sub Main()

End sub
I couldn't find such thing in .net 2008 . Is it that it cant be done in here.
0
Comment
Question by:dotnet0824
  • 4
  • 4
  • 4
  • +3
15 Comments
 
LVL 10

Expert Comment

by:kdwood
ID: 22788033
Check out this link, I think it will answer your question and give you examples.

http://www.getdotnetcode.com/GdncStore/free/Articles/VB%20NET%20Sub%20Main%20Procedure.htm

Regards,

Keith
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22788076
There is no more bas files in vb.net. VB.NET is object oriented and so everything here is classes.
You will have to create classes or assemblies and use them in your code.
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22788093
In the version of VB.NET that I use, it is possible to add this type of functionality.

Steps:

1. Load/Create your VB.NET project.
2. Create a new 'Module' file...'Module1.vb' for the sake of this example.
3. Replace the contents of this file with something like shown in the source listing below.
4. From the Project properties window you can change the startup object to 'Sub Main'.

A windows application defaults to the generated form. In a console application, the IDE automatically generates a module with 'Sub Main' inside it.
' Module1.vb
 

Module Module1
 

    Sub Main()

        MessageBox.Show("Hello World")

    End Sub
 

End Module

Open in new window

0
 
LVL 41

Expert Comment

by:graye
ID: 22788119
There really is *.vb file that contains the "Sub Main" equivalent.... it's just hidden.    From the Solution Explorer (typically in the upper right-hand corner), click the tool bar icon for "Show All Files".   Then expand the Form1 "node" (or whatever it's called in  your application) to see the Form1.designer.vb file.   That's where it is...
However, you'd not normally edit anything from there.  So typically you'd just go to Form1 and use the pull down to add a New() method to the form (recall that the hidden one is a partial class).   That's where you'd do the very early initialization tasks
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22788851
In VB.Net 2008, you do NOT need Sub Main() anymore...

Read up on the Application Model:
http://msdn.microsoft.com/en-us/library/w3xx6ewx(VS.80).aspx

Specifically, you want the Startup() Event, which fires BEFORE any forms are shown:
http://msdn.microsoft.com/en-us/library/t4zch4d2(VS.80).aspx

Click on Project --> Propertes.
On the Application Tab, make sure the "Enable Application Framework" box is Checked.
In the Bottom Right, click on the "View Application Events" Button.
Across the top...
Change the Left DropDown to "(MyApplication Events)".
Change the Right DropDown to "Startup".
Namespace My
 

    ' The following events are available for MyApplication:

    ' 

    ' Startup: Raised when the application starts, before the startup form is created.

    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.

    ' UnhandledException: Raised if the application encounters an unhandled exception.

    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 

    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.

    Partial Friend Class MyApplication
 

        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup

            ' ...your code in here...

        End Sub
 

    End Class
 

End Namespace

Open in new window

0
 

Author Comment

by:dotnet0824
ID: 22829245
Hi All,
Please find the pic attatched. I dint find View Application Events button to open the Code Editor when I go to the project properties (VS.NET 2008 Professional)
Project-Properties.JPG
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22829755
The version of Visual Studio your using doesn't have this option. I have attached a screen shot of Visual Basic Express 2008 which does have the option that Idle_Mind suggested.

Try this instead.

Go to your Form source code and add a constructor (like shown in the source below). This WILL be called automatically before the form is loaded.
    Public Sub New()
 

        ' This call is required by the Windows Form Designer.

        InitializeComponent()
 

        ' Add any initialization after the InitializeComponent() call.

        MessageBox.Show("Test")

    End Sub

Open in new window

Untitled.jpg
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:dotnet0824
ID: 22829789
But I am not looking @ form level . Lets say I have 4 forms the example which you had mentioned is specific to a form. I want to declare some methods before any form loads .

For eg : determining the Regional settings the  user computer uses ie english Or whatever and some global kind of functions
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22829811
Initially one of your forms is loaded. That form is responsible for loading the other 3. So the constructor of the form that spawns the other 3 forms should do the trick.

The only alternative that I know of is Sub Main as I suggested originally, which gives you complete control over how your program is initialized. You can initialize things and display forms as you please.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22831028
What version of Visual Studio is that dotnet0824?  (It's not the Express version obviously...)

I assume that it is a WinForms project based on the window title?
0
 

Author Comment

by:dotnet0824
ID: 22842923
Hi Idle_Mind.
Version : Microsoft Visual studio 2008 (Professional Edition).
I cant find the feature you mentioned there  :-(
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 22843272
I see what the problem is!...

Your question title is:

    "vb.net winform  / visual basic sub main"

But your screenshot clearly shows that you are working in C#, as evidenced by the circled "Form1.cs [design]" below.  The ".cs" is what gives it away.  If you were working in Visual Basic then it would be ".vb".

In this respect, VB and C# are NOT equivalent.  The Application Events are only available in VB.Net 2005 (or above).

So switch to a VB.Net project and you'll have quite a different story on your hands...  =)

If you want to stick with C# then you need to use Sub Main() as the others have suggested.  It is located in "program.cs" in your solution explorer:
    static class Program

    {

        /// <summary>

        /// The main entry point for the application.

        /// </summary>

        [STAThread]

        static void Main()

        {

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new Form1());

        }

    }

Open in new window

That-s-NOT-VB.Net.jpg
0
 

Author Comment

by:dotnet0824
ID: 22846369
Fantastic catch Idle_Mind.
Yes I can see program.cs ... So thats the place which has be used like a Module in VB or Application Events in vb.net  right !!!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22846477
Correct.
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22846607
But remember, you can only place C# code in a .cs file!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Multiple file Upload asp.net 2 36
Visual Studio .Net 2012 and Crystal Reports 9 43
How can I use SQL Stored Procedure to return FedEx Zone 17 41
C# Single Form 8 26
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now