I'm trying to have my winform check for changes in textboxes, combo boxes and masked textboxes. I've found a way for one textbox, but how can I do this for all of the textboxes as well as ones on other tabpages?

 Private Sub Textbox_Textchanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EVENT_LOCATION.TextChanged
        'Shows that most recent changes were not saved
        saved = False
    End Sub

Private Sub PFC_XPRESS_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

        Dim Result As DialogResult

        If EVENT_LOCATION.Text <> "" Then
            'If the most recent changes are unsaved, we have this option

            If saved = False Then

                Result = MessageBox.Show("Would you like to save changes?", "Confirm Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

                If Result = Windows.Forms.DialogResult.Yes Then

                Call Save_Event()
4 Years
Discussion Span
Last Post by Reverend Jim

For the following code I created a blank form and added one textbox. I also added a tab control with two pages. Each page has two textbox controls. When you run the code, whenever you type in any taxtbox the complete text in that box will be displayed in the title bar. The outer loop cycles through all controls on the form. If a tab control is found then each page is checked for text controls. Whenever a text control is found, the common TextChanged handler is attached to it.

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'check all controls at the top level

        For Each ctrl As Control In Me.Controls

            If TypeOf (ctrl) Is TextBox Then
                Dim tbx As TextBox = ctrl
                AddHandler tbx.TextChanged, AddressOf TextBox_TextChanged
            ElseIf TypeOf (ctrl) Is TabControl Then

                'check all controls on all tabs

                Dim tabctrl As TabControl = ctrl

                For Each page As Control In tabctrl.TabPages
                    For Each tbx As TextBox In page.Controls.OfType(Of TextBox)()
                        Debug.WriteLine("add " & tbx.Name)
                        AddHandler tbx.TextChanged, AddressOf TextBox_TextChanged

            End If


    End Sub

    Private Sub TextBox_TextChanged(sender As System.Object, e As System.EventArgs)
        Dim tbx As TextBox = sender
        Me.Text = tbx.Text
    End Sub

End Class

Edited by Reverend Jim

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.