Using different colors in Visual Basic : Color « 2D « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » 2D » ColorScreenshots 
Using different colors in Visual Basic
Using different colors in Visual Basic

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class MainClass

   Shared Sub Main()
        Dim myform As Form = New FrmColorForm()
        Application.Run(myform)
   End Sub ' Main

End Class


Public Class FrmColorForm
   Inherits System.Windows.Forms.Form

   ' input text boxes
   Friend WithEvents txtColorName As TextBox
   Friend WithEvents txtGreenBox As TextBox
   Friend WithEvents txtRedBox As TextBox
   Friend WithEvents txtAlphaBox As TextBox
   Friend WithEvents txtBlueBox As TextBox

   ' set color command buttons
   Friend WithEvents cmdColorName As Button
   Friend WithEvents cmdColorValue As Button

   ' color labels
   Friend WithEvents lblBlue As Label
   Friend WithEvents lblGreen As Label
   Friend WithEvents lblRed As Label
   Friend WithEvents lblAlpha As Label

   ' group boxes
   Friend WithEvents nameBox As GroupBox
   Friend WithEvents colorValueGroup As GroupBox

#Region " Windows Form Designer generated code "

   Public Sub New()
      MyBase.New()

      'This call is required by the Windows Form Designer.
      InitializeComponent()

      'Add any initialization after the InitializeComponent() call

   End Sub

   'Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
      If disposing Then
         If Not (components Is NothingThen
            components.Dispose()
         End If
      End If
      MyBase.Dispose(disposing)
   End Sub

   'Required by the Windows Form Designer
   Private components As System.ComponentModel.Container

   'NOTE: The following procedure is required by the Windows Form Designer
   'It can be modified using the Windows Form Designer.  
   'Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()Private Sub InitializeComponent()
      Me.nameBox = New System.Windows.Forms.GroupBox()
      Me.cmdColorName = New System.Windows.Forms.Button()
      Me.txtColorName = New System.Windows.Forms.TextBox()
      Me.colorValueGroup = New System.Windows.Forms.GroupBox()
      Me.lblBlue = New System.Windows.Forms.Label()
      Me.lblGreen = New System.Windows.Forms.Label()
      Me.lblRed = New System.Windows.Forms.Label()
      Me.lblAlpha = New System.Windows.Forms.Label()
      Me.txtBlueBox = New System.Windows.Forms.TextBox()
      Me.txtGreenBox = New System.Windows.Forms.TextBox()
      Me.txtRedBox = New System.Windows.Forms.TextBox()
      Me.txtAlphaBox = New System.Windows.Forms.TextBox()
      Me.cmdColorValue = New System.Windows.Forms.Button()
      Me.nameBox.SuspendLayout()
      Me.colorValueGroup.SuspendLayout()
      Me.SuspendLayout()
      '
      'nameBox
      '
      Me.nameBox.Controls.AddRange(New System.Windows.Forms.Control() {Me.cmdColorName, Me.txtColorName})
      Me.nameBox.Location = New System.Drawing.Point(0184)
      Me.nameBox.Name = "nameBox"
      Me.nameBox.Size = New System.Drawing.Size(30464)
      Me.nameBox.TabIndex = 0
      Me.nameBox.TabStop = False
      Me.nameBox.Text = "Set Back Color Name"
      '
      'cmdColorName
      '
      Me.cmdColorName.Location = New System.Drawing.Point(16024)
      Me.cmdColorName.Name = "cmdColorName"
      Me.cmdColorName.Size = New System.Drawing.Size(11224)
      Me.cmdColorName.TabIndex = 1
      Me.cmdColorName.Text = "Set Color Name"
      '
      'txtColorName
      '
      Me.txtColorName.Location = New System.Drawing.Point(1624)
      Me.txtColorName.Name = "txtColorName"
      Me.txtColorName.Size = New System.Drawing.Size(12820)
      Me.txtColorName.TabIndex = 0
      Me.txtColorName.Text = "Wheat"
      '
      'colorValueGroup
      '
      Me.colorValueGroup.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblBlue, Me.lblGreen, Me.lblRed, Me.lblAlpha, Me.txtBlueBox, Me.txtGreenBox, Me.txtRedBox, Me.txtAlphaBox, Me.cmdColorValue})
      Me.colorValueGroup.Location = New System.Drawing.Point(0248)
      Me.colorValueGroup.Name = "colorValueGroup"
      Me.colorValueGroup.Size = New System.Drawing.Size(30456)
      Me.colorValueGroup.TabIndex = 1
      Me.colorValueGroup.TabStop = False
      Me.colorValueGroup.Text = "Set Front Color Value"
      '
      'lblBlue
      '
      Me.lblBlue.Location = New System.Drawing.Point(12816)
      Me.lblBlue.Name = "lblBlue"
      Me.lblBlue.Size = New System.Drawing.Size(4016)
      Me.lblBlue.TabIndex = 7
      Me.lblBlue.Text = "Blue"
      '
      'lblGreen
      '
      Me.lblGreen.Location = New System.Drawing.Point(8816)
      Me.lblGreen.Name = "lblGreen"
      Me.lblGreen.Size = New System.Drawing.Size(4016)
      Me.lblGreen.TabIndex = 6
      Me.lblGreen.Text = "Green"
      '
      'lblRed
      '
      Me.lblRed.Location = New System.Drawing.Point(5616)
      Me.lblRed.Name = "lblRed"
      Me.lblRed.Size = New System.Drawing.Size(3216)
      Me.lblRed.TabIndex = 5
      Me.lblRed.Text = "Red"
      '
      'lblAlpha
      '
      Me.lblAlpha.Location = New System.Drawing.Point(816)
      Me.lblAlpha.Name = "lblAlpha"
      Me.lblAlpha.Size = New System.Drawing.Size(4016)
      Me.lblAlpha.TabIndex = 4
      Me.lblAlpha.Text = "Alpha"
      '
      'txtBlueBox
      '
      Me.txtBlueBox.Location = New System.Drawing.Point(12832)
      Me.txtBlueBox.Name = "txtBlueBox"
      Me.txtBlueBox.Size = New System.Drawing.Size(3220)
      Me.txtBlueBox.TabIndex = 3
      Me.txtBlueBox.Text = "255"
      '
      'txtGreenBox
      '
      Me.txtGreenBox.Location = New System.Drawing.Point(8832)
      Me.txtGreenBox.Name = "txtGreenBox"
      Me.txtGreenBox.Size = New System.Drawing.Size(3220)
      Me.txtGreenBox.TabIndex = 2
      Me.txtGreenBox.Text = "0"
      '
      'txtRedBox
      '
      Me.txtRedBox.Location = New System.Drawing.Point(4832)
      Me.txtRedBox.Name = "txtRedBox"
      Me.txtRedBox.Size = New System.Drawing.Size(3220)
      Me.txtRedBox.TabIndex = 1
      Me.txtRedBox.Text = "0"
      '
      'txtAlphaBox
      '
      Me.txtAlphaBox.Location = New System.Drawing.Point(832)
      Me.txtAlphaBox.Name = "txtAlphaBox"
      Me.txtAlphaBox.Size = New System.Drawing.Size(3220)
      Me.txtAlphaBox.TabIndex = 0
      Me.txtAlphaBox.Text = "100"
      '
      'cmdColorValue
      '
      Me.cmdColorValue.Location = New System.Drawing.Point(16832)
      Me.cmdColorValue.Name = "cmdColorValue"
      Me.cmdColorValue.Size = New System.Drawing.Size(11224)
      Me.cmdColorValue.TabIndex = 3
      Me.cmdColorValue.Text = "Set Color Value"
      '
      'FrmColorForm
      '
      Me.AutoScaleBaseSize = New System.Drawing.Size(513)
      Me.ClientSize = New System.Drawing.Size(304309)
      Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.colorValueGroup, Me.nameBox})
      Me.Name = "FrmColorForm"
      Me.Text = "Using Colors"
      Me.nameBox.ResumeLayout(False)
      Me.colorValueGroup.ResumeLayout(False)
      Me.ResumeLayout(False)

   End Sub

