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
TerrygordonAsked:
Who is Participating?
 
Éric MoreauSenior .Net ConsultantCommented:
something like this to loop recursively through the controls:
    Private mblnChangingValue As Boolean = False

    Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged, TextBox5.TextChanged, TextBox6.TextChanged, TextBox7.TextChanged, TextBox8.TextChanged, TextBox9.TextChanged
        If mblnChangingValue Then Return

        Dim objTextBox As TextBox = DirectCast(sender, TextBox)

        Dim intIndice As Integer
        Integer.TryParse(objTextBox.Name.Trim().ToUpper().Replace("TEXTBOX", ""), intIndice)

        Dim objTrackBar As gTrackBar.gTrackBar = FindTrackBar(Controls, "GTrackBar" + intIndice.ToString()) ' CType(Controls("GTrackBar" + intIndice.ToString()), gTrackBar.gTrackBar)

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

    Private Sub TrackBar1_ValueChanged(sender As Object, e As EventArgs) Handles GTrackBar1.ValueChanged, GTrackBar2.ValueChanged, GTrackBar3.ValueChanged, GTrackBar4.ValueChanged, GTrackBar5.ValueChanged, GTrackBar6.ValueChanged, GTrackBar7.ValueChanged, GTrackBar8.ValueChanged, GTrackBar9.ValueChanged
        If Not Visible Then Return
        If mblnChangingValue Then Return

        Dim objTrackBar As gTrackBar.gTrackBar = DirectCast(sender, gTrackBar.gTrackBar)

        Dim intIndice As Integer
        Integer.TryParse(objTrackBar.Name.Trim().ToUpper().Replace("GTRACKBAR", ""), intIndice)

        Dim objTextBox As TextBox = FindTextBox(Controls, "TextBox" + intIndice.ToString()) ' CType(Controls("TextBox" + intIndice.ToString()), TextBox)

        mblnChangingValue = True
        objTextBox.Text = objTrackBar.Value.ToString()
        mblnChangingValue = False
    End Sub

    Private Function FindTextBox(ByVal pControls As Control.ControlCollection, ByVal pName As String) As TextBox
        Dim objTextBox As TextBox = CType(pControls(pName), TextBox)
        If objTextBox IsNot Nothing Then Return objTextBox

        For Each c As Control In pControls
            If c.Controls IsNot Nothing Then
                objTextBox = FindTextBox(c.Controls, pName)
                If objTextBox IsNot Nothing Then Return objTextBox
            End If
        Next
    End Function

    Private Function FindTrackBar(ByVal pControls As Control.ControlCollection, ByVal pName As String) As gTrackBar.gTrackBar
        Dim objTrackBar As gTrackBar.gTrackBar = CType(pControls(pName), gTrackBar.gTrackBar)
        If objTrackBar IsNot Nothing Then Return objTrackBar

        For Each c As Control In pControls
            If c.Controls IsNot Nothing Then
                objTrackBar = FindTrackBar(c.Controls, pName)
                If objTrackBar IsNot Nothing Then Return objTrackBar
            End If
        Next
    End Function

End Class

Open in new window

0
 
Éric MoreauSenior .Net ConsultantCommented:
If you have full controls over the name of your controls, you can use code like this (be sure to list all your controls in the Handles clause):

Public Class Form1

    Private mblnChangingValue As Boolean = False

    Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged, TextBox5.TextChanged
        If mblnChangingValue Then Return

        Dim objTextBox As TextBox = DirectCast(sender, TextBox)

        Dim intIndice as Integer
        Integer.TryParse(objTextBox.Name.Trim().ToUpper().Replace("TEXTBOX", ""), intIndice)

        Dim objTrackBar As TrackBar = CType(Controls("TrackBar" + intIndice.ToString()), TrackBar)

        Dim intNewValue As Integer
        Integer.TryParse(objTextBox.Text, intNewValue)
        If intNewValue > objTrackBar.Minimum AndAlso intNewValue < objTrackBar.Maximum Then
            mblnChangingValue = True
            objTrackBar.Value = intNewValue
            mblnChangingValue = False
        End If
    End Sub

    Private Sub TrackBar1_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar1.ValueChanged, TrackBar2.ValueChanged, TrackBar3.ValueChanged, TrackBar4.ValueChanged, TrackBar5.ValueChanged
        If mblnChangingValue Then Return

        Dim objTrackBar As TrackBar = DirectCast(sender, TrackBar)

        Dim intIndice As Integer
        Integer.TryParse(objTrackBar.Name.Trim().ToUpper().Replace("TRACKBAR", ""), intIndice)

        Dim objTextBox As TextBox = CType(Controls("TextBox" + intIndice.ToString()), TextBox)

        mblnChangingValue = True
        objTextBox.Text = objTrackBar.Value.ToString()
        mblnChangingValue = False
    End Sub
End Class

Open in new window

0
 
David L. HansenProgrammer AnalystCommented:
I'd have a customized function that takes in a textbox and slider as inputs where your current logic is implemented. Each control's event of interest (ie. Text_Changed) can be delegated to (or passed to) your custom function:

Something kind of like this (I threw this together quickly but I hope the idea comes through...happy to help you iron it out if needed).
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        combineFunctions(Me.TextBox1, Me.Slider1)
End Sub

Private Sub combineFunctions(iTxt as textbox, iSlider as Trackbar)
     iTxt.Text = iSlider.Value
      mblnChangingValue = False

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

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
it_saigeDeveloperCommented:
Using Eric's solution, you could also early map the instances so that you don't have to always compare by name; e.g. -

