我想问一下,当有子窗体运行时,不能关闭父窗体,或关闭时出现提示,如WORD提示保存文件一样

解决方案 »

  1.   


    1.父窗体调用 子窗体 用以下方式:子窗体.Show vbmodal 父窗体2.在子窗体中写入以下代码:Option ExplicitPrivate Sub Form_Unload(Cancel As Integer)
      If MsgBox("要关闭窗体吗?", vbYesNo) = vbYes Then
        Cancel = 1
      Else
        Cancel = 0
      End If
    End Sub
      

  2.   

    '上面那个子窗体中的代码反了.
    1.父窗体调用 子窗体 用以下方式: 子窗体.Show vbmodal 父窗体 2.在子窗体中写入以下代码: Option Explicit Private Sub Form_Unload(Cancel As Integer) 
      If MsgBox("要关闭窗体吗?", vbYesNo) = vbYes Then 
        Cancel = 0 
      Else 
        Cancel = 1 
      End If 
    End Sub 
      

  3.   

    在父窗体Form_QueryUnload事件里用API判断是否还有子窗体在运行,有就不关闭或提示保存
      

  4.   

    楼主要说明一下,你所说的父窗体,是否指MDI的主窗体?
    如果是的话,用3楼的办法不错!
    省得在每个子窗体中写退出代码
      

  5.   

    我举个例子:
    有一个MDI窗口,创建了3个文档
    第一个文档是直接打开的,没有任何改动(不需要提示保存)
    第二个文档是新建的文档,写了些信息没有保存过(需要提示保存路径并进行保存)
    第三个文档是打开的一个文档文件并进行了修改(需要覆盖原来的文件直接保存)这时,如果关闭主窗口的话应该提示保存第二个文档,并让用户选择保存路径再保存文档内容。
    也会提示是否保存第三个文档,如果保存就直接替换原来文档的内容,最后就可以关闭窗口了。如果按3楼的方法
    来实现无疑是绕很大的远路,而且不使用对象内自己对自己内容的处理将会很麻烦。
    如果要保存的内容复杂点,比如带图层的图像,那么就更加麻烦了。
    如果用API在主窗口来弄,只能说是绕远路找麻烦。而且程序的思路会非常混乱我写了个范例,大家可以照做看看,最起码思路是很清晰的:1、 创建一个标准EXE工程
    2、 添加部件(ActiveX控件),将 Microsoft Common Dialog Control 6.0 控件添加到工具箱
    3、 添加 MDI 窗口(采用默认属性),默认名称为 MDIForm1
    4、 修改 Form1 窗口的 MDIChild 属性值为 True
    5、 设置启动对象为 MDIForm1
    6、 为 MDI 主窗口添加以下菜单项
        文件(&F)            名称:menuFiles
        ....新建(&N)        名称:menuNewFile
        ....打开(&O)...     名称:menuOpenFile
        ....保存(&S)...     名称:menuSaveFile
        ....另存为(&C)...   名称:menuSaveFileCopy
    7、 将 CommonDialog 控件放入 MDI 主窗口 MDIForm1 中,采用默认名称 CommonDialog1
    8、 将 CommonDialog 控件放入 MDI 子窗口 Form1 中,采用默认名称 CommonDialog1
    9、 将 TextBox 控件放入 MDI 子窗口 Form1 中,采用默认名称 Text1
    10、设置 Text1 的 MultiLine 属性值为 True
    11、设置 Text1 的 ScrollBars 属性值为 3 - Both
    12、将以下代码作为 MDI 主窗口 MDIForm1 的全部代码'FileName = MDIForm1.frm
    Public NowObject As Form1, FileCount As Long
    Private Sub MDIForm_Load()
       FileCount = 0
       CommonDialog1.CancelError = True
       menuSaveFile.Enabled = False
       menuSaveFileCopy.Enabled = False
    End SubPrivate Sub menuNewFile_Click()
       Dim NewWindow As Form1
       Set NewWindow = New Form1
       NewWindow.NewFile
    End SubPrivate Sub menuOpenFile_Click()
       CommonDialog1.Filter = "文本文件(*.TXT)|*.TXT|所有文件(*.*)|*.*"
       On Error Resume Next
       CommonDialog1.ShowOpen
       If Err.Number <> 0 Then
          Err.Clear
          Exit Sub
       End If
       Dim NewWindow As Form1
       Set NewWindow = New Form1
       NewWindow.OpenFile CommonDialog1.FileName
    End SubPrivate Sub menuSaveFile_Click()
       NowObject.SaveFile
    End SubPrivate Sub menuSaveFileCopy_Click()
       NowObject.FilePath = ""
       NowObject.IsSave = True
       NowObject.SaveFile
    End Sub
    13、将以下代码作为 MDI 子窗口 Form1 的全部代码'FileName = Form1.frm
    Public IsSave As Boolean
    Public FilePath As String
    Public FileTitle As String
    Private Sub Form_Load()
       CommonDialog1.CancelError = True
    End SubPublic Sub NewFile()
       FilePath = ""
       Text1.Text = ""
       IsSave = False
       MDIForm1.FileCount = MDIForm1.FileCount + 1
       FileTitle = "新建文本(" & MDIForm1.FileCount & ").TXT"
       Me.Caption = FileTitle
    End SubPublic Sub OpenFile(FileName As String)
       FilePath = FileName
       
       Dim fc As Integer
       Dim TempStr As String
       Dim FileValue As String
       Dim i As Long
       
       i = 0
       fc = FreeFile
       Open FileName For Input As #fc
       Do While Not EOF(fc)
       Line Input #fc, TempStr
       i = i + 1
       If i = 1 Then
          FileValue = TempStr
       Else
          FileValue = FileValue & vbCrLf & TempStr
       End If
       Loop
       Close #fc
       Text1.Text = FileValue
       IsSave = False
       FileTitle = Dir(FilePath, vbHidden + vbSystem + vbReadOnly + vbArchive)
       Me.Caption = FileTitle
       Me.Show
    End SubPublic Sub SaveFile(Optional FileName As String = "")
       If Len(FileName) = 0 Then
          If Len(FilePath) = 0 Then
             CommonDialog1.Filter = "文本文件(*.TXT)|*.TXT|所有文件(*.*)|*.*"
             CommonDialog1.FileName = FileTitle
             On Error Resume Next
             CommonDialog1.ShowSave
             If Err.Number = 0 Then
                FilePath = CommonDialog1.FileName
             Else
                Err.Clear
                Exit Sub
             End If
          End If
       Else
          FilePath = FileName
       End If
       Dim fc As Integer
       fc = FreeFile
       Open FilePath For Output As #fc
       Print #fc, Text1.Text
       Close #fc
       FileTitle = Dir(FilePath, vbHidden + vbSystem + vbReadOnly + vbArchive)
       Me.Caption = FileTitle
       IsSave = False
    End SubPrivate Sub Form_Activate()
       Set MDIForm1.NowObject = Me
       MDIForm1.menuSaveFile.Enabled = True
       MDIForm1.menuSaveFileCopy.Enabled = True
    End SubPrivate Sub Form_Resize()
       On Error Resume Next
       Text1.Move 0, 0, Me.Width, Me.Height
    End SubPrivate Sub Form_Unload(Cancel As Integer)
       If IsSave = True Then
          If MsgBox("您是否保存 " & FileTitle & " 文件?", 32 + vbYesNo, "保存询问") = vbYes Then
             If Len(FilePath) = 0 Then
                CommonDialog1.Filter = "文本文件(*.TXT)|*.TXT|所有文件(*.*)|*.*"
                CommonDialog1.FileName = FileTitle
                On Error Resume Next
                CommonDialog1.ShowSave
                If Err.Number = 0 Then
                   FilePath = CommonDialog1.FileName
                Else
                   Err.Clear
                   Exit Sub
                End If
             End If
             Dim fc As Integer
             fc = FreeFile
             Open FilePath For Output As #fc
             Print #fc, Text1.Text
             Close #fc
          End If
       End If
       MDIForm1.menuSaveFile.Enabled = False
       MDIForm1.menuSaveFileCopy.Enabled = False
    End SubPrivate Sub Text1_Change()
       IsSave = True
    End Sub
    14、运行程序进行测试我已经将这个程序放到了我CSDN的资源里,大家也可以下载来看看。
    http://supermanking.download.csdn.net/
    名称是:vb多文档保存及处理的范例