Search code examples
vb.netdevexpressuserformtexteditxtraeditors

Iterate Through Devexpres TextEdit Controls in VB.NET


Could someone help iterating through DevExpress TextEdit controls within an XTRAFORM in vb.net?

What I am actually trying to do is to intercept any value changes at FormClosing event by using EditValue and OldEditValue properties.

I meight need to tell that my controls are contained in XtraTab and XtraPanel Containers.

the following is what I tried:

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

but it says unable to cast XtraTab control to TextEdit control.

Your help will be much appreciated.


Solution

  • To make your code works just change your code snippet as follows:

    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
    

    To detect changes for all the editors within a Form use the following approach:

    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