Avatar of Terrygordon
Terrygordon

asked on 

Assigning the same code to multiple controls

Hi

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.

Regards

Terry
Visual Basic.NET

Avatar of undefined
Last Comment
Terrygordon

8/22/2022 - Mon