Use TrackBar to control label color : Track Bar « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » Track BarScreenshots 
Use TrackBar to control label color
Use TrackBar to control label color


  using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;

  public class TrackForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TrackBar blueTrackBar;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TrackBar greenTrackBar;
    private System.Windows.Forms.TrackBar redTrackBar;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.Label lblCurrColor;
    private System.Windows.Forms.PictureBox colorBox;

    public TrackForm()
    {
      InitializeComponent();
      CenterToScreen();         
  
      redTrackBar.Value = 0;
      greenTrackBar.Value = 0;
      blueTrackBar.Value = 0;
      UpdateColor();
    }
    private void InitializeComponent()
    {
      this.label4 = new System.Windows.Forms.Label ();
      this.label1 = new System.Windows.Forms.Label ();
      this.label3 = new System.Windows.Forms.Label ();
      this.label2 = new System.Windows.Forms.Label ();
      this.panel1 = new System.Windows.Forms.Panel ();
      this.redTrackBar = new System.Windows.Forms.TrackBar ();
      this.greenTrackBar = new System.Windows.Forms.TrackBar ();
      this.colorBox = new System.Windows.Forms.PictureBox ();
      this.lblCurrColor = new System.Windows.Forms.Label ();
      this.blueTrackBar = new System.Windows.Forms.TrackBar ();
      redTrackBar.BeginInit ();
      greenTrackBar.BeginInit ();
      blueTrackBar.BeginInit ();

      label4.Location = new System.Drawing.Point (1688);
      label4.Text = "Pick your slider here:";
      label4.Size = new System.Drawing.Size (24032);
      label4.Font = new System.Drawing.Font ("Microsoft Sans Serif"15);
      label4.TabIndex = 9;
      label1.Location = new System.Drawing.Point (2416);
      label1.Text = "Red:";
      label1.Size = new System.Drawing.Size (8832);
      label1.Font = new System.Drawing.Font ("Arial"15);
      label1.TabIndex = 4;
      label3.Location = new System.Drawing.Point (24104);
      label3.Text = "Blue:";
      label3.Size = new System.Drawing.Size (8832);
      label3.Font = new System.Drawing.Font ("Arial"15);
      label3.TabIndex = 6;
      label2.Location = new System.Drawing.Point (2464);
      label2.Text = "Green:";
      label2.Size = new System.Drawing.Size (8832);
      label2.Font = new System.Drawing.Font ("Arial"15);
      label2.TabIndex = 5;
      panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
      panel1.Location = new System.Drawing.Point (16120);
      panel1.Size = new System.Drawing.Size (384188);
      panel1.TabIndex = 8;
      panel1.AutoScroll = true;
      redTrackBar.TickFrequency = 5;
      redTrackBar.Location = new System.Drawing.Point (12016);
      redTrackBar.TabIndex = 2;
      redTrackBar.TickStyle = System.Windows.Forms.TickStyle.TopLeft;
      redTrackBar.Maximum = 255;
      redTrackBar.Size = new System.Drawing.Size (23242);
      redTrackBar.Scroll += new System.EventHandler (this.redTrackBar_Scroll);
      greenTrackBar.TickFrequency = 5;
      greenTrackBar.Location = new System.Drawing.Point (12056);
      greenTrackBar.TabIndex = 3;
      greenTrackBar.TickStyle = System.Windows.Forms.TickStyle.TopLeft;
      greenTrackBar.Maximum = 255;
      greenTrackBar.Size = new System.Drawing.Size (24042);
      greenTrackBar.Scroll += new System.EventHandler (this.greenTrackBar_Scroll);
      colorBox.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      colorBox.BackColor = System.Drawing.Color.Blue;
      colorBox.Location = new System.Drawing.Point (1616);
      colorBox.Size = new System.Drawing.Size (38456);
      colorBox.TabIndex = 0;
      colorBox.TabStop = false;
      lblCurrColor.Location = new System.Drawing.Point (16324);
      lblCurrColor.Text = "label4";
      lblCurrColor.Size = new System.Drawing.Size (39240);
      lblCurrColor.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
      lblCurrColor.Font = new System.Drawing.Font ("Microsoft Sans Serif"14);
      lblCurrColor.TabIndex = 7;
      blueTrackBar.TickFrequency = 5;
      blueTrackBar.Location = new System.Drawing.Point (12096);
      blueTrackBar.TabIndex = 1;
      blueTrackBar.TickStyle = System.Windows.Forms.TickStyle.TopLeft;
      blueTrackBar.Maximum = 255;
      blueTrackBar.Size = new System.Drawing.Size (24042);
      blueTrackBar.Scroll += new System.EventHandler (this.blueTrackBar_Scroll);
      this.Text = "Color Form";
      this.AutoScaleBaseSize = new System.Drawing.Size (513);
      this.ClientSize = new System.Drawing.Size (424385);
      panel1.Controls.Add (this.label2);
      panel1.Controls.Add (this.blueTrackBar);
      panel1.Controls.Add (this.label3);
      panel1.Controls.Add (this.greenTrackBar);
      panel1.Controls.Add (this.redTrackBar);
      panel1.Controls.Add (this.label1);
      this.Controls.Add (this.label4);
      this.Controls.Add (this.panel1);
      this.Controls.Add (this.lblCurrColor);
      this.Controls.Add (this.colorBox);
      redTrackBar.EndInit ();
      greenTrackBar.EndInit ();
      blueTrackBar.EndInit ();
    }
    static void Main() 
    {
      Application.Run(new TrackForm());
    }

    protected void greenTrackBar_Scroll (object sender, System.EventArgs e)
    {
      UpdateColor();
    }

    protected void redTrackBar_Scroll (object sender, System.EventArgs e)
    {
      UpdateColor();
    }

    protected void blueTrackBar_Scroll (object sender, System.EventArgs e)
    {
      UpdateColor();
    }

    private void UpdateColor()
    {
      Color c = Color.FromArgb(redTrackBar.Value, greenTrackBar.Value, blueTrackBar.Value);
      colorBox.BackColor = c;

      lblCurrColor.Text = "Current color is: " "(" 
        redTrackBar.Value + ", " 
        greenTrackBar.Value + " ," +  
        blueTrackBar.Value + ")";
    }
  }


           
       
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.