Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1973
  • Last Modified:

vb.net winform / visual basic sub main

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
dotnet0824
Asked:
dotnet0824
  • 4
  • 4
  • 4
  • +3
1 Solution
 
kdwoodCommented:
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
 
TechTiger007Commented:
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
 
numberkruncherCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
grayeCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
dotnet0824Author Commented:
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
 
numberkruncherCommented:
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
 
dotnet0824Author Commented:
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
 
numberkruncherCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
dotnet0824Author Commented:
Hi Idle_Mind.
Version : Microsoft Visual studio 2008 (Professional Edition).
I cant find the feature you mentioned there  :-(
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
dotnet0824Author Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Correct.
0
 
numberkruncherCommented:
But remember, you can only place C# code in a .cs file!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 4
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now