Solved

vb.net winform  / visual basic sub main

Posted on 2008-10-23
15
1,897 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query Help Top 1 and Distinct? 6 36
JSON Deserialize issue 6 26
run a stored procedure from vb.net 1 27
Upgrade code from VS 2010 to VS 2015 7 21
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 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