#End Region

   ' color for back rectangle
   Private mBehindColor As Color = Color.Wheat

   ' color for front rectangle
   Private mFrontColor As Color = Color.FromArgb(10000255)

   ' overrides Form OnPaint method
   Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
      Dim graphicsObject As Graphics = e.Graphics ' get graphics

      Dim textBrush As SolidBrush = New SolidBrush(Color.Black

      Dim brush As SolidBrush = New SolidBrush(Color.White

      graphicsObject.FillRectangle(brush, 44275180)

      graphicsObject.DrawString(mBehindColor.Name, Me.Font, _
         textBrush, 405)

      brush.Color = mBehindColor

      graphicsObject.FillRectangle(brush, 4520150120)

      graphicsObject.DrawString("Alpha: " & mFrontColor.A & _
         " Red: " & mFrontColor.R & " Green: " & mFrontColor.G _
         " Blue: " & mFrontColor.B, Me.Font, textBrush, _
         55165)

      brush.Color = mFrontColor

      graphicsObject.FillRectangle(brush, 6535170130)
   End Sub

   Private Sub cmdColorValue_Click(ByVal sender As _
      System.Object, ByVal e As System.EventArgs_
      Handles cmdColorValue.Click

      mFrontColor = Color.FromArgb(txtAlphaBox.Text, _
         txtRedBox.Text, txtGreenBox.Text, txtBlueBox.Text)

      Invalidate()
   End Sub

   Private Sub cmdColorName_Click(ByVal sender As _
      System.Object, ByVal e As System.EventArgs_
      Handles cmdColorName.Click

      mBehindColor = Color.FromName(txtColorName.Text)

      Invalidate()
   End Sub

End Class 


           
       
Related examples in the same category
1.Get all Known color and calcuate its Brightness and SaturationGet all Known color and calcuate its Brightness and Saturation
2.Track Form background ColorTrack Form background Color
3.Color Dialog: Changing the background and text colors of a formColor Dialog: Changing the background and text colors of a form
4.Construct Color from R G B ValueConstruct Color from R G B Value
5.Use color dialog to set form backgroundUse color dialog to set form background
w___w___w__.__j__a__v__a2___s.___c_o__m__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.