Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

close winform form another winform

$
0
0

Hello:

I have 2 forms on my application. I have the main form (frmWelcomePage) that opens TopMost, CenterScreen and Maximized. Then I have another form (frmButtonDescription) on this screen that pops open when I press a button. That second screen has a button that navigates to another screen, so when I press that button the second form closes, and the main form is suppose to close as well and the selected sheet open up.

However, the second screen closes fine, but my main screen remains open and active, while the called sheet opens but does not enable. I track down what was happening and the issue is that form all code runs, but the main screen does not seem to want to close.

Here is my original code:

      Private Sub btnOpenDashboard_Click(sender As Object, e As EventArgs) Handles btnOpenDashboard.Click

    Dim welcomeForm As New frmWelcomePage

    If lblReportTitle.Text = "Employee Dashboard" Then

        Me.Close() 'This works
        welcomeForm.Close() 'This one remains open and active
        Globals.dsbEmployeeBoard.Select() 'This one opens but is not enabled


    End If


End Sub

So I figured out that I could not do that because all I was doing was creating an instance of a form that was already opened. I need to pass a reference of your first form (frmWelcomePage) to the second form (frmButtonDescription), so that in the second form you can close the first form. So I worked out the following code:

Option Explicit On
Option Strict On

'Import Libraries
Imports Microsoft.Office.Tools
Imports System
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Windows.Forms

Public Class frmButtonDescription

    Inherits Form
    Private _frmWelPage As frmWelcomePage

    Public Sub New(frmWelPage As frmWelcomePage)
        Me.frmWelcomePage = frmWelPage

    End Sub

    Private Sub btnOpenDashboard_Click(sender As Object, e As EventArgs) Handles btnOpenDashboard.Click

        If lblReportTitle.Text = "Employee Dashboard" Then

            Me.Close()
            _frmWelPage.Close()
            Globals.dsbEmployeeBoard.Select()

        End If


    End Sub
End Class

But now I get 2 errors I cannot figure out. The first is here:

Public Sub New(frmWelPage As frmWelcomePage)

It says that my sub should call an Initialize component. 

The other error is the line right below it,

 Me.frmWelcomePage = frmWelPage

The error says that Me.frmWelcomePage is not a member of btnButtonDescription.


Viewing all articles
Browse latest Browse all 12583

Trending Articles