Form1.vb -
Imports System.Runtime.CompilerServices

Public Class Form1
    Private mappings As List(Of Tuple(Of Control, Control))
    Private IsChangingValue As Boolean = False

    Private Sub OnLoad(sender As Object, e As EventArgs) Handles MyBase.Load
        If mappings Is Nothing Then
            mappings = (From tb In Me.Controls.GetChildren(GetType(TextBox))
                        Join trk In Me.Controls.GetChildren(GetType(TrackBar))
                        On tb.Name.ToUpper().Replace("TEXTBOX", "") Equals trk.Name.ToUpper().Replace("TRACKBAR", "")
                        Select New Tuple(Of Control, Control)(tb, trk)).ToList()
        End If
    End Sub

    Private Sub OnTextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged, TextBox3.TextChanged, TextBox2.TextChanged, TextBox1.TextChanged
        If TypeOf sender Is TextBox And Not IsChangingValue Then
            Dim tb = DirectCast(sender, TextBox)
            Dim mapping = (From m In mappings Where m.Item1.Equals(tb) Select m).FirstOrDefault()
            If mapping IsNot Nothing Then
                Dim newValue As Integer
                If Integer.TryParse(mapping.Item1.Text, newValue) AndAlso newValue > CType(mapping.Item2, TrackBar).Minimum AndAlso newValue < CType(mapping.Item2, TrackBar).Maximum Then
                    IsChangingValue = True
                    CType(mapping.Item2, TrackBar).Value = newValue
                    IsChangingValue = False
                End If
            End If
        End If
    End Sub

    Private Sub OnValueChanged(sender As Object, e As EventArgs) Handles TrackBar4.ValueChanged, TrackBar3.ValueChanged, TrackBar2.ValueChanged, TrackBar1.ValueChanged
        If TypeOf sender Is TrackBar And Not IsChangingValue Then
            Dim trk = DirectCast(sender, TrackBar)
            Dim mapping = (From m In mappings Where m.Item2.Equals(trk) Select m).FirstOrDefault()
            If mapping IsNot Nothing Then
                IsChangingValue = True
                mapping.Item1.Text = CType(mapping.Item2, TrackBar).Value
                IsChangingValue = False
            End If
        End If
    End Sub
End Class

Module Extensions
    <Extension>
    Public Function GetChildren(source As Control.ControlCollection, Optional ByVal filter As Type = Nothing) As IEnumerable(Of Control)
        Dim children As List(Of Control) = New List(Of Control)
        For Each child As Control In source
            If child.GetType() Is If(filter IsNot Nothing, filter, GetType(Control)) Then children.Add(child)
            If child.HasChildren() Then children.AddRange(child.Controls.GetChildren(filter))
        Next
        Return children
    End Function
End Module

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.TrackBar1 = New System.Windows.Forms.TrackBar()
        Me.TrackBar2 = New System.Windows.Forms.TrackBar()
        Me.TextBox2 = New System.Windows.Forms.TextBox()
        Me.TrackBar3 = New System.Windows.Forms.TrackBar()
        Me.TextBox3 = New System.Windows.Forms.TextBox()
        Me.TrackBar4 = New System.Windows.Forms.TrackBar()
        Me.TextBox4 = New System.Windows.Forms.TextBox()
        CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.TrackBar2, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.TrackBar3, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.TrackBar4, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(13, 13)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(100, 20)
        Me.TextBox1.TabIndex = 0
        '
        'TrackBar1
        '
        Me.TrackBar1.Location = New System.Drawing.Point(119, 12)
        Me.TrackBar1.Name = "TrackBar1"
        Me.TrackBar1.Size = New System.Drawing.Size(104, 45)
        Me.TrackBar1.TabIndex = 1
        '
        'TrackBar2
        '
        Me.TrackBar2.Location = New System.Drawing.Point(119, 63)
        Me.TrackBar2.Name = "TrackBar2"
        Me.TrackBar2.Size = New System.Drawing.Size(104, 45)
        Me.TrackBar2.TabIndex = 3
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(13, 64)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(100, 20)
        Me.TextBox2.TabIndex = 2
        '
        'TrackBar3
        '
        Me.TrackBar3.Location = New System.Drawing.Point(119, 114)
        Me.TrackBar3.Name = "TrackBar3"
        Me.TrackBar3.Size = New System.Drawing.Size(104, 45)
        Me.TrackBar3.TabIndex = 5
        '
        'TextBox3
        '
        Me.TextBox3.Location = New System.Drawing.Point(13, 115)
        Me.TextBox3.Name = "TextBox3"
        Me.TextBox3.Size = New System.Drawing.Size(100, 20)
        Me.TextBox3.TabIndex = 4
        '
        'TrackBar4
        '
        Me.TrackBar4.Location = New System.Drawing.Point(119, 165)
        Me.TrackBar4.Name = "TrackBar4"
        Me.TrackBar4.Size = New System.Drawing.Size(104, 45)
        Me.TrackBar4.TabIndex = 7
        '
        'TextBox4
        '
        Me.TextBox4.Location = New System.Drawing.Point(13, 166)
        Me.TextBox4.Name = "TextBox4"
        Me.TextBox4.Size = New System.Drawing.Size(100, 20)
        Me.TextBox4.TabIndex = 6
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(284, 261)
        Me.Controls.Add(Me.TrackBar4)
        Me.Controls.Add(Me.TextBox4)
        Me.Controls.Add(Me.TrackBar3)
        Me.Controls.Add(Me.TextBox3)
        Me.Controls.Add(Me.TrackBar2)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.TrackBar1)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.TrackBar2, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.TrackBar3, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.TrackBar4, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

    Friend WithEvents TextBox1 As TextBox
    Friend WithEvents TrackBar1 As TrackBar
    Friend WithEvents TrackBar2 As TrackBar
    Friend WithEvents TextBox2 As TextBox
    Friend WithEvents TrackBar3 As TrackBar
    Friend WithEvents TextBox3 As TextBox
    Friend WithEvents TrackBar4 As TrackBar
    Friend WithEvents TextBox4 As TextBox
