In this application, we will use three scroll bars to
change the background color of a panel.
|
- Start Microsoft Visual Basic and create a Standard EXE application
- Save the application in a new folder named Color Changer
- Save the form as frmMain and save the project as ColorChanger
- Design it as follows
|
Control |
Properties |
Form |
Name: frmMain
Border Style: 3 - FixedDialog
Caption: Color Changer |
PictureBox |
Name: pctPreview |
VScrollBar |
Name: scrRed
Max: 255 |
VScrollBar |
Name: scrGreen
Max: 255 |
VScrollBar |
Name: scrBlue
Max: 255 |
CommandButton |
Name: cmdClose
Caption: Close |
Label |
Name: lblRed
Alignment: 2 - Center
Caption: 000 |
Label |
Name: lblGreen
Alignment: 2 - Center
Caption: 000 |
Label |
Name: lblBlue
Alignment: 2 - Center
Caption: 000 |
|
- Double-click an unoccupied area on the form and implement its OnLoad
event
as follows:
Private Sub Form_Load()
pctPreview.BackColor = RGB(128, 128, 128)
scrRed.Value = 127
scrGreen.Value = 127
scrBlue.Value = 127
End Sub
|
- Double-click the Close button and implement its OnClick event as
follows:
Private Sub cmdClose_Click()
End
End Sub
|
- Double-click the left ScrollBar control on the form to access its
OnChange event
- In the same way, double-click the middle and the right scroll bars
- In the code editor, when the caret is positioned in the OnChange
event of a scroll bar, in the Procedure combo box, select the Change
event
- Implement the events as follows:
Option Explicit
Private Sub cmdClose_Click()
End
End Sub
Private Sub Form_Load()
pctPreview.BackColor = RGB(128, 128, 128)
scrRed.Value = 127
scrGreen.Value = 127
scrBlue.Value = 127
End Sub
Private Sub scrBlue_Change()
Dim ValueRed As Integer
Dim ValueGreen As Integer
Dim ValueBlue As Integer
ValueRed = 255 - scrRed.Value
ValueGreen = 255 - scrGreen.Value
ValueBlue = 255 - scrBlue.Value
pctPreview.BackColor = RGB(ValueRed, ValueGreen, ValueBlue)
lblRed.Caption = CStr(ValueRed)
lblGreen.Caption = CStr(ValueGreen)
lblBlue.Caption = CStr(ValueBlue)
End Sub
Private Sub scrBlue_Scroll()
scrBlue_Change
End Sub
Private Sub scrGreen_Change()
Dim ValueRed As Integer
Dim ValueGreen As Integer
Dim ValueBlue As Integer
ValueRed = 255 - scrRed.Value
ValueGreen = 255 - scrGreen.Value
ValueBlue = 255 - scrBlue.Value
pctPreview.BackColor = RGB(ValueRed, ValueGreen, ValueBlue)
lblRed.Caption = CStr(ValueRed)
lblGreen.Caption = CStr(ValueGreen)
lblBlue.Caption = CStr(ValueBlue)
End Sub
Private Sub scrGreen_Scroll()
scrGreen_Change
End Sub
Private Sub scrRed_Change()
Dim ValueRed As Integer
Dim ValueGreen As Integer
Dim ValueBlue As Integer
ValueRed = 255 - scrRed.Value
ValueGreen = 255 - scrGreen.Value
ValueBlue = 255 - scrBlue.Value
pctPreview.BackColor = RGB(ValueRed, ValueGreen, ValueBlue)
lblRed.Caption = CStr(ValueRed)
lblGreen.Caption = CStr(ValueGreen)
lblBlue.Caption = CStr(ValueBlue)
End Sub
Private Sub scrRed_Scroll()
scrRed_Change
End Sub
|
- Test the application
|
|
|