Home

The Color Changer Application

 

Introduction

In this application, we will use three scroll bars to change the background color of a panel.

  1. Start Microsoft Visual Basic and create a Standard EXE application
  2. Save the application in a new folder named Color Changer
  3. Save the form as frmMain and save the project as ColorChanger
  4. 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
  5. 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
  6. Double-click the Close button and implement its OnClick event as follows:
     
    Private Sub cmdClose_Click()
        End
    End Sub
  7. Double-click the left ScrollBar control on the form to access its OnChange event
  8. In the same way, double-click the middle and the right scroll bars
  9. 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
  10. 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
  11. Test the application
 

Home Copyright © 2002-2005 FunctionX, Inc.