Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 22nd, 2003, 08:24 PM
Authorized User
 
Join Date: Jun 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default Declaring an instance of a form

Hi,

I have an application with 9 forms. All of my subroutines are contained withing my main form (form1). I have added another form and wish to access subroutines and objects on form1. I have NO problem accessing objects on forms #2-8 from form1, but for some reason, I cant figure out how to declare form1 so that I can reference objects within that form from other forms.

I have tried place in a module:

Public mainForm as form1 = New Form1()

Any many other variations of it. In some cases I get errors regarding having no instance of an object. In some cases, the I can call a subroutine on form1 but a lot of the variables return zero since its trying to access data from objects on form1.

The bottom line is that I need to figure out how to access the active instance of form1.

Any and all help truly appreciated..

Thank

Ray
 
Old June 23rd, 2003, 06:58 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Ray,

As you found out, declaring and instantiating a new Form1 doesn't work. All you are doing is creating a second instance of Form1, which of course doesn't represent your first Form1.

Here's what you could do:

1. Declare a Public variable to hold your Form1 reference so it is available to all other forms as well.

Code:
    Dim MainForm As Form1
2. Add a Sub Main() to your project.

3. Add the following code to that method:

Code:
    Public Sub Main()
        MainForm = New Form1
        MainForm.ShowDialog()
    End Sub


4. Make the Main method the start-up object of your project. You can do that on the Properties dialog for the project.

Now whenever you start your app, the Main function runs. It creates a new Form1 and assigns a reference to it to the MainForm variable. All other forms should now be able to see your MainForm and access its controls.

HtH

Imar


----------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old June 23rd, 2003, 07:54 AM
Authorized User
 
Join Date: Jun 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

Harstke bedankt!

Thats exactly what I thought I was doing, creating a second instance of form1. I will follow your instructions.

Thanks again!
 
Old June 23rd, 2003, 11:10 AM
Authorized User
 
Join Date: Jun 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

Please bear with me.

1) I declared the public variable in a module. Is this correct?
2) When you say add Sub Main() to my project and then add the code you specified, is this two steps?? I just copied the code directly into the module. Is adding Sub Main() a step before adding the actual code, or is simple adding the code to the module correct?
3) I made the main method the start up object
4) My references to mainForm on other forms still give me an error that says mainForm is not declared.

If you find the time, I would appreciate another reply. Should I be adding this code to form1? The module? Where am I going wrong?

Thanks

Ray
 
Old June 23rd, 2003, 11:13 AM
Authorized User
 
Join Date: Jun 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Disregard,

I used Public instead of Dim and I got it to work. Thanks again!
 
Old June 23rd, 2003, 01:56 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Graag gedaan ;)

As you could see, by using Dim the variable is not available in your entire project. By making it Public, each individual form in your application can reach this variable.

You can use this setup to create a splash screen for your application as well. Use ShowDialog to bring up the Splash screen, have the Splash Screen unload itself and then run your main form from the Main method.

Cheers,

Imar


----------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
A newer's declaring Ant_Yan J2EE 0 September 15th, 2007 12:35 AM
Declaring a dropdownlist Rod Merritt BOOK: ASP.NET Website Programming Problem-Design-Solution 0 August 1st, 2006 02:11 PM
declaring structs scoobie C++ Programming 3 April 6th, 2006 12:44 AM
declaring variables that are not used crmpicco VB How-To 2 May 19th, 2005 04:22 PM
Declaring and instantiating... jacob ASP.NET 1.0 and 1.1 Basics 4 August 21st, 2003 10:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.