shill1

Using VS 2005. Creating windows forms application.

I have a search form that the user can enter their search criteria and then find records. The records found populate a grid. The user selects a record in the grid and then clicks open record button. The click event of the open record button does the following:

Dim frm As New frmRS_ModMenu

frm.Show()

Me.Hide() 'This is frmSearchForm

This works great. The user can enter data for the selected record and then close the form. When the data entry form is closed I do the following:

frmSearchForm.Show()

Me.Close()

However, when I close the data entry form and call show on the search form the form load event fires only on the initial hide/show. This causes the search criteria, records found, and the record selected in the grid to reset, thus losing the data entered by the user.

Why does this happen only on the first hide/show



Re: Visual Basic General Hide/Show form causes form to reload

MSFT Abel Valadez

Try using the "Activated" and "Deactivate" events instead. The form "load" and "closed" events only get fired during the initial creation and final destruction of the form.

Hope this helps,






Re: Visual Basic General Hide/Show form causes form to reload

shill1

I need the form load event to only fire during the initial creation.

The problem is that is fires once on the initial load, then I hide the form, when I show the form the first time the form load event fires again. This is what I need to correct.





Re: Visual Basic General Hide/Show form causes form to reload

Dustin_H

Although this will not fix the bug, it will be a valid work-around.

Create a new local boolean variable in the form.

on the forms first load, set the variable to true.

In the load sub, check to see if the variable is true, and if it is, exit sub .

Example...

Private FormLoaded as Boolean

private sub form_Load(...)

If FormLoaded = true then exit sub

FormLoaded = True

end sub

Hope this helps,

Dustin






Re: Visual Basic General Hide/Show form causes form to reload

shill1

I will try this approach. However, can you confirm this is an actual bug

Thanks.





Re: Visual Basic General Hide/Show form causes form to reload

shill1

The boolean approach did not work.

Basically, after the first hide/show any subsequent hide/show does not call the form load event.

Any other ideas

Thanks.





Re: Visual Basic General Hide/Show form causes form to reload

Dustin_H

I was not able to reproduce your bug.

 

Here's the code i used....

 

Public Class Form1

 

    Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown

        Debug.Print("Show Code")

    End Sub

 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Debug.Print("Load Code")

    End Sub

 

    Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize

        Debug.Print(Me.WindowState.ToString)

    End Sub

End Class

 

And here's my debug output.  I opened, minimized, restored, maximized, then restored again.

 

*************Debug output window**************

Load Code

Show Code

Minimized

Normal

Maximized

Normal






Re: Visual Basic General Hide/Show form causes form to reload

shill1

Ok, then it has to be the way I am dimensioning the form variables

Can you give me an example of how to diminsion my form variables to show/hide between forms

Login > Main Menu > Search > Data Entry

After login verified, open main menu and close Login form.

On form load of main menu, pull options from db. user selects option and opens search form, hide main menu.

Same principle from search to data entry. User selects record on search form and clicks open record. Hide search form and show data entry.

Does this make sense





Re: Visual Basic General Hide/Show form causes form to reload

Dustin_H

This can be done, however, why bother closing the Mainmenu or search forms

I can see closing the login form, but the others

When you are opening the other forms, why not open them modal So that the user can't switch to the other forms until the Search or Data Entry forms are closed.

if not, i wouldn't Hide() or show(). I would simply set visible property to True/False. (And reset the form controls when a 'new' form is needed instead of creating a new object)






Re: Visual Basic General Hide/Show form causes form to reload

shill1

I think I figured out what is going on. It is the show method.

After I diminsion and show the new form then hide the current form, the show method on the new form is creating a completely new instance of the first form vs. just showing the form that was hidden.

Show search form:

Dim frm As New frmSearchForm

frm.Show()

Me.Hide()

Close search form and go back to Main Menu.

frmMainMenu.show()

Me.Close()

I diminsioned and showed the Main menu form on the login form which is no longer in memory. How do I specifically show the instance that I hid, see code in red





Re: Visual Basic General Hide/Show form causes form to reload

Dustin_H

Well, i guess you'll do it that way then :P

Here's how. Assume Form1 is your main form and Form2 is your search form.

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim frm As New Form2

frm.Show()

frm.Owner = Me

Me.Hide()

End Sub

Private Sub frm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

Debug.Print("frm1 loaded")

End Sub

End Class

Public Class Form2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Me.Owner.Show()

Me.Close()

End Sub

End Class

Dustin






Re: Visual Basic General Hide/Show form causes form to reload

shill1

I must hide the forms due to the application specifications. Gotta love those. ;)

This method works. Thank you so much for your assistance.

Sharon