Press "Enter" to skip to content

Change Form Background Color using Scrollbars in VB

How to Change Background Color using Scrollbars in VB

 Tip Abstraction:

        In Visual Basic programming code, we can simply change window form background color by using Scrollbar scrolling activity.

A type of grey color is the default background of all Windows Visual Basic form. We can simply change its background using form properties. You can set any background color even you can set transparent background for your form.
Do you know the 3 primary colors RGB? RBG stands for Red, Blue and Green respectively, known as primary colors because we can make any other colors using different combinations of these colors. By changing the values of these colors, then the output color will be change.
Here we are going to set the background of our form by setting values to these three colors. For setting the values for these colors, we have to add three scrollbars tools.

Set VB form background color using scrollbars

 

Each scrollbar represents a color such as Red, Green or Blue. While we scrolling a scrollbar, then the value of the scrollbar will be considered as the value of the color.
Here three scrollbars named with Srollbar1, Srollbar2, Srollbar3 respectively. Labels Red, Green, Blue are labels to denote three colors.
First of all, you have to declare three variable to keep the value of scroll bar as r, b, g (red, blue, green).

    Dim r As Integer = 0

Dim g As Integer = 0

Dim b As Integer = 0


Now we have to get the scrolling value, so we need to write corresponding VB code to catch the scrolling value on scrollbar scroll event. While we scroll the scrollbar1 (denotes red), then we get the scroll value on variable r.

Double clicking on scrollbar1 will give you the code event space.

Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll

r = VScrollBar1.Value()

Me.BackColor = Color.FromArgb(r, g, b)

End Sub


 

Code Explanation:

 r = VScrollBar1.Value()
Variable r gets the scrollbar value of scrollbar1.

Me.BackColor = Color.FromArgb(r, g, b)
Me.BackColor means the background color of the form.

Color.FromArgb(r, g, b) denotes a color by RGB combination.

Where r is the scrollbar value of scrollbar1, like this g,b are the values of scrollbar1 and scrollbar2 respectively.

Like scrollbar1 scroll event, we have to write similar codes on scrollbar1 and scrollbar2 scroll event.

The complete Code:

Public Class Form1

Dim r As Integer = 0

Dim g As Integer = 0

Dim b As Integer = 0

Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll

r = VScrollBar1.Value()

Me.BackColor = Color.FromArgb(r, g, b)

End Sub

Private Sub VScrollBar2_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar2.Scroll

g = VScrollBar2.Value()

Me.BackColor = Color.FromArgb(r, g, b)

End Sub

Private Sub VScrollBar3_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar3.Scroll

b = VScrollBar3.Value()

Me.BackColor = Color.FromArgb(r, g, b)

End Sub

End Class

Sample Output:

Changing form background using RGB scrollbars

 

One Comment

  1. Nethran Kumarasamy Nethran Kumarasamy 24th March 2021

    thanks . good explanation

Leave a Reply