通过VB.NET中的Devexpres TextEdit控件进行迭代

时间:2014-07-14 12:38:07

标签: vb.net devexpress userform textedit xtraeditors

有人可以帮助在vb.net的XTRAFORM中迭代DevExpress TextEdit控件吗?

我实际上要做的是使用EditValue和OldEditValue属性拦截FormClosing事件中的任何值更改。

我可能需要告诉我的控件包含在XtraTab和XtraPanel容器中。

以下是我的尝试:

Public Function TextEditChangesOccured(frm As XtraForm) As Boolean
    Dim result As Boolean
    For Each ctrl As BaseEdit In frm.Controls
        If TypeOf ctrl Is TextEdit Then
            If ctrl.EditValue <> ctrl.OldEditValue Then
                result = True
            Else
                result = False
            End If
        End If
    Next
    Return result
End Function

 Private Sub MyXtraForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    If TextEditChangesOccured(Me) Then
        DevExpress.XtraEditors.XtraMessageBox.Show("Changes have occured!", My.Application.Info.AssemblyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If
End Sub

但它表示无法将XtraTab控件转换为TextEdit控件。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

要使您的代码正常工作,只需更改您的代码段,如下所示:

Public Function TextEditChangesOccured(container As Control) As Boolean
    Dim result As Boolean
    For Each ctrl As Control In container.Controls
        Dim bEdit As BaseEdit = TryCast(ctrl, BaseEdit)
        If bEdit IsNot Nothing Then
            Dim tEdit As TextEdit = TryCast(ctrl, TextEdit)
            If tEdit IsNot Nothing Then
                result = result Or (bEdit.EditValue <> bEdit.OldEditValue)
            End If
        Else
            result = result Or TextEditChangesOccured(ctrl)
        End If
    Next
    Return result
End Function

要检测表单中所有编辑器的更改,请使用以下方法:

Partial Public Class Form1
    Inherits Form

    Public Sub New()
        InitializeComponent()
        SubscribeTextEditValueChanged(Me)
    End Sub
    Private Sub SubscribeTextEditValueChanged(ByVal container As Control)
        For Each ctrl As Control In container.Controls
            Dim tEdit As TextEdit = TryCast(ctrl, TextEdit)
            If tEdit IsNot Nothing Then
                AddHandler tEdit.EditValueChanged, AddressOf tEdit_EditValueChanged
            Else
                SubscribeTextEditValueChanged(ctrl)
            End If
        Next ctrl
    End Sub
    Private IsEditValueChanged As Boolean
    Private Sub tEdit_EditValueChanged(ByVal sender As Object, ByVal e As EventArgs)
        IsEditValueChanged = True
    End Sub
    Protected Overrides Sub OnClosing(ByVal e As CancelEventArgs)
        If IsEditValueChanged Then
            ' do some stuff
        End If
        MyBase.OnClosing(e)
    End Sub
End Class