Click here to Skip to main content
14,085,313 members
Rate this:
 
Please Sign up or sign in to vote.
There must be an easier way to do this...

' 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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

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:
Public Partial Class frmActivityLogViewer
    Implements ISaveableForm
    
    Public Sub SaveNow() Implements ISaveableForm.SaveNow
        ...
    End Sub
End Class
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190518.1 | Last Updated 18 Feb 2016
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100