【问题标题】:VB.Net UserControl Closing Event in WinformsWinforms中的VB.Net UserControl关闭事件
【发布时间】:2010-03-04 16:43:19
【问题描述】:

我正在尝试从 DevExpress Grid Controls 中保存一些布局,以便用户可以更改布局并在以后使用该控件时重新加载它们。

我的问题是速度问题,我通过表单内的用户控件加载控件。现在我的问题是我通过在选项卡可见时将控件的实例添加到选项卡式组控件内的面板控件来创建控件,然后在控件隐藏时清除控件。

    If ClaimsGridPanelControl.Visible = True Then
            ClaimsGridPanelControl.Controls.Add(New RXClaimsGridControl(ClaimsBindingSource))
    Else
            ClaimsGridPanelControl.Controls.Clear()
    End If

所以在 RxClaimGridControl 内部,我需要在清除控件时调用 SaveLayout 方法。但是至少我可以找到没有任何事件会在用户控件被删除/关闭/隐藏时触发。

我处理 .Clear() 的想法是在父控件中引发一个事件,然后在用户控件中处理该事件。

在删除/关闭/隐藏用户控件方面,我是否遗漏了一些事件,或者有更好的方法来做到这一点。

【问题讨论】:

    标签: .net vb.net winforms events


    【解决方案1】:

    覆盖 UserControl 的 DisposeOnHandleDestroyed 方法。

    【讨论】:

    • 不得不对我清理表格的方式进行轻微改变。从 .Clear 更改为 For Each 循环遍历面板控件集合中的所有控件并在控件上调用 dispose。
    【解决方案2】:

    我在http://lukhezo.com/2007/10/10/usercontrol-closing-event/找到了这个

    Protected Overloads Overrides Sub OnCreateControl()
    
        MyBase.OnCreateControl()
    
        AddHandler Me.ParentForm.FormClosing, AddressOf ParentForm_FormClosing
    
    End Sub
    
    Private Sub ParentForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
    
        If MessageBox.Show("Would you like to close the parent form?", "Close parent form?", _
    
                           MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then
    
            e.Cancel = True
    
        End If
    
    End Sub
    

    然后您可以在 ParentForm_FormClosing 事件中调用 client.Close()(或任何您想要的操作)而不是 MessageBox.Show()

    我的评论:您可以将 AddHandler 语句放在 UserControl Load 事件中。

    虽然这个问题是 5 年前提出的,但我认为这个解决方案很好。

    【讨论】:

      【解决方案3】:

      我相信您从错误的地方调用 SaveLayout()。仅当对 DXGrid 本身进行更改且与面板无关时才应保存布局。

      你可以通过处理

      GridView_ShowCustomizationForm
      

      示例代码

        Private Sub GridView_ShowCustomizationForm(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridView.ShowCustomizationForm
          Dim gView As DevExpress.XtraGrid.Views.Grid.GridView = CType(sender, DevExpress.XtraGrid.Views.Grid.GridView)
          AddHandler gView.CustomizationForm.FormClosing, AddressOf SaveGridSettings
      End Sub
      
      Private Sub SaveGridSettings()
      
       Grid.MainView.SaveLayoutToXml("c:\Settings.xml")
      
      End Sub
      

      【讨论】:

      • 如果用户使用自定义表单来自定义网格控件,就会出现这种情况。这可以处理他们以任何方式对其进行自定义的情况,只需使用分组方法拖动列即可。然而,这是我处理保存普通 LayoutControl 的方式,因为它提供了每次打开和关闭控件时都不会保存控件的情况。我同意可能使用 CustomizationForm 作为自定义网格的方法,然后仅使用该事件可能是一种更有效的方法。
      • 我是根据你所说的“我正在尝试从 DevExpress Grid Controls 中保存一些布局...”
      • 是的,我不想实际保存网格布局,因为这不是问题,它在 UserControl 被“关闭”时捕获
      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      相关资源
      最近更新 更多