Solved

vb.net winform  / visual basic sub main

Posted on 2008-10-23
15
1,889 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

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

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 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