Link to home
Start Free TrialLog in
Avatar of coperations07
coperations07Flag for United States of America

asked on

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

Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

try F3.Activate (after F3.Show)
Avatar of coperations07

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This gave the same error.
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.
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

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
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

remove the new from your declaration since you do it in your click event
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

have you tried not to declare instances (F1-F6) and use directly form names:
frmMain.Show
frmMain.Activate
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"
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.
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.
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
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?
>>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.
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.