Linking a texbox to a slider control in Visual Basic

Hi - I have some slider controls on a form in VB Express that also contains textboxes. I want to link the sliders and textboxes (e.g. Slider1 with textbox1, slider2 with textbox2, etc.) so that if I move the slider, the slider value appears in the textbox and, if I type a value into the textbox, the slider adjusts to the textbox value.

Any help would be greatly appreciated.

Regards

Terry
TerrygordonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
have you tried handling the ValueChanged event of your slider control?

    Private Sub TrackBar1_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar1.ValueChanged
        TextBox1.Text = TrackBar1.Value
    End Sub

    Private Sub TrackBar2_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar2.ValueChanged
        TextBox2.Text = TrackBar2.Value
    End Sub

Open in new window

0
Éric MoreauSenior .Net ConsultantCommented:
I missed the second part! I be back in a minute
0
Éric MoreauSenior .Net ConsultantCommented:
a bit more complete!

Public Class Form1

    Private mblnChangingValue as Boolean = false
    Private Sub TrackBar1_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar1.ValueChanged
        if mblnChangingValue Then Return

        mblnChangingValue = true
        TextBox1.Text = TrackBar1.Value
        mblnChangingValue = false
    End Sub

    Private Sub TrackBar2_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar2.ValueChanged
        if mblnChangingValue Then Return

        mblnChangingValue = true
        TextBox2.Text = TrackBar2.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 > TrackBar1.Minimum AndAlso intNewValue < TrackBar1.Maximum Then
            mblnChangingValue = True
            TrackBar1.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 > TrackBar2.Minimum AndAlso intNewValue < TrackBar2.Maximum Then
            mblnChangingValue = True
            TrackBar2.Value = intNewValue
            mblnChangingValue = False
        End If
    End Sub
End Class

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TerrygordonAuthor Commented:
Thanks Eric - this is perfect. Had to change a few of the property names because it is a custom slider, but works great.

Regards

Terry
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

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.