Results 1 to 3 of 3

Thread: [RESOLVED] command line parameter in Windows Forms Application

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    Belgium
    Posts
    68

    Resolved [RESOLVED] command line parameter in Windows Forms Application

    I'm trying to pass a command line parameter to a vb.net solution that starts up with a Windows Form.

    I noticed it wouldn't accept parameters, it gave errors when writing the constructors.
    So then I created a module, to start up with, that accepts the parameter. And that module then creates the form and passes the parameter to it.

    Now my problem is that my form closes immediatly after opening, since my module just shows it and then quits.

    Code:
    Module StartUp
    
        Sub Main(ByVal args() As String)
            Dim frmMain As New frmMain(args(0))
            frmMain.Show()
        End Sub
    
    End Module
    Code:
    Public Class frmMain
    
        Inherits System.Windows.Forms.Form
    
        Public Sub New(ByVal path As String)
            serverPath = path
        End Sub
    
    ....
    
    
    End Class
    What am I doing wrong?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: command line parameter in Windows Forms Application

    The reason that your form is closing immediately is because Show returns immediately and then your Main method completes and your app exits. Instead of doing this:
    VB Code:
    1. Module StartUp
    2.  
    3.     Sub Main(ByVal args() As String)
    4.         Dim frmMain As New frmMain(args(0))
    5.         frmMain.Show()
    6.     End Sub
    7.  
    8. End Module
    you should have done this:
    VB Code:
    1. Module StartUp
    2.  
    3.     Sub Main(ByVal args() As String)
    4.         Dim frmMain As New frmMain(args(0))
    5.         Application.Run(frmMain)
    6.     End Sub
    7.  
    8. End Module
    That's not what you were doing wrong though. You can use that method to retrieve commandline arguments but there are better ways in VB.NET. As you're using a Main method I'm going to assume that you're using VB.NET 2003 (please specify in future). Get rid of that Main method and get rid of that constructor argument.
    VB Code:
    1. Dim cla As String() = Environment.GetCommandLineArgs()
    2.  
    3. If cla.Length > 1 Then
    4.     'cla(0) is the executable path.
    5.     'cla(1) is the first argument.
    6. End If
    Also note for future reference that if you add any constructors to a form they will either need to call InitializeComponent explicitly or else they will need to call the default constructor so that it can call InitializeComponent, i.e. you constructor should have been:
    VB Code:
    1. Public Sub New(ByVal path As String)
    2.         Me.New()
    3.         serverPath = path
    4.     End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    Belgium
    Posts
    68

    Re: command line parameter in Windows Forms Application

    I'll try that. Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width