troubleshooting Question

Assigning the same code to multiple controls

Avatar of Terrygordon
Terrygordon asked on
Visual Basic.NET
28 Comments2 Solutions170 ViewsLast Modified:

I have 24 textboxes and 24 sliders on a tabpage that are linked to each other (I.e. change the textbox value and the slider value changes - change the slider value and the textbox value changes). The code for a single pair of controls (kindly suggested by an EE expert) is as follows:

    Private mblnChangingValue As Boolean = False
    Private Sub Gtrackbar1_ValueChanged(sender As Object, e As EventArgs) Handles GTrackBar1.ValueChanged
        If mblnChangingValue Then Return

        mblnChangingValue = True
        TextBox1.Text = GTrackBar1.Value
        mblnChangingValue = False
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        If mblnChangingValue Then Return

        Dim intNewValue As Integer
        Integer.TryParse(TextBox1.Text, intNewValue)
        If intNewValue > GTrackBar1.MinValue AndAlso intNewValue < GTrackBar1.MaxValue Then
            mblnChangingValue = True
            GTrackBar1.Value = intNewValue
            mblnChangingValue = False
        End If
    End Sub

What I am wondering is, whether there is any way to handle this for all 24 sliders and textboxes, without having to repeat the above code 24 times (times 7 tabpages). For example, is it possible to assigned a variable name to the control in the above code, so that the execution of the code depends on which pair of controls is being adjusted? Each pair has a matching control number (e.g. textbox5 and  GTrackBar5, if that helps.  Incidentally, the sliders are custom controls, just in case some of the properties are confusing.

If this is possible, I can use the same principle on numerous other functions of the application, hopefully saving a few thousand lines of code and a day cutting, pasting and changing control names.

Any ideas greatly appreciated.


Éric Moreau
Senior .Net Consultant
Join our community to see this answer!
Unlock 2 Answers and 28 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 28 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros