Navigating between forms

Hi,
I'm using VB.Net 2005, VS2005.
I'm working on a program that has multiple forms. I want to go from one form to another when a button is clicked. So far I've setup a module that declares the forms as "Friends." Then I use the attached code to open a form. This works once, but I want to be able to go back and forth between forms and what I have isn't working for this. What is the best way to achieve this? I'm not doing anything with webpages, just getting some user input for a work app.

Thanks,
Dave
Friend Module mod_forms
    Friend F1 As frmMain
    Friend F2 As frmQuery
    Friend F3 As frmCriteria
    Friend F4 As frmWaves
    Friend F5 As frmWave_Views
    Friend F6 As frmMatch
End Module
 
    Private Sub btnSeq_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSeq.Click
        If F3 Is Nothing Then
            F3 = New frmCriteria
        End If
        F3.Show()
    End Sub

Open in new window

coperations07Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
try F3.Activate (after F3.Show)
0
coperations07Author Commented:
I tried this and it says "there is not source code available for the current location"
It also says it "cannot access a disposed object"
        If F1 Is Nothing Then
            F1 = New frmMain
        End If
        F1.Show()
        F1.Activate()

Open in new window

0
Éric MoreauSenior .Net ConsultantCommented:
this is because you closed the form. use this:
        If F1 Is Nothing OrElse F1.IsDisposed Then
            F1 = New frmMain
        End If
        F1.Show()
        F1.Activate()
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

coperations07Author Commented:
This gave the same error.
0
Éric MoreauSenior .Net ConsultantCommented:
can you show your code? are you sure you are using ORELSE? on which line do you have the error? what are the steps to reproduce the error?

it was working in sample.
0
coperations07Author Commented:
Well I think my project just has issues. I got out of it and opened a new project, created a module w/ friend variables, created 2 forms w/ buttons and tied the code to them. It works except that there ends up being 2 instances of the first form.
I run the program and the main form comes up. I click the button and the second form opens w/ the main form in the background. Then I click the button and I can go back and forth between the 2 forms, but the initial instance of the main form is still showing in the background.
The 2 lines below that are commented out are also working. The problem w/ them is that when I click the button to go back to the main form it minimizes the form. Once I click on it to bring it back up it I can click from one form to the othe all day. What could cause this?
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Me.Visible = False
        'My.Forms.frmWaves.Show()
        If F2 Is Nothing OrElse F2.IsDisposed Then
            F2 = New frmWaves
        End If
        F2.Show()
        F2.Activate()
    End Sub

Open in new window

0
Éric MoreauSenior .Net ConsultantCommented:
I might be stupid ... I don't understand. Without complete code, I cannot help you anymore.

little question/comment: I hope that F2 and your other variable is global to the application otherwise for sure you will get many instances.

another comment: in VS2005, you don't need anymore to create form instances. just have to call frmWaves.show
0
coperations07Author Commented:
Sorry, I'll try to explain better.
There isn't much code really. There are 2 forms. Each has a button w/ a click event like the above code.
Other than that all I have is a module with the below code. (I don't have the code infront of me, but I think I remember)
Public Module modForm
   Friend F1 as new frmMain
   Friend F2 as new frmWave
End Module

Open in new window

0
Éric MoreauSenior .Net ConsultantCommented:
remove the new from your declaration since you do it in your click event
0
coperations07Author Commented:
The attached snippet is exactly what I have in my module.
I have this code in one form on a click event and the same code, w/ F1 instead of F4, in a click event on another form. F1 is the main form and opens to begin with. Its button works to take me to F4, but then the button on F4 isn't doing anything when I click it.
        If F4 Is Nothing OrElse F4.IsDisposed Then
            F4 = New frmWaves
        End If
        F4.Show()
        F4.Activate()

Friend Module mod_forms
    Friend F1 As frmMain
    Friend F2 As frmQuery
    Friend F3 As frmCriteria
    Friend F4 As frmWaves
    Friend F5 As frmWave_Views
    Friend F6 As frmMatch
End Module

Open in new window

0
Éric MoreauSenior .Net ConsultantCommented:
have you tried not to declare instances (F1-F6) and use directly form names:
frmMain.Show
frmMain.Activate
0
coperations07Author Commented:
I tried using the form names and I'm getting the same result.

I execute the program and the main form opens,
I push a button to go to the 2nd form and the 2nd form opens,
I push a button to go back to the main form and it does nothing,
I close the 2nd form and the main form is still there,
I push the button to go to 2nd form again but it gives this error:
"There is no source code available for the current location"
Then I click OK and it shows another error message that says:
"cannot access a disposed object. object name frmWaves"
0
Éric MoreauSenior .Net ConsultantCommented:
very strange because when I create 2 forms with buttons that show/activate the other one, I have no problem.

you have something else in your code that is causing problems. as long as you won't share it, it won't be possible for anyone to help you.
0
coperations07Author Commented:
What code are you wanting me to share? The only code I haven't shared that would be remotely relavant is the automatically generated code which I prefer not to mess with.
I'll post what ever code will help.
0
Éric MoreauSenior .Net ConsultantCommented:
can you replicate the strict minimal code (only 2 forms) to reproduce the error? create a brand new project for the purpose of demoing int
0
coperations07Author Commented:
Okay I have a new project. I created 2 forms. each form has a button w/ a click event.
Button1_click
        Me.Visible = False
        My.Forms.frmWaves.Show()
Button2_click
        Me.Visible = False
        My.Forms.frmMain.Show()

This is working. The only issue w/ it is that it minimizes the form some times when I go back and forth between the 2.  So what could cause this to not work in my other program?
0
Éric MoreauSenior .Net ConsultantCommented:
>>it is that it minimizes the form some times when I go back and forth between the 2

I cannot reproduce this behavior. Maybe the Activate method fixes that (see bellow).

BTW, you do't even have to pass through the Forms collection:
        Me.Visible = False
        Form1.Show()
        Form1.Activate()

>>So what could cause this to not work in my other program?

Bebause you surely have something else that interferes the process.
0
coperations07Author Commented:
Well, I switched the code back to using the friend module and it works fine now. Not sure why it didn't work before. Thanks for the help emoreau.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.