End Class

Open in new window

Produces the following output -Capture.PNG
-saige-
0
 
TerrygordonAuthor Commented:
Hi Eric/Saige

The trackbars I am using are user created custom sliders (they support numerous properties not available on the system trackbar). They have the control name gTrackbar. Unfortunately VB is not recognising "gTrackbar" (as an alternative to "trackbar" in your code) as a valid control.

If it helps, the control name at the top of the properties window is the name of the control followed by gTrackbar.gTrackbar (for example, Gtrackbar1 gTrackbar.gTrackbar).

I am not sufficiently advanced to know why they are not being recognised, or if there is a workaround, so any help would be very welcome.

Regards

Terry
0
 
Éric MoreauSenior .Net ConsultantCommented:
just need to replace trackbar with gtrackbar everywhere as in:

        Dim objTrackBar As GTrackBar = CType(Controls("GTrackBar" + intIndice.ToString()), GTrackBar)

Open in new window

0
 
TerrygordonAuthor Commented:
Hi Eric

I have tried the code with gGTrackbar, but I get a red underline error where it is referred to as a control. Replacing this with gTrackbar.gTrackbar gets rid of the red underline but when I try to run it I get the following error message:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

objTextBox was Nothing.

I will check back tomorrow to see if there is a solution.

Regards

Terry
0
 
it_saigeDeveloperCommented:
Assuming that you are using this control, the the following should work for you:

Form1.vb -
Imports System.Runtime.CompilerServices

Public Class Form1
    Private mappings As List(Of Tuple(Of Control, UserControl))
    Private IsChangingValue As Boolean = False

    Private Sub OnLoad(sender As Object, e As EventArgs) Handles MyBase.Load
        If mappings Is Nothing Then
            mappings = (From tb In Me.Controls.GetChildren(GetType(TextBox))
                        Join trk In Me.Controls.GetChildren(GetType(gTrackBar.gTrackBar))
                        On tb.Name.ToUpper().Replace("TEXTBOX", "") Equals trk.Name.ToUpper().Replace("TRACKBAR", "")
                        Select New Tuple(Of Control, UserControl)(tb, trk)).ToList()
        End If
    End Sub

    Private Sub OnTextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged, TextBox3.TextChanged, TextBox2.TextChanged, TextBox1.TextChanged
        If TypeOf sender Is TextBox And Not IsChangingValue Then
            Dim tb = DirectCast(sender, TextBox)
            Dim mapping = (From m In mappings Where m.Item1.Equals(tb) Select m).FirstOrDefault()
            If mapping IsNot Nothing Then
                Dim newValue As Integer
                If Integer.TryParse(mapping.Item1.Text, newValue) AndAlso newValue > CType(mapping.Item2, gTrackBar.gTrackBar).MinValue AndAlso newValue < CType(mapping.Item2, gTrackBar.gTrackBar).MaxValue Then
                    IsChangingValue = True
                    CType(mapping.Item2, gTrackBar.gTrackBar).Value = newValue
                    IsChangingValue = False
                End If
            End If
        End If
    End Sub

    Private Sub OnValueChanged(sender As Object, e As EventArgs) Handles TrackBar4.ValueChanged, TrackBar3.ValueChanged, TrackBar2.ValueChanged, TrackBar1.ValueChanged
        If TypeOf sender Is gTrackBar.gTrackBar And Not IsChangingValue Then
            Dim trk = DirectCast(sender, gTrackBar.gTrackBar)
            Dim mapping = (From m In mappings Where m.Item2.Equals(trk) Select m).FirstOrDefault()
            If mapping IsNot Nothing Then
                IsChangingValue = True
                mapping.Item1.Text = CType(mapping.Item2, gTrackBar.gTrackBar).Value
                IsChangingValue = False
            End If
        End If
    End Sub
End Class

Module Extensions
    <Extension>
    Public Function GetChildren(source As Control.ControlCollection, Optional ByVal filter As Type = Nothing) As IEnumerable(Of Control)
        Dim children As List(Of Control) = New List(Of Control)
        For Each child As Control In source
            If child.GetType() Is If(filter IsNot Nothing, filter, GetType(Control)) Then children.Add(child)
            If child.HasChildren() Then children.AddRange(child.Controls.GetChildren(filter))
        Next
        Return children
    End Function
End Module

<AttributeUsage(AttributeTargets.All, AllowMultiple:=True)>
Class MappingAttribute
    Inherits Attribute
    Public Property MapTo() As Control
    Public Sub New(mapping As Control)
        MapTo = mapping
    End Sub
