Switching between forms

In a WinApp I have a startup page "frmRules" which displays information to users.  Upon them hitting the OK button, I open frmMainScreen like so:

        Dim frmMainScreen As New frmMainScreen
        frmMainScreen .Owner = Me
        frmMainScreen .Show()

I want for frmRules to complete hide when frmMainScreen loads, but on frmMainScreen I want a button that will bring it back up and place focus on it.  I just can'tseem to figure this out.
LVL 2
chrisryhalAsked:
Who is Participating?
 
Shahan AyyubSenior Software Engineer - iOSCommented:
Below this line in frmrules:
frmMainScreen .Show()

write
Me.hide()

frmMainScreen will be appear and on frmMainScreen, on button event write:

        Frmrules.Show()

Is this helpful ????
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Just be aware that if you go back and forth multiple times then you will be creating multiple instances of frmMainScreen but will only be hiding/showing the same instance of frmRules.

To work with the same frmMainScreen everytime get rid of this line:

    Dim frmMainScreen As New frmMainScreen

Just depends on what your app does and how it should work.
0
 
jppintoCommented:
To open my child forms on top of a parent form, I use the attached funcion to do that. I call the function with something like this on the main form, when I click a button to open a child form:

Call OpenMDIForm(New frmInsertProduction, Me)

jppinto
Public Sub OpenMDIForm(ByVal frmName As Form, ByVal frmParent As Form, Optional ByVal frmTag As Integer = 0, Optional ByVal frmDuplicates As Boolean = False)
        Dim IsLoaded As Boolean = False
        Dim frm As Form

        On Error Resume Next

        ' In case Duplicate forms are not allowded
        If Not frmDuplicates Then
            ' Checks if it's already opened
            For Each frm In frmParent.MdiChildren
                If frm.Name = frmName.Name Then
                    IsLoaded = True
                    Exit For
                End If
            Next
        End If

        ' Case it's not open
        If Not IsLoaded Then
            frmName.MdiParent = frmParent

            'Sets the Tag value of the form
            If frmTag > 0 Then frmName.Tag = frmTag
            frmName.Show()

        Else
            frmName.Focus()
        End If

        frmName = Nothing
        frm = Nothing
    End Sub

Open in new window

0
 
PeterDimitrovCommented:
Create a property in frmMainScreen which will keep pointer to frmRules (I am using the name RulesForm , but you can use any other)

' code for the new property in frmMainScreen 
    Private _frmRules As System.Windows.Forms.Form
    Public Property RulesForm() As System.Windows.Forms.Form
        Get
            Return _frmRules
        End Get
        Set(ByVal value As System.Windows.Forms.Form)
            _frmRules = value
        End Set
    End Property

    'code to show frmRules form from frmMainScreen 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If _frmRules IsNot Nothing Then
            _frmRules.Show()
        End If
    End Sub

' code to show frmMainScreen  from frmRules ( your original code) + 1 line to set the new property value
       Dim frmMainScreen As New frmMainScreen
        frmMainScreen.Owner = Me
        frmMainScreen.RulesForm=Me  'set new property value to the current instance of frmRoles
        frmMainScreen.Show()
        Me.Hide()    ' hide frmRules

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.