Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello i am a c# developer recently i'am trying to work with vb.net now in c# GUI application if i want to run a particular form say form2.cs i can do that by just writing the name of the form in the Program.cs file at
C#
Application.Run(new form2());

now i am working with 3 forms in vb.net and i want to run form2.vb in vb.net so how to do that means is their a program.vb file to do that

Please help me out

Thank you
Radix
Posted
Updated 27-Mar-10 1:27am
v2

You could change your start up form from your project properties. :)

For example (in VS2008) - right click on the project go to properties,
in Application tab you can select which form you want to use as a startup form.
 
Share this answer
 
v2
Comments
Member 10030181 6-May-13 8:36am    
thankss
Make an object of the form and then run it.

Dim MainMenu as New Form2
MainMenu.show



If you want to close form1 after you call form2, then use Dispose.

Me.Dispose(False)


Dont use Me.Close


You may also want to work with hide and show with the forms as well.
 
Share this answer
 
v2
For my fellow C# developers, I understand the dilemma. I came to this page in search of an answer, and I found most of it here.

Using the information in this page as a starting point, I discovered that you can accomplish everything you want, using code that is almost identical to the code you would write in C#, without touching the application property sheet! and the only change needed on the application property sheet is to disable the Application Framework.

I have an existing application, EZPayWebPmtsNotifier, whose only form is called frmMain. Like many that came before it, the initial requirements were pretty simple: display a window when a specific file exists in a designated folder. Otherwise, keep the window hidden. A timer loop checks every second or so to see whether conditions have changed (File is now present/absent.), and adjust the display accordingly.

Requirements have since evolved, and the program now keeps a log of its activities. This led to the need for a mechanism to allow a script to shut it down, e. g., for maintenance.

Into the application I go, and add the following short function to the file that contains the hand coded portion of the source code of frmMain, Form1.vb.

VB
Public Shared Function Main(ByVal cmdArgs As String()) As Integer

    Application.Run(New frmMain())
    Return (ERROR_SUCCESS)

End Function


That's it.

You can use any of the four ways to declare your Main routine that are discussed in "Main Procedure in Visual Basic," at http://msdn.microsoft.com/en-us/library/ms235406%28v=VS.100%29.aspx[^]; I chose the most complete form, because I need the argument list, and I want the option of returning a nonzero exit code.

I've omitted the code to parse the command line, which isn't written yet. My first objective was to prove the concept, by incorporating my own start-up routine.

The key points are these.

1) The function or subroutine must be called Main.

2) The function or subroutine must be marked Public and Shared.

3) The function or subroutine must be defined in the class module that defines your startup form.

4) If Main is a function, it must return Integer.

5) If Main takes arguments, it must take only one, which must be an array of strings, passed ByVal.

Caveats

When you disable the Application Framework, you lose the ability to mark the application as a single instance. Nevertheless, you don't really lose the ability to have a single instance; you just have to work for it.

The following routine does two things.

1) Enforce single instance.

2) Implement shutdown by command, so that a script can shut it down.

Following is the whole routine that I am about to put into production, having proven to myself, by enumerating processes, that single instance is enforced.

VB
Public Shared Function Main(ByVal cmdArgs As String()) As Integer

    Const CMD_SHUTDOWN As String = "shutdown"

    '   ------------------------------------------------------------
    '   Get the name and ID of this process. The name is used to get
    '   a list of active instances of this program, and the ID is to
    '   prevent us telling Windows to kill us before we finish the
    '   scan.
    '   ------------------------------------------------------------

    Dim intMyProcessID As Integer
    Dim strMyProcessName As String
    Dim myProcess As Process = Process.GetCurrentProcess

    With myProcess
        intMyProcessID = .Id
        strMyProcessName = .ProcessName
    End With    ' With myProcess

    myProcess.Dispose()
    myProcess = Nothing

    If cmdArgs.Length > ZERO Then
        If cmdArgs(ARRAY_FIRST_ELEMENT).ToLower() = CMD_SHUTDOWN Then
            If Application.OpenForms.Count > ZERO Then
                For Each frm As Form In Application.OpenForms
                    frm.Close()
                Next    ' For Each frm As Form In Application.OpenForms
            End If  ' If Application.OpenForms.Count > ZERO Then

            '   ------------------------------------------------------------
            '   With our name and ID in hand, ask Windows for a list of all
            '   processes that have the same name. For each such process,
            '   except this one, ask Windows to close it.
            '   ------------------------------------------------------------

            For Each activeProcess As Process In Process.GetProcessesByName(strMyProcessName)
                If activeProcess.Id <> intMyProcessID Then
                    activeProcess.CloseMainWindow()
                End If  ' If activeProcess.Id <> intMyProcessID Then

                activeProcess.Dispose()
            Next    ' For Each activeProcess As Process In Process.GetProcessesByName(strMyProcessName)

            '   ------------------------------------------------------------
            '   Finally, we bug out.
            '   ------------------------------------------------------------

            Application.Exit()
            Return (ERROR_SUCCESS)
        End If  ' If cmdArgs(ARRAY_FIRST_ELEMENT).ToLower() = CMD_SHUTDOWN Then
    End If  ' If cmdArgs.Length > ZERO Then

    '   --------------------------------------------------------------------
    '   If there are no other instances running, we put up our window.
    '   Otherwise, we vanish, leaving the first instance running.
    '   --------------------------------------------------------------------

    If Process.GetProcessesByName(strMyProcessName).Length = PLUS_ONE Then
        Application.Run(New frmMain())
    End If  ' If Process.GetProcessesByName(strMyProcessName).Length = PLUS_ONE Then

    Return (ERROR_SUCCESS)

End Function
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900