Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,605 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 907 people online right now. Registration is fast and FREE... Join Now!




Read the arguments passes to your VB.NET application

 
Reply to this topicStart new topic

> Read the arguments passes to your VB.NET application

jacobjordan
Group Icon



post 9 Jul, 2008 - 04:03 PM
Post #1


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:

vb

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

vb

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

vb

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

vb

Me.MainForm

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

vb

Me.EnableVisualStyles

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

vb

Me.SplashScreen

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

vb

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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

jagatworld
Group Icon



post 15 Jul, 2008 - 12:20 AM
Post #2
Hi jacobjordan,

I need some clarification on the code.

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

CODE

    Class MyApplication
         replaced with  
    Class MyApplication1


it is giving errors.

Also, the Me.Startup in
CODE

    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.
Go to the top of the page
+Quote Post

jacobjordan
Group Icon



post 19 Jul, 2008 - 01:08 PM
Post #3
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:
vb

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.
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/23/08 02:26AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month