Page 1 of 1

Read the arguments passes to your VB.NET application Rate Topic: -----

#1 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Posted 09 July 2008 - 05:03 PM

There may be many times you want to have your application read the arguments (if any) that were passed to it. To do so, first add a new blank code file (if you don't have a template for a blank code file, you can just add a class and delete the code it puts in it) and call it "MyArguments.vb", or something like that. Once you have added that file, add this code in it:

Namespace My

    Class MyApplication

#If _MyType = "WindowsForms" Then
        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As ApplicationServices.StartupEventArgs) Handles Me.Startup
            '*******************************************************
            '* This is the sub that fires when your application starts. You can   *
            '* read the arguments from here.                                               *
            '*******************************************************
        End Sub

        'OnInitialize is used for advanced customization of the My Application Model (MyApplication).
        'Startup code for your specific application should be placed in a Startup event handler.
        <Global.System.Diagnostics.DebuggerStepThrough()> _
        Protected Overrides Function OnInitialize(ByVal commandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String)) As Boolean
            Return MyBase.OnInitialize(commandLineArgs)
        End Function
#End If

    End Class
End Namespace


Look for the MyApplication_Startup() method in that code. That is the method that fires when your application starts, and that is also the method where you can read what arguments were passed to it. The arguments are stored in the parameter e, which is of type ApplicationServices.StartupEventArgs. To access the collection of arguments passed to your application, use the

e.CommandLine


parameter, which is a Collection(Of String) and each member is a different argument. To get the number of arguments passed to your application, you could use

e.CommandLine.Count



Now you can see what arguments were passed to your application, but really, what's the good of knowing them if you cant do anything about them? So, here i will explain a few things you can do from the MyApplication_Startup() method.

If the startup form of your application depended on what argument(s) were passed to it, you can change the startup form with the property

Me.MainForm


You can change weather or not to render XP visual styles on your application with the property

Me.EnableVisualStyles


You can change (or remove) the splash screen of your application with the property

Me.SplashScreen 


You can change the shutdown style of your application with the property

Me.ShutdownStyle


That is only an example of what you can do in the MyApplication_Startup() method. Play around a bit with the Me and Application classes. You will find there is a lot more you can do.

Is This A Good Question/Topic? 1
  • +

Replies To: Read the arguments passes to your VB.NET application

#2 jagatworld  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 11-May 08

Posted 15 July 2008 - 01:20 AM

Hi jacobjordan,

I need some clarification on the code.

I have tried the same above with some other name instead of MyApplication, as below

	Class MyApplication
		 replaced with  
	Class MyApplication1



it is giving errors.

Also, the Me.Startup in
	Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As ApplicationServices.StartupEventArgs) Handles Me.Startup


is working only from this class, if Iam declaring the same in another module it is notworking, it is working only in class.

What are all these exactly? Does VB.Net have pre defined MyApplication or other things?

Can you please explain, we will be very thankful.

Regards.

Jagat.
Was This Post Helpful? 0
  • +
  • -

#3 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Posted 19 July 2008 - 02:08 PM

The MyApplication class must be named MyApplication, and nothing else. Also, the MyApplication class must be in the My namespace, just in case it wasn't in your case. Lastly the MyApplication_Startup method must be inside the MyApplication class. All in all, this is EXACTLY how your code should look:
Namespace My
    Class MyApplication

#If _MyType = "WindowsForms" Then
        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As ApplicationServices.StartupEventArgs) Handles Me.Startup
            
        End Sub
#End If

    End Class
End Namespace


The MyApplication class MUST be in the My namespace, and the MyApplication_Startup method MUST be in the MyApplication class. If your still having trouble, show me the full code you are using for this and tell me the version of .NET your using.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1