End Class

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.TrackBar1 = New gTrackBar.gTrackBar()
        Me.TrackBar2 = New gTrackBar.gTrackBar()
        Me.TextBox2 = New System.Windows.Forms.TextBox()
        Me.TrackBar3 = New gTrackBar.gTrackBar()
        Me.TextBox3 = New System.Windows.Forms.TextBox()
        Me.TrackBar4 = New gTrackBar.gTrackBar()
        Me.TextBox4 = New System.Windows.Forms.TextBox()
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(13, 13)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(100, 20)
        Me.TextBox1.TabIndex = 0
        '
        'TrackBar1
        '
        Me.TrackBar1.BackColor = System.Drawing.SystemColors.Control
        Me.TrackBar1.Label = Nothing
        Me.TrackBar1.Location = New System.Drawing.Point(119, 12)
        Me.TrackBar1.Margin = New System.Windows.Forms.Padding(2, 3, 2, 3)
        Me.TrackBar1.Name = "TrackBar1"
        Me.TrackBar1.Size = New System.Drawing.Size(104, 45)
        Me.TrackBar1.SliderWidthHigh = 1.0!
        Me.TrackBar1.SliderWidthLow = 1.0!
        Me.TrackBar1.TabIndex = 1
        Me.TrackBar1.TickThickness = 1.0!
        Me.TrackBar1.Value = 0
        Me.TrackBar1.ValueAdjusted = 0!
        Me.TrackBar1.ValueDivisor = gTrackBar.gTrackBar.eValueDivisor.e1
        Me.TrackBar1.ValueStrFormat = Nothing
        '
        'TrackBar2
        '
        Me.TrackBar2.BackColor = System.Drawing.SystemColors.Control
        Me.TrackBar2.Label = Nothing
        Me.TrackBar2.Location = New System.Drawing.Point(119, 63)
        Me.TrackBar2.Margin = New System.Windows.Forms.Padding(2, 3, 2, 3)
        Me.TrackBar2.Name = "TrackBar2"
        Me.TrackBar2.Size = New System.Drawing.Size(104, 45)
        Me.TrackBar2.SliderWidthHigh = 1.0!
        Me.TrackBar2.SliderWidthLow = 1.0!
        Me.TrackBar2.TabIndex = 3
        Me.TrackBar2.TickThickness = 1.0!
        Me.TrackBar2.Value = 0
        Me.TrackBar2.ValueAdjusted = 0!
        Me.TrackBar2.ValueDivisor = gTrackBar.gTrackBar.eValueDivisor.e1
        Me.TrackBar2.ValueStrFormat = Nothing
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(13, 64)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(100, 20)
        Me.TextBox2.TabIndex = 2
        '
        'TrackBar3
        '
        Me.TrackBar3.BackColor = System.Drawing.SystemColors.Control
        Me.TrackBar3.Label = Nothing
        Me.TrackBar3.Location = New System.Drawing.Point(119, 114)
        Me.TrackBar3.Margin = New System.Windows.Forms.Padding(2, 3, 2, 3)
        Me.TrackBar3.Name = "TrackBar3"
        Me.TrackBar3.Size = New System.Drawing.Size(104, 45)
        Me.TrackBar3.SliderWidthHigh = 1.0!
        Me.TrackBar3.SliderWidthLow = 1.0!
        Me.TrackBar3.TabIndex = 5
        Me.TrackBar3.TickThickness = 1.0!
        Me.TrackBar3.Value = 0
        Me.TrackBar3.ValueAdjusted = 0!
        Me.TrackBar3.ValueDivisor = gTrackBar.gTrackBar.eValueDivisor.e1
        Me.TrackBar3.ValueStrFormat = Nothing
        '
        'TextBox3
        '
        Me.TextBox3.Location = New System.Drawing.Point(13, 115)
        Me.TextBox3.Name = "TextBox3"
        Me.TextBox3.Size = New System.Drawing.Size(100, 20)
        Me.TextBox3.TabIndex = 4
        '
        'TrackBar4
        '
        Me.TrackBar4.BackColor = System.Drawing.SystemColors.Control
        Me.TrackBar4.Label = Nothing
        Me.TrackBar4.Location = New System.Drawing.Point(119, 165)
        Me.TrackBar4.Margin = New System.Windows.Forms.Padding(2, 3, 2, 3)
        Me.TrackBar4.Name = "TrackBar4"
        Me.TrackBar4.Size = New System.Drawing.Size(104, 45)
        Me.TrackBar4.SliderWidthHigh = 1.0!
        Me.TrackBar4.SliderWidthLow = 1.0!
        Me.TrackBar4.TabIndex = 7
        Me.TrackBar4.TickThickness = 1.0!
        Me.TrackBar4.Value = 0
        Me.TrackBar4.ValueAdjusted = 0!
        Me.TrackBar4.ValueDivisor = gTrackBar.gTrackBar.eValueDivisor.e1
        Me.TrackBar4.ValueStrFormat = Nothing
        '
        'TextBox4
        '
        Me.TextBox4.Location = New System.Drawing.Point(13, 166)
        Me.TextBox4.Name = "TextBox4"
        Me.TextBox4.Size = New System.Drawing.Size(100, 20)
        Me.TextBox4.TabIndex = 6
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(284, 261)
        Me.Controls.Add(Me.TrackBar4)
        Me.Controls.Add(Me.TextBox4)
        Me.Controls.Add(Me.TrackBar3)
        Me.Controls.Add(Me.TextBox3)
        Me.Controls.Add(Me.TrackBar2)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.TrackBar1)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

    Friend WithEvents TextBox1 As TextBox
    Friend WithEvents TrackBar1 As gTrackBar.gTrackBar
    Friend WithEvents TrackBar2 As gTrackBar.gTrackBar
    Friend WithEvents TextBox2 As TextBox
    Friend WithEvents TrackBar3 As gTrackBar.gTrackBar
    Friend WithEvents TextBox3 As TextBox
    Friend WithEvents TrackBar4 As gTrackBar.gTrackBar
    Friend WithEvents TextBox4 As TextBox
