Introduction: How to Integrate a Trackbar Into Visual Basic

Picture of How to Integrate a Trackbar Into Visual Basic

OK, so this sounds easy but the beginners have absolutely noidea how to do this. Trackbars can be useful tools. From deciding a budget to choosing how many fireworks to detonate, using trackbars are very very easily.

NOTE: Does anyone know how to increase the number of places on a trackbar? I can only get 11...

Step 1: Get Your Form Ready

Picture of Get Your Form Ready

This is pretty easy. Make when you want your program to look like. I'm not making anything in particular. I'll be making something that changes the colour of a textbox.

==Don't forget to add a trackbar!!!== Yes, alot of people forget that

Step 2: Code!

Picture of Code!
So you,ve done your user interface, now what? Code of course!!

Double click the trackbar. 2 lines of code should pop up. Something like:
    Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll    End Sub

Now we learn about values. On this trackbar we can only get 11 values, 0-10. Value 0 is the very left while value 10 is the very right. Got that? Lets move on.

To make life easier all text in this step from now on is going to be code-style


In between the two lines of code type in:If TrackBar1.Value = placceavaluenumberhere ThenEnd Ifreplacing placeavaluenumberhere with the value number of the place where you want it to be. Now in between those 4 lines of code type what you want to do. In this case:TextBox1.BackColor = Color.BlackOnce you're done that repeat for each place. When I was done it looked like this:Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll        If TrackBar1.Value = 0 Then            TextBox1.BackColor = Color.Black        End If        If TrackBar1.Value = 1 Then            TextBox1.BackColor = Color.White        End If        If TrackBar1.Value = 2 Then            TextBox1.BackColor = Color.Red        End If        If TrackBar1.Value = 3 Then            TextBox1.BackColor = Color.Blue        End If        If TrackBar1.Value = 4 Then            TextBox1.BackColor = Color.Yellow        End If        If TrackBar1.Value = 5 Then            TextBox1.BackColor = Color.Purple        End If        If TrackBar1.Value = 6 Then            TextBox1.BackColor = Color.Green        End If        If TrackBar1.Value = 7 Then            TextBox1.BackColor = Color.Orange        End If        If TrackBar1.Value = 8 Then            TextBox1.BackColor = Color.Brown        End If        If TrackBar1.Value = 9 Then            TextBox1.BackColor = Color.White        End If        If TrackBar1.Value = 10 Then            TextBox1.BackColor = Color.White        End If    End Sub

Step 3: Done!

Picture of Done!
Well, I was anyway. If you haven't finished your program, then keep going. Oh, and good luck!

But for those who are done, well, that wasn't too hard was it? And if you were wondering, the code for the reset and close is:

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click        End    End Sub    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        TrackBar1.Value = 0    End Sub

Comments

ekulmeekul (author)2009-02-11

Have you tested this without the "end if" at the end of every "if then" statement?

awang8 (author)ekulmeekul2009-02-21

Ifs are expressions and need a End If otherwise it either automatically puts it in or comes up with a error.

Seifpic (author)awang82011-02-14

Actually, you should do it like this instead:
If trackbar1.value = 0 then
'do something
Else if if trackbar1.value = 1 then
'do something
End if

Gamernotnerd (author)2009-08-28

Slider1.Max = ("Value")
Slider1.TickFrequency = ("Value")

But that's VB6.

zaketus (author)2009-02-09

I haven't used VB almost at all (I've just made some simple controlling software for microcontroller projects few times). Is it possible to make color array? So you could replace that if then end if if then end if -"monster" with just
TextBox1.BackColor = colors (TrackBar.Value)
Or that might work with string array...

When working with some bigger sources it's much more comfortable to work, if code is compact (everything shouldn't be shrinked, but this kind of stuff can be, without making any harm).

About This Instructable

18,331views

2favorites

License:

Bio: I hate brussel sprouts... Yeah, you really expected that in a profile didn't you?
More by awang8:How to Integrate a Trackbar into Visual BasicThumb-Pop IR Battery Drainer!How to find out your WEP/WPA key
Add instructable to: