Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Assigning the same code to multiple controls

Avatar of Terrygordon
Terrygordon asked on
Visual Basic.NET
28 Comments1 Solution169 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.


Avatar of Éric Moreau
Éric MoreauFlag of Canada imageSenior .Net Consultant
This problem has been solved!
Unlock 1 Answer and 28 Comments.
See Answers