End Class

Open in new window


-saige-
0
 
TerrygordonAuthor Commented:
Hi Saige

I'm still getting the Nullreference exception. Do I need the form1.designer.vb code? All the texboxes and sliders and their properties are already in place.

Regards

Terry
0
 
TerrygordonAuthor Commented:
Hi David

Your solution seems to work, but using the code as it stands I can only change the value of the textbox by moving the slider. It won't let me change the value in the textbox.

Any suggestions?

Regards

Terry
0
 
Éric MoreauSenior .Net ConsultantCommented:
Since we don't have access to your gtrackbar, and since you have issues trying to convert a string to your control, have you tried David L. Hansen answer? No casting is required
0
 
TerrygordonAuthor Commented:
Hi Eric

Just waiting for David to get back to me (see comments above).

Regards

Terry
0
 
it_saigeDeveloperCommented:
You don't need the designer, it is just in case you wanted to implement my example as a test application.  With regards to the the object reference error, are you by chance setting any of the textbox or slider values pre-load?

Where do you get the object reference error using my implementation?

Can you provide your source code?

-saige-
0
 
Éric MoreauSenior .Net ConsultantCommented:
I have your solution! Your exception was surely coming from the fact that ValueChanged event of the trackbar was raised while the form was loading (and because the form was still not visible, the TextBox is not available)!

Public Class Form2

  
    Private mblnChangingValue As Boolean = False

    Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged, TextBox5.TextChanged
        If mblnChangingValue Then Return

        Dim objTextBox As TextBox = DirectCast(sender, TextBox)

        Dim intIndice as Integer
        Integer.TryParse(objTextBox.Name.Trim().ToUpper().Replace("TEXTBOX", ""), intIndice)

        Dim objTrackBar As gTrackBar.gTrackBar = CType(Controls("GTrackBar" + intIndice.ToString()), gTrackBar.gTrackBar)

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

    Private Sub TrackBar1_ValueChanged(sender As Object, e As EventArgs) Handles gTrackBar1.ValueChanged, gTrackBar2.ValueChanged, gTrackBar3.ValueChanged, gTrackBar4.ValueChanged, gTrackBar5.ValueChanged
        if not Visible then return
        If mblnChangingValue Then Return

        Dim objTrackBar As GTrackBar.gTrackBar = DirectCast(sender, gTrackBar.gTrackBar)

        Dim intIndice As Integer
        Integer.TryParse(objTrackBar.Name.Trim().ToUpper().Replace("GTRACKBAR", ""), intIndice)

        Dim objTextBox As TextBox = CType(Controls("TextBox" + intIndice.ToString()), TextBox)

        mblnChangingValue = True
        objTextBox.Text = objTrackBar.Value.ToString()
        mblnChangingValue = False
    End Sub

End Class

Open in new window

0
 
TerrygordonAuthor Commented:
Sorry Eric - still getting the null reference, but this time for the trackbar. The error is as follows:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

objTrackBar was Nothing.

This happening at line 18 in your code.

Regards

Terry
0
 
Éric MoreauSenior .Net ConsultantCommented:
are your trackbar controls really name "GTrackBar1", "GTrackBar2", "GTrackBar3", ... ? And it is case sensitibe.
0
 
Éric MoreauSenior .Net ConsultantCommented:
if you use the trackbar, are the textboxes correctly updated?
0
 
TerrygordonAuthor Commented:
Hi Eric

I don't think the names are case sensitive, but I tried the code again with the names case matching the control names and got the same error. In my original code, the textboxes and sliders interact perfectly - they update each other.

Regards

Terry
0
 
Éric MoreauSenior .Net ConsultantCommented:
it is working perfectly using the GTrackbar control. Can you show your code?
0
 
TerrygordonAuthor Commented:
Hi Eric

I'm just heading off somewhere, so I will respond tomorrow. Below is the code for Form3 with my original method for changing the values.  Also, in my code it loads the initial textbox and slider values from a file into a datatable. I assume you don't need this.

Regards

Terry

Imports System.Data.OleDb

