mdi保存到mdichild的最佳方法 [英] Best way to mdi save to mdichild

查看:210
本文介绍了mdi保存到mdichild的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必须有一个更简单的方法来做到这一点...



 '   MDI在子表单上调用保存例程的父代码 

私有 Sub SaveToolStripButton_Click( ByVal sender As System。 对象 ByVal e As System.EventArgs )句柄 SaveToolStripButton.Click


如果 TypeOf (MDICurrentForm) frmActivityLogViewer 然后
< span class =code-keyword> Dim f As frmActivityLogViewer = MDICurrentForm
f.SaveNow()
结束 如果

如果 TypeOf (MDICurrentForm) frmUserDetail 然后
Dim f As frmUserDetail = MDICurrentForm
f。 SaveNow()
结束 如果

如果 TypeOf (MDICurrentForm) frmActivityLogViewer 然后
Dim f As frmActivityLogViewer = MDICurrentForm
f.SaveNow()
结束 如果
如果 TypeOf (MDICurrentForm) frmCodingDocDetail < span class =code-keyword>然后
Dim f 作为 frmCodingDocDetail = MDICurrentForm
f.SaveNow()
结束 如果

如果 TypeOf (MDICurrentForm) FrmDBIterateRecords 然后
' 什么都不做,保存此表单类型不是选项
结束 如果

如果 TypeOf (MDICurrentForm) FrmDbNewRecords 然后
' < span class =code-comment>什么都不做,保存不是这种表单类型的选项
结束 如果

如果 TypeOf (MDICurrentForm) frmActivityDetail 然后
Dim f As frmActivityDetail = MDICurrentForm
f.SaveNow()
结束 如果

如果 TypeOf (MDICurrentForm) frmCorporateDocDetail 然后
Dim f As frmCorporateDocDetail = MDICurrentForm
f.SaveNow()
结束 如果

如果 TypeOf (MDICurrentForm) frmHRDocDetail 然后
Dim f As frmHRDocDetail = MDICurrentForm
f.SaveNow()
结束 如果

如果 TypeOf (MDICurrentForm) frmKBArticleDetail 然后
Dim f As frmKBArticleDet ail = MDICurrentForm
f.SaveNow()
结束 如果

如果 TypeOf (MDICurrentForm) frmManagementDocDetail 然后
Dim f As frmManagementDocDetail = MDICurrentForm
f.SaveNow()
结束 如果

如果 TypeOf (MDICurrentForm) frmMarketingDocDetail 然后
Dim f 作为 frmMarketingDocDetail = MDICurrentForm
f.SaveNow()
结束 如果

结束 Sub





我的尝试:



此代码有效,但必须有一个更短,更正确的方法来执行此操作。



我已经尝试从包含savenow方法的表单继承所有表单。



然后......



Dim f As FrmFormTemplate = Me.ActiveMdiChild

f.SaveNow



但是我不能将我的保存代码放入模板中,因为每个表单都有不同的程序。



我需要一个理由使它更短更好(它很复杂但不仅仅是保存...当有未保存的更改时启用和禁用保存按钮,处理更多按钮和功能而不仅仅是保存按钮,以及处理它们时启用和禁用等。我需要一个月的星期日来做这个长手,我的代码会过于臃肿来启动)



真的卡住了,谢谢我可以获得任何帮助:)

解决方案

使用接口 [<一个href =https://msdn.microsoft.com/en-us/library/28e2e18x.aspx\"target =_ blanktitle =New Window> ^ ]。

演练:创建和实现接口(Visual Basic) [ ^ ]



 公共 接口 ISaveableForm 
Sub SaveNow()
结束 接口
...
私有 Sub SaveToolStripButton_Click( ByVal 发​​件人作为系统。对象 ByVal e As System.EventArgs)句柄 SaveToolStripButton.Click
Dim 形式 As ISaveableForm = TryCast (MDICurrentForm,ISaveableForm)
如果形式 IsNot Nothing 然后
form.SaveNow()
结束 如果
结束 Sub



以任何需要保存的形式实施界面:

 公开 部分  frmActivityLogViewer 
实现 ISaveableForm

公共 < span class =cod e-keyword> Sub SaveNow() Implements ISaveableForm.SaveNow
...
结束 Sub
结束


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 :)

解决方案

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


这篇关于mdi保存到mdichild的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