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()

Dani AI

Generated

Good start, — and useful contributions from and . ’s snippet shows the basic idea (attach a shared handler), but it only looks for TextBox at the top level and special-cases TabControl. For a production-ready approach you want three things: recurse every container (tab pages, panels, user controls), include MaskedTextBox and ComboBox (and other text-like controls), and avoid marking the form "dirty" when you change control values from code (your save routine).

A compact, robust pattern is: walk the control tree recursively, attach TextChanged to text-like controls and SelectedIndexChanged to combo boxes, use a simple suppress-flag while making programmatic updates, and subscribe to ControlAdded on containers if controls can be created at runtime. Example (call AttachHandlers(Me) from Form.Load):

Private saved As Boolean = True
Private suppressEvents As Boolean = False

Private Sub AttachHandlers(parent As Control)
    For Each c As Control In parent.Controls
        If TypeOf c Is TextBox Then
            AddHandler DirectCast(c, TextBox).TextChanged, AddressOf AnyChanged
        ElseIf TypeOf c Is MaskedTextBox Then
            AddHandler DirectCast(c, MaskedTextBox).TextChanged, AddressOf AnyChanged
        ElseIf TypeOf c Is RichTextBox Then
            AddHandler DirectCast(c, RichTextBox).TextChanged, AddressOf AnyChanged
        ElseIf TypeOf c Is ComboBox Then
            Dim cb = DirectCast(c, ComboBox)
            AddHandler cb.TextChanged, AddressOf AnyChanged
            AddHandler cb.SelectedIndexChanged, AddressOf AnyChanged
        End If

        If c.HasChildren Then AttachHandlers(c)
    Next
End Sub

Private Sub AnyChanged(sender As Object, e As EventArgs)
    If suppressEvents Then Return
    saved = False
End Sub

When saving or populating controls in code, wrap changes so they do not set the dirty flag:

suppressEvents = True
Try
    ' programmatic updates here
Finally
    suppressEvents = False
End Try
saved = True

Troubleshooting tips: if controls are added dynamically, handle their parent.ControlAdded to call AttachHandlers for the new control; use TextBox.Modified for per-control user-edit detection when useful; and prefer SelectedIndexChanged for combo selection-only changes. This approach handles nested tab pages, masked boxes and combo boxes without repeating handlers for every control manually.

Recommended Answers

All 3 Replies

please visit this link , hope this will help you to solve your prob.
Click Here

Regards

This is a nice information. it will be useful my website . once again thanks for the info

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
                    Next
                Next

            End If

        Next

    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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.