Public Class Form3
    Dim MS(24) As Integer
    Dim MaxMS As Integer
    Dim MaxFactor As Integer
    Dim Maxadd As Integer
    Dim Numlen As Integer
    Dim Tbindex As Integer
    Dim dt As New DataTable

    Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        TabControl1.Hide()
        Panel1.Hide()
        Panel2.Hide()
        'load data from scenario file into datatable dt
        Dim folder = "D:\Users\Terry Gordon\Simco2017\"
        Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & folder & ";" + "Extended Properties='text;FMT=Delimited(;);HDR=YES'")
        Dim da As New OleDbDataAdapter("select * from [Simco Variables with headers.csv]", con)

        da.Fill(dt)
        'MsgBox(dt.Rows.Item(5).Item(5))
        'read tab headings from datatable dt
        TabPage1.Text = dt.Columns(1).ColumnName
        TabPage2.Text = dt.Columns(2).ColumnName
        TabPage3.Text = dt.Columns(3).ColumnName
        TabPage4.Text = dt.Columns(4).ColumnName
        TabPage5.Text = dt.Columns(5).ColumnName
        TabPage6.Text = dt.Columns(6).ColumnName
        TabPage7.Text = dt.Columns(7).ColumnName
        TextBox1.Text = "40000"
    End Sub
    Private Sub Gtrackbar1_ValueChanged(sender As Object, e As EventArgs) Handles GTrackBar1.ValueChanged
        If mblnChangingValue Then Return
        'combineFunctions(Me.TextBox1, Me.GTrackBar1)
        mblnChangingValue = True
        TextBox1.Text = GTrackBar1.Value
        mblnChangingValue = False
    End Sub

    Private Sub Gtrackbar2_ValueChanged(sender As Object, e As EventArgs) Handles GTrackBar2.ValueChanged
        If mblnChangingValue Then Return

        mblnChangingValue = True
        TextBox2.Text = GTrackBar2.Value
        mblnChangingValue = False
    End Sub
    Private Sub Gtrackbar3_ValueChanged(sender As Object, e As EventArgs) Handles GTrackBar3.ValueChanged
        If mblnChangingValue Then Return

        mblnChangingValue = True
        TextBox3.Text = GTrackBar3.Value
        mblnChangingValue = False
    End Sub
    Private Sub Gtrackbar4_ValueChanged(sender As Object, e As EventArgs) Handles GTrackBar4.ValueChanged
        If mblnChangingValue Then Return

        mblnChangingValue = True
        TextBox4.Text = GTrackBar4.Value
        mblnChangingValue = False
    End Sub
    Private Sub Gtrackbar5_ValueChanged(sender As Object, e As EventArgs) Handles GTrackBar5.ValueChanged
        If mblnChangingValue Then Return

        mblnChangingValue = True
        TextBox5.Text = GTrackBar5.Value
        mblnChangingValue = False
    End Sub
    Private Sub Gtrackbar6_ValueChanged(sender As Object, e As EventArgs) Handles GTrackBar6.ValueChanged
        If mblnChangingValue Then Return

        mblnChangingValue = True
        TextBox6.Text = GTrackBar6.Value
        mblnChangingValue = False
    End Sub
    Private Sub Gtrackbar7_ValueChanged(sender As Object, e As EventArgs) Handles GTrackBar7.ValueChanged
        If mblnChangingValue Then Return

        mblnChangingValue = True
        TextBox7.Text = GTrackBar7.Value
        mblnChangingValue = False
    End Sub
    Private Sub Gtrackbar8_ValueChanged(sender As Object, e As EventArgs) Handles GTrackBar8.ValueChanged
        If mblnChangingValue Then Return

        mblnChangingValue = True
        TextBox8.Text = GTrackBar8.Value
        mblnChangingValue = False
    End Sub
    Private Sub Gtrackbar9_ValueChanged(sender As Object, e As EventArgs) Handles GTrackBar9.ValueChanged
        If mblnChangingValue Then Return

        mblnChangingValue = True
        TextBox9.Text = GTrackBar9.Value
        mblnChangingValue = False
    End Sub
    Private Sub Gtrackbar10_ValueChanged(sender As Object, e As EventArgs) Handles GTrackBar10.ValueChanged
        If mblnChangingValue Then Return

        mblnChangingValue = True
        TextBox10.Text = GTrackBar10.Value
        mblnChangingValue = False
    End Sub
    Private Sub Gtrackbar11_ValueChanged(sender As Object, e As EventArgs) Handles GTrackBar11.ValueChanged
        If mblnChangingValue Then Return

        mblnChangingValue = True
        TextBox11.Text = GTrackBar11.Value
        mblnChangingValue = False
    End Sub
    Private Sub Gtrackbar12_ValueChanged(sender As Object, e As EventArgs) Handles GTrackBar12.ValueChanged
        If mblnChangingValue Then Return

        mblnChangingValue = True
        TextBox12.Text = GTrackBar12.Value
        mblnChangingValue = False
    End Sub

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

        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

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

        Dim intNewValue As Integer
        Integer.TryParse(TextBox2.Text, intNewValue)
        If intNewValue > GTrackBar2.MinValue AndAlso intNewValue < GTrackBar2.MaxValue Then
            mblnChangingValue = True
            GTrackBar2.Value = intNewValue
            mblnChangingValue = False
        End If
    End Sub
    Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
        If mblnChangingValue Then Return

        Dim intNewValue As Integer
        Integer.TryParse(TextBox3.Text, intNewValue)
        If intNewValue > GTrackBar3.MinValue AndAlso intNewValue < GTrackBar3.MaxValue Then
            mblnChangingValue = True
            GTrackBar3.Value = intNewValue
            mblnChangingValue = False
        End If
    End Sub
    Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged
        If mblnChangingValue Then Return

        Dim intNewValue As Integer
        Integer.TryParse(TextBox4.Text, intNewValue)
        If intNewValue > GTrackBar4.MinValue AndAlso intNewValue < GTrackBar4.MaxValue Then
            mblnChangingValue = True
            GTrackBar4.Value = intNewValue
            mblnChangingValue = False
        End If
    End Sub
    Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles TextBox5.TextChanged
        If mblnChangingValue Then Return

        Dim intNewValue As Integer
        Integer.TryParse(TextBox5.Text, intNewValue)
        If intNewValue > GTrackBar5.MinValue AndAlso intNewValue < GTrackBar5.MaxValue Then
            mblnChangingValue = True
            GTrackBar5.Value = intNewValue
            mblnChangingValue = False
        End If
    End Sub
    Private Sub TextBox6_TextChanged(sender As Object, e As EventArgs) Handles TextBox6.TextChanged
        If mblnChangingValue Then Return

        Dim intNewValue As Integer
        Integer.TryParse(TextBox6.Text, intNewValue)
        If intNewValue > GTrackBar6.MinValue AndAlso intNewValue < GTrackBar6.MaxValue Then
            mblnChangingValue = True
            GTrackBar6.Value = intNewValue
            mblnChangingValue = False
        End If
    End Sub
    Private Sub TextBox7_TextChanged(sender As Object, e As EventArgs) Handles TextBox7.TextChanged
        If mblnChangingValue Then Return

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

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

        Dim intNewValue As Integer
        Integer.TryParse(TextBox8.Text, intNewValue)
        If intNewValue > GTrackBar8.MinValue AndAlso intNewValue < GTrackBar8.MaxValue Then
            mblnChangingValue = True
            GTrackBar8.Value = intNewValue
            mblnChangingValue = False
        End If
    End Sub
    Private Sub TextBox9_TextChanged(sender As Object, e As EventArgs) Handles TextBox9.TextChanged
        If mblnChangingValue Then Return

        Dim intNewValue As Integer
        Integer.TryParse(TextBox9.Text, intNewValue)
        If intNewValue > GTrackBar9.MinValue AndAlso intNewValue < GTrackBar9.MaxValue Then
            mblnChangingValue = True
            GTrackBar9.Value = intNewValue
            mblnChangingValue = False
        End If
    End Sub
    Private Sub TextBox10_TextChanged(sender As Object, e As EventArgs) Handles TextBox10.TextChanged
        If mblnChangingValue Then Return

        Dim intNewValue As Integer
        Integer.TryParse(TextBox10.Text, intNewValue)
        If intNewValue > GTrackBar10.MinValue AndAlso intNewValue < GTrackBar10.MaxValue Then
            mblnChangingValue = True
            GTrackBar10.Value = intNewValue
            mblnChangingValue = False
        End If
    End Sub
    Private Sub TextBox11_TextChanged(sender As Object, e As EventArgs) Handles TextBox11.TextChanged
        If mblnChangingValue Then Return

        Dim intNewValue As Integer
        Integer.TryParse(TextBox11.Text, intNewValue)
        If intNewValue > GTrackBar11.MinValue AndAlso intNewValue < GTrackBar11.MaxValue Then
            mblnChangingValue = True
            GTrackBar11.Value = intNewValue
            mblnChangingValue = False
        End If
    End Sub
    Private Sub TextBox12_TextChanged(sender As Object, e As EventArgs) Handles TextBox12.TextChanged
        If mblnChangingValue Then Return

        Dim intNewValue As Integer
        Integer.TryParse(TextBox12.Text, intNewValue)
        If intNewValue > GTrackBar12.MinValue AndAlso intNewValue < GTrackBar12.MaxValue Then
            mblnChangingValue = True
            GTrackBar12.Value = intNewValue
            mblnChangingValue = False
        End If
    End Sub
    Private Sub combineFunctions(iTxt As TextBox, iSlider As gTrackBar.gTrackBar)
        iTxt.Text = iSlider.Value
        mblnChangingValue = False

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

    Private Sub TabControl1_Click(sender As Object, e As EventArgs) Handles TabControl1.Click
        Tbindex = TabControl1.SelectedIndex
        'MsgBox(Tbindex)
        filltextboxes()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        TabControl1.Show()
        TabControl1.SelectedIndex = 0
        Tbindex = 0
        Panel1.Show()
        Panel2.Show()
        filltextboxes()
    End Sub
    Private Sub filltextboxes()

        TextBox1.Text = dt.Rows.Item(0).Item(Tbindex + 1)
        TextBox2.Text = dt.Rows.Item(1).Item(Tbindex + 1)
        TextBox3.Text = dt.Rows.Item(2).Item(Tbindex + 1)
        TextBox4.Text = dt.Rows.Item(3).Item(Tbindex + 1)
        TextBox5.Text = dt.Rows.Item(4).Item(Tbindex + 1)
        TextBox6.Text = dt.Rows.Item(5).Item(Tbindex + 1)
        TextBox7.Text = dt.Rows.Item(6).Item(Tbindex + 1)
        TextBox8.Text = dt.Rows.Item(7).Item(Tbindex + 1)
        TextBox9.Text = dt.Rows.Item(8).Item(Tbindex + 1)
        TextBox10.Text = dt.Rows.Item(9).Item(Tbindex + 1)
        TextBox11.Text = dt.Rows.Item(10).Item(Tbindex + 1)
        TextBox12.Text = dt.Rows.Item(11).Item(Tbindex + 1)
        TextBox13.Text = dt.Rows.Item(13).Item(Tbindex + 1)
        TextBox14.Text = dt.Rows.Item(14).Item(Tbindex + 1)
        TextBox15.Text = dt.Rows.Item(15).Item(Tbindex + 1)
        TextBox16.Text = dt.Rows.Item(16).Item(Tbindex + 1)
        TextBox17.Text = dt.Rows.Item(17).Item(Tbindex + 1)
        TextBox18.Text = dt.Rows.Item(18).Item(Tbindex + 1)
        TextBox19.Text = dt.Rows.Item(19).Item(Tbindex + 1)
        TextBox20.Text = dt.Rows.Item(20).Item(Tbindex + 1)
        TextBox21.Text = dt.Rows.Item(21).Item(Tbindex + 1)
        TextBox22.Text = dt.Rows.Item(22).Item(Tbindex + 1)
        TextBox23.Text = dt.Rows.Item(23).Item(Tbindex + 1)
        TextBox24.Text = dt.Rows.Item(24).Item(Tbindex + 1)
        TextBox25.Text = dt.Rows.Item(12).Item(Tbindex + 1)
        TextBox26.Text = dt.Rows.Item(25).Item(Tbindex + 1)
    End Sub
