Calling a form from another form


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Calling a form from another form

  1. #1
    Join Date
    Jan 2005
    Posts
    2

    Calling a form from another form

    I got help from someone with my question, how do I make forms appear and disappear, but I had a few question more I was hoping I could get help with. First, I know the puporse of the last two lines of code, but what is the purpose of the first line. Second, where are the first two lines put. I mean they worked for me in the click event of a button, but can you put it in the declarations section. Last, is there a way to declare it so I can use "frm" in other controls, buttons, forms, etc, without redeclaring it.

    Dim frm As Form2
    frm = New Form2() ' instance a new Form2 (create an
    object)
    frm.Show() ' now refer to frm as an instance
    of Form2

  2. #2
    Join Date
    Dec 2003
    Posts
    2,750
    The first line of code is simply declaring the variable object for the Form instance you are going to create. You can define the scope of this variable just like any other.

    If you want to use it in other procedures or events in the Form, or other module, just declare it once at the module level (top of the module outside any procedures).
    Paul
    ~~~~
    Microsoft MVP (Visual Basic)

  3. #3
    Join Date
    Jan 2005
    Location
    hard question >.>'
    Posts
    8
    I have a question that pretains to this, that code will open "Form2" but how can you make "Form1" Close but have "Form2" stay open???

    I've tried
    dim frm1 as new Form1
    frm1.Close()

    I've put that code both before, and after the code to open "Form2".

  4. #4
    Join Date
    Dec 2003
    Posts
    2,750
    I think you're looking for the Application.Run method:

    http://msdn.microsoft.com/library/de...ssruntopic.asp
    Paul
    ~~~~
    Microsoft MVP (Visual Basic)

  5. #5
    Join Date
    Jan 2005
    Location
    hard question >.>'
    Posts
    8
    Ok, I have a completely new project with two blank forms, Form1 and Form2, There is a button on Form1, I want the button on Form1 to open Form2, Then close Form1, So after the button is pressed, the only thing open will be Form2.
    Last edited by Ryau; 02-03-2005 at 11:20 AM.

  6. #6
    Join Date
    Jan 2005
    Posts
    2
    Thank you guys, but I am going out of town for a week and I won't be able to try it for awhile. Just wanted to let everyone know that I wasn't ignoring your help.

  7. #7
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Ryau: Set your application to use Sub Main as its startup object. In the module containing Sub Main, declare your global Form variables. Also declare a global ApplicationContext variable:
    Code:
    Option Strict On
    Option Explicit On 
    Imports System.Windows.Forms
    
    Module MyApp
        Public Form2 As Form2
        Public Context As New ApplicationContext(New Form1)
        Sub Main()
            Application.Run(Context)
        End Sub
    End Module
    That will start your app with Form1 as its main form. Now, to switch forms in a Button_Click event, do this:
    Code:
    Private Sub Button_Click(ByVal sender As System.Object, _ 
        ByVal e As System.EventArgs) Handles Button.Click
        ' Create new instance of Form2
        MyApp.Form2 = New Form2
        ' Set Form2 as new MainForm
        Context.MainForm = MyApp.Form2
        ' Show it
        MyApp.Form2.Show()
        ' Dispose current form
        Me.Dispose()
    End Sub
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  8. #8
    Join Date
    Jan 2005
    Location
    hard question >.>'
    Posts
    8
    Uh, I dont know what that code was supposed to do, but this is what I wanted:

    code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim frm1 As New Form1
    Dim frm2 As New Form2
    frm2.Show()
    Me.Hide()
    End Sub

    That opens Form2, and Hides Form1.

  9. #9
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Ryau: If that works for you, cool. You asked, though, how to close Form1. The code you posted merely hides it; my code actually closes (or "disposes") it.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center