Solved

vb.net winform  / visual basic sub main

Posted on 2008-10-23
15
1,876 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:dotnet0824
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Correct.
0
 
LVL 13

Expert Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

7 Experts available now in Live!

Get 1:1 Help Now