End Class

Open in new window

0
 
Éric MoreauSenior .Net ConsultantCommented:
the problem is that you have tabpages and the controls collection is not flat. give me a couple of minutes!
0
 
it_saigeDeveloperCommented:
@Eric,

The GetChildren method I used above gets all children regardless of Parent depth.

-saige-
0
 
TerrygordonAuthor Commented:
Hi Eric

Your solution works a treat. Thanks to both yourself and Saige for all the work you have put into this.

My problem now is that there are 24 trackbars and 24 textboxes that have to go after the 'handles' statement. I don't suppose there is any way of spreading it across multiple lines? :-)

Have a fantastic Christmas.

Regards

Terry
0
 
Éric MoreauSenior .Net ConsultantCommented:
Instead of using the Handles clause, you could use a AddHandler statement in your form's initializer but you would need 48 rows of them: https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/addhandler-statement
0
 
it_saigeDeveloperCommented:
Agreed.  I could whip something up in a bit...

-saige-
0
 
it_saigeDeveloperCommented:
Using the extension method GetChildren, we could do something like this:

Form1.vb -
Imports System.Runtime.CompilerServices

Public Class Form1
    Private mappings As List(Of Tuple(Of Control, UserControl))
    Private IsChangingValue As Boolean = False

    Private Sub OnLoad(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each pair In (From tb In Me.Controls.GetChildren(GetType(TextBox))
                          Join trk In Me.Controls.GetChildren(GetType(gTrackBar.gTrackBar))
                          On tb.Name.ToUpper().Replace("TEXTBOX", "") Equals trk.Name.ToUpper().Replace("GTRACKBAR", "")
                          Select New With {.TextBox = CType(tb, TextBox), .gTrackBar = CType(trk, gTrackBar.gTrackBar)})
            AddHandler pair.TextBox.TextChanged, AddressOf OnTextChanged
            AddHandler pair.gTrackBar.ValueChanged, AddressOf OnValueChanged
        Next

        If mappings Is Nothing Then
            mappings = (From tb In Me.Controls.GetChildren(GetType(TextBox))
                        Join trk In Me.Controls.GetChildren(GetType(gTrackBar.gTrackBar))
                        On tb.Name.ToUpper().Replace("TEXTBOX", "") Equals trk.Name.ToUpper().Replace("GTRACKBAR", "")
                        Select New Tuple(Of Control, UserControl)(tb, trk)).ToList()
        End If
    End Sub

    Private Sub OnTextChanged(sender As Object, e As EventArgs)
        If TypeOf sender Is TextBox And Not IsChangingValue Then
            Dim tb = DirectCast(sender, TextBox)
            Dim mapping = (From m In mappings Where m.Item1.Equals(tb) Select m).FirstOrDefault()
            If mapping IsNot Nothing Then
                Dim newValue As Integer
                If Integer.TryParse(mapping.Item1.Text, newValue) AndAlso newValue > CType(mapping.Item2, gTrackBar.gTrackBar).MinValue AndAlso newValue < CType(mapping.Item2, gTrackBar.gTrackBar).MaxValue Then
                    IsChangingValue = True
                    CType(mapping.Item2, gTrackBar.gTrackBar).Value = newValue
                    IsChangingValue = False
                End If
            End If
        End If
    End Sub

    Private Sub OnValueChanged(sender As Object, e As EventArgs)
        If TypeOf sender Is gTrackBar.gTrackBar And Not IsChangingValue Then
            Dim trk = DirectCast(sender, gTrackBar.gTrackBar)
            Dim mapping = (From m In mappings Where m.Item2.Equals(trk) Select m).FirstOrDefault()
            If mapping IsNot Nothing Then
                IsChangingValue = True
                mapping.Item1.Text = CType(mapping.Item2, gTrackBar.gTrackBar).Value
                IsChangingValue = False
            End If
        End If
    End Sub
End Class

Module Extensions
    <Extension>
    Public Function GetChildren(source As Control.ControlCollection, Optional ByVal filter As Type = Nothing) As IEnumerable(Of Control)
        Dim children As List(Of Control) = New List(Of Control)
        For Each child As Control In source
            If child.GetType() Is If(filter IsNot Nothing, filter, GetType(Control)) Then children.Add(child)
            If child.HasChildren() Then children.AddRange(child.Controls.GetChildren(filter))
        Next
        Return children
    End Function
End Module

Open in new window


-saige-
0
 
David L. HansenProgrammer AnalystCommented:
Terry,
 
Apologies, I was mostly "off the grid" during the holiday. I'm glad your question was resolved. Saige and Eric do great work.

David
0
 
TerrygordonAuthor Commented:
No problem David - I was only around intermittently myself. Thanks for your input.

Terry
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.