-
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
-
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)
-
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".
-
I think you're looking for the Application.Run method:
http://msdn.microsoft.com/library/de...ssruntopic.asp
Paul
~~~~
Microsoft MVP (Visual Basic)
-
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.
-
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.
-
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!
-
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.
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks