Issue: Details Window Open again and again
Level: Intermediate
Knowledge Required:
To understand the following solution you must have the knowledge of:
- Win Forms
- Properties
- Events
Consider a scenario, we have created 2 Forms
- Master Form i.e. frmStudentMaster
- Detail Form i.e. frmStudentDetail
if "New Student" button is clicked then another form should be displayed (Detail Form) which takes the full detail of student and saves it in database
if the second button "Edit Student" is clicked then same Detail Form is displayed but this time user is allowed to make changes and save them in database again.
We are NOT focusing here that how to create list and how to save data in database instead we are focusing here a situation when "Edit Student" button is clicked consider the following code:
Dim Student_ID As Integer
Dim frmNew As frmStudentDetail
Student_ID = Me.SelectedStudentID
frmNew = New frmStudentDetail(Student_ID)
frmNew.Show()
Above code is executed when "Edit Student" button is clicked. What actually happens that procedure first gets the Currently Selected Student ID by calling Forms Private Property "SelectedStudentID" then it creates a new instance of frmStudentDetail by passing the Student ID so this form opens up with Student Details to edit.
This works fine if just edit the student and save it. What if user clicks the "Edit Student" Button and after window is opened, goes back in Master Window and again clicks on Edit button then another instance of the Detail Window will appear, which is actually wrong since 2 windows of same student is appearing simaltaneously. We want that whenever "Edit Student" button is clicked then system do:
Check that Form is Opened?
If Yes Then
    Set Focus On That Form
Else
    Create New Instance
In my previous post I have demonstrated how to find a form whether it is opened or NOT. We are going to add few things in order to acheive our goal. For this purpose we will add a Public Readonly Property to Detail Form as:
Private _StudentID As Integer
Public ReadOnly Property StudentID() As Integer
    Get
        Return _StudentID
    End Get
End Property
Public Sub New(ByVal Student_ID As Integer)
    InitializeComponent()
    Me._StudentID = Student_ID
End Sub
In above code StudentID property will tell us about the Detail Form and finally in the "Edit Student" Button Click Event we will change code as:
Dim Student_ID As Integer
Student_ID = Me.SelectedStudentID
For Each frmEach As Form In My.Application.OpenForms
    If TypeOf frmEach Is frmStudentDetail Then
        If CType(frmEach, frmStudentDetail).StudentID = Student_ID Then
            frmEach.Activate()
            Exit Sub
        End If
    End If
Next
Dim frmNew As frmStudentDetail
frmNew = New frmStudentDetail(Student_ID)
frmNew.Show()
In the above loop when the Student Detail window is found then system first check that is this the same window has the same Student ID which is going to be opened then system will activate this form and will Exit the Sub, this will prevent the creation of 2nd instance of same Student ID.