Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There must be an easier way to do this...

VB
' MDI Parent code calling save routine on child form

   Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click


        If TypeOf (MDICurrentForm) Is frmActivityLogViewer Then
            Dim f As frmActivityLogViewer = MDICurrentForm
            f.SaveNow()
        End If

        If TypeOf (MDICurrentForm) Is frmUserDetail Then
            Dim f As frmUserDetail = MDICurrentForm
            f.SaveNow()
        End If

        If TypeOf (MDICurrentForm) Is frmActivityLogViewer Then
            Dim f As frmActivityLogViewer = MDICurrentForm
            f.SaveNow()
        End If
        If TypeOf (MDICurrentForm) Is frmCodingDocDetail Then
            Dim f As frmCodingDocDetail = MDICurrentForm
            f.SaveNow()
        End If

        If TypeOf (MDICurrentForm) Is FrmDBIterateRecords Then
            ' do nothing, save is not an option with this form type
        End If

        If TypeOf (MDICurrentForm) Is FrmDbNewRecords Then
            ' do nothing, save is not an option with this form type
        End If

        If TypeOf (MDICurrentForm) Is frmActivityDetail Then
            Dim f As frmActivityDetail = MDICurrentForm
            f.SaveNow()
        End If

        If TypeOf (MDICurrentForm) Is frmCorporateDocDetail Then
            Dim f As frmCorporateDocDetail = MDICurrentForm
            f.SaveNow()
        End If

        If TypeOf (MDICurrentForm) Is frmHRDocDetail Then
            Dim f As frmHRDocDetail = MDICurrentForm
            f.SaveNow()
        End If

        If TypeOf (MDICurrentForm) Is frmKBArticleDetail Then
            Dim f As frmKBArticleDetail = MDICurrentForm
            f.SaveNow()
        End If

        If TypeOf (MDICurrentForm) Is frmManagementDocDetail Then
            Dim f As frmManagementDocDetail = MDICurrentForm
            f.SaveNow()
        End If

        If TypeOf (MDICurrentForm) Is frmMarketingDocDetail Then
            Dim f As frmMarketingDocDetail = MDICurrentForm
            f.SaveNow()
        End If

    End Sub


What I have tried:

This code works, but there must be a shorter and more proper way to do this.

I've tried having all of my forms inheriting from a form that contains a savenow method.

Then ...

Dim f As FrmFormTemplate = Me.ActiveMdiChild
f.SaveNow

But I can't place my save code into the template, as it is different procedure for each form.

There is a reason I need to make it shorter and better (it's complicated but there is a lot more than just saving... enabling and disabling save button when there are unsaved changes, and handling of many more buttons and functions than just the save button, and handling when they enable and disable etc.. It would take me a month of Sundays to do this long hand and my code would be so over bloated to boot)

Really stuck, thanks for any help I can get :)
Posted
Updated 18-Feb-16 5:02am
v2

1 solution

Use an Interface[^].
Walkthrough: Creating and Implementing Interfaces (Visual Basic)[^]

VB.NET
Public Interface ISaveableForm
    Sub SaveNow()
End Interface
...
Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click
    Dim form As ISaveableForm = TryCast(MDICurrentForm, ISaveableForm)
    If form IsNot Nothing Then
        form.SaveNow()
    End If
End Sub

Implement the interface in any form that needs to be saved:
VB.NET
Public Partial Class frmActivityLogViewer
    Implements ISaveableForm
    
    Public Sub SaveNow() Implements ISaveableForm.SaveNow
        ...
    End Sub
End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900