VScrollBar ValueChanged

using System;
using System.Drawing;
using System.Windows.Forms;

class ColorScroll: Form
{
Panel panel;
Label[] alabelName = new Label[3];
Label[] alabelValue = new Label[3];
VScrollBar[] avscroll = new VScrollBar[3];

public static void Main()
{
Application.Run(new ColorScroll());
}
public ColorScroll()
{
Color[] acolor = { Color.Red, Color.Green, Color.Blue };

panel = new Panel();
panel.Parent = this;
panel.Location = new Point(0, 0);
panel.BackColor = Color.White;

for (int i = 0; i < 3; i++) { alabelName[i] = new Label(); alabelName[i].Parent = panel; alabelName[i].ForeColor = acolor[i]; alabelName[i].Text = "&" + acolor[i].ToKnownColor(); alabelName[i].TextAlign = ContentAlignment.MiddleCenter; avscroll[i] = new VScrollBar(); avscroll[i].Parent = panel; avscroll[i].SmallChange = 1; avscroll[i].LargeChange = 16; avscroll[i].Minimum = 0; avscroll[i].Maximum = 255 + avscroll[i].LargeChange - 1; avscroll[i].ValueChanged += new EventHandler(ScrollOnValueChanged); avscroll[i].TabStop = true; alabelValue[i] = new Label(); alabelValue[i].Parent = panel; alabelValue[i].TextAlign = ContentAlignment.MiddleCenter; } Color color = BackColor; avscroll[0].Value = color.R; // Generates ValueChanged event avscroll[1].Value = color.G; avscroll[2].Value = color.B; OnResize(EventArgs.Empty); } protected override void OnResize(EventArgs ea) { base.OnResize(ea); int cx = ClientSize.Width; int cy = ClientSize.Height; int cyFont = Font.Height; panel.Size = new Size(cx / 2, cy); for (int i = 0; i < 3; i++) { alabelName[i].Location = new Point(i * cx / 6, cyFont / 2); alabelName[i].Size = new Size(cx / 6, cyFont); avscroll[i].Location = new Point((4 * i + 1) * cx / 24, 2 * cyFont); avscroll[i].Size = new Size(cx / 12, cy - 4 * cyFont); alabelValue[i].Location = new Point(i * cx / 6, cy - 3 * cyFont / 2); alabelValue[i].Size = new Size(cx / 6, cyFont); } } void ScrollOnValueChanged(Object obj, EventArgs ea) { for (int i = 0; i < 3; i++) if((VScrollBar) obj == avscroll[i]) alabelValue[i].Text = avscroll[i].Value.ToString(); BackColor = Color.FromArgb(avscroll[0].Value, avscroll[1].Value, avscroll[2].Value); } } [/csharp]

ScrollBars Demo

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ScrollBars
{
///

/// Summary description for ScrollBars.
///

public class ScrollBars : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.VScrollBar vScrollBar1;
///

/// Required designer variable.
///

int counter=0;
private System.Windows.Forms.Label label1;
private System.ComponentModel.Container components = null;

public ScrollBars()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

///

/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///

/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.vScrollBar1 = new System.Windows.Forms.VScrollBar();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Font = new System.Drawing.Font(“Microsoft Sans Serif”, 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.textBox1.ForeColor = System.Drawing.Color.Transparent;
this.textBox1.Location = new System.Drawing.Point(24, 56);
this.textBox1.Multiline = true;
this.textBox1.Name = “textBox1”;
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(144, 32);
this.textBox1.TabIndex = 4;
this.textBox1.Text = “”;
this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// vScrollBar1
//
this.vScrollBar1.Location = new System.Drawing.Point(168, 56);
this.vScrollBar1.Name = “vScrollBar1”;
this.vScrollBar1.Size = new System.Drawing.Size(16, 32);
this.vScrollBar1.TabIndex = 7;
this.vScrollBar1.Scroll += new System.Windows.Forms.ScrollEventHandler(this.vScrollBar1_Scroll);
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 16);
this.label1.Name = “label1”;
this.label1.Size = new System.Drawing.Size(192, 16);
this.label1.TabIndex = 6;
this.label1.Text = “Numeric Scolling using VScroll Bars”;
//
// ScrollBars
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(208, 109);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.vScrollBar1,
this.textBox1});
this.Name = “ScrollBars”;
this.Text = “Numeric Scroll”;
this.Load += new System.EventHandler(this.ScrollBars_Load);
this.ResumeLayout(false);

}
#endregion

///

/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Application.Run(new ScrollBars());
}

private void textBox1_TextChanged(object sender, System.EventArgs e)
{
}

private void ScrollBars_Load(object sender, System.EventArgs e)
{
// Set the maximum range for the scrollbar
vScrollBar1.Maximum = 100;
// Set the minimum range for the scrollbar
vScrollBar1.Minimum = 0 ;

// Set the SmallChange factor
vScrollBar1.SmallChange = 1;

}

private void vScrollBar1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
{
// Check if the increment is Small
if ( e.Type == ScrollEventType.Last )
counter = 100 ;
else
// Check if the scroll is moved to minimum pos
if ( e.Type == ScrollEventType.First)
counter = 0 ;
else
// Check if the scroll is moved small distance
if ( e.Type == ScrollEventType.SmallDecrement )
counter — ;
else
// Check if the scroll is moved small distance
if ( e.Type == ScrollEventType.SmallIncrement )
{
counter++;
MessageBox.Show(“Small increment”);
}
else
// Check if the scroll is moved large distance
if ( e.Type == ScrollEventType.LargeDecrement )
counter-=5;
else
// Check if the scroll is moved large distance
if ( e.Type == ScrollEventType.LargeIncrement )
{
MessageBox.Show(“Large increment”);
counter+=5;
}
else
// Check if the scroll is moved to the Min position
if ( e.Type == ScrollEventType.First )
counter = 0 ;
else
// Check if the scroll to the Max position
if ( e.Type == ScrollEventType.Last)
counter = 100 ;

Console.WriteLine(e.NewValue+”
“);
// Check if the scroll is moved large distance
if ( counter > 100 ) counter = 100 ;
if ( counter < 0 ) counter = 0 ; textBox1.Text = counter.ToString() ; } } } [/csharp]

Use ScrollBar to control the picture size


   


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

public class Sizer : System.Windows.Forms.Form {
    private System.Windows.Forms.PictureBox picCritter;
    private System.Windows.Forms.HScrollBar scrSize;
    
    public static void Main(){
        Application.Run(new Sizer());
    } 
    public Sizer() {
        InitializeComponent();
    }
    private void InitializeComponent() {
      this.picCritter = new System.Windows.Forms.PictureBox();
      this.scrSize = new System.Windows.Forms.HScrollBar();
      this.SuspendLayout();

      this.picCritter.BackColor = System.Drawing.Color.White;
      this.picCritter.Image = new Bitmap("winter.jpg");
      this.picCritter.Location = new System.Drawing.Point(8, 8);
      this.picCritter.Name = "picCritter";
      this.picCritter.Size = new System.Drawing.Size(40, 40);
      this.picCritter.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
      this.picCritter.TabIndex = 0;
      this.picCritter.TabStop = false;

      this.scrSize.Location = new System.Drawing.Point(16, 248);
      this.scrSize.Maximum = 200;
      this.scrSize.Minimum = 50;
      this.scrSize.Name = "scrSize";
      this.scrSize.Size = new System.Drawing.Size(256, 16);
      this.scrSize.TabIndex = 1;
      this.scrSize.Value = 50;
      this.scrSize.Scroll += new System.Windows.Forms.ScrollEventHandler(this.scrSize_Scroll);

      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.scrSize,
                                                                  this.picCritter});
      this.Name = "Sizer";
      this.Text = "Sizer";
      this.ResumeLayout(false);

  }

  private void scrSize_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e) {
      picCritter.Size = new Size(scrSize.Value, scrSize.Value);
  }
}

           
          


Scrollable Form


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/

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

namespace UnsualScrolling
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class ScrollableForm : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.Panel Panel1;
        internal System.Windows.Forms.Button Button6;
        internal System.Windows.Forms.Button Button5;
        internal System.Windows.Forms.Button Button4;
        internal System.Windows.Forms.Button Button3;
        internal System.Windows.Forms.Button Button2;
        internal System.Windows.Forms.Button Button1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public ScrollableForm()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Panel1 = new System.Windows.Forms.Panel();
            this.Button6 = new System.Windows.Forms.Button();
            this.Button5 = new System.Windows.Forms.Button();
            this.Button4 = new System.Windows.Forms.Button();
            this.Button3 = new System.Windows.Forms.Button();
            this.Button2 = new System.Windows.Forms.Button();
            this.Button1 = new System.Windows.Forms.Button();
            this.Panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // Panel1
            // 
            this.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.Panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                 this.Button6,
                                                                                 this.Button5,
                                                                                 this.Button4,
                                                                                 this.Button3,
                                                                                 this.Button2,
                                                                                 this.Button1});
            this.Panel1.Location = new System.Drawing.Point(16, 17);
            this.Panel1.Name = "Panel1";
            this.Panel1.Size = new System.Drawing.Size(260, 232);
            this.Panel1.TabIndex = 1;
            // 
            // Button6
            // 
            this.Button6.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.Button6.Location = new System.Drawing.Point(16, 180);
            this.Button6.Name = "Button6";
            this.Button6.Size = new System.Drawing.Size(168, 24);
            this.Button6.TabIndex = 5;
            this.Button6.Text = "Button6";
            // 
            // Button5
            // 
            this.Button5.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.Button5.Location = new System.Drawing.Point(16, 148);
            this.Button5.Name = "Button5";
            this.Button5.Size = new System.Drawing.Size(168, 24);
            this.Button5.TabIndex = 4;
            this.Button5.Text = "Button5";
            // 
            // Button4
            // 
            this.Button4.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.Button4.Location = new System.Drawing.Point(16, 116);
            this.Button4.Name = "Button4";
            this.Button4.Size = new System.Drawing.Size(168, 24);
            this.Button4.TabIndex = 3;
            this.Button4.Text = "Button4";
            // 
            // Button3
            // 
            this.Button3.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.Button3.Location = new System.Drawing.Point(16, 84);
            this.Button3.Name = "Button3";
            this.Button3.Size = new System.Drawing.Size(168, 24);
            this.Button3.TabIndex = 2;
            this.Button3.Text = "Button3";
            // 
            // Button2
            // 
            this.Button2.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.Button2.Location = new System.Drawing.Point(16, 52);
            this.Button2.Name = "Button2";
            this.Button2.Size = new System.Drawing.Size(168, 24);
            this.Button2.TabIndex = 1;
            this.Button2.Text = "Button2";
            // 
            // Button1
            // 
            this.Button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.Button1.Location = new System.Drawing.Point(16, 20);
            this.Button1.Name = "Button1";
            this.Button1.Size = new System.Drawing.Size(168, 24);
            this.Button1.TabIndex = 0;
            this.Button1.Text = "Button1";
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.AutoScroll = true;
            this.ClientSize = new System.Drawing.Size(211, 169);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.Panel1});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "Form1";
            this.Text = "Scrollable Form";
            this.Panel1.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new ScrollableForm());
        }
    }
}


           
          


Scrollable Panel


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/

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

namespace UnsualScrolling
{
    /// <summary>
    /// Summary description for ScrollablePanel.
    /// </summary>
    public class ScrollablePanel : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.Panel Panel1;
        internal System.Windows.Forms.Button Button6;
        internal System.Windows.Forms.Button Button5;
        internal System.Windows.Forms.Button Button4;
        internal System.Windows.Forms.Button Button3;
        internal System.Windows.Forms.Button Button2;
        internal System.Windows.Forms.Button Button1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public ScrollablePanel()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Panel1 = new System.Windows.Forms.Panel();
            this.Button6 = new System.Windows.Forms.Button();
            this.Button5 = new System.Windows.Forms.Button();
            this.Button4 = new System.Windows.Forms.Button();
            this.Button3 = new System.Windows.Forms.Button();
            this.Button2 = new System.Windows.Forms.Button();
            this.Button1 = new System.Windows.Forms.Button();
            this.Panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // Panel1
            // 
            this.Panel1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.Panel1.AutoScroll = true;
            this.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.Panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                 this.Button6,
                                                                                 this.Button5,
                                                                                 this.Button4,
                                                                                 this.Button3,
                                                                                 this.Button2,
                                                                                 this.Button1});
            this.Panel1.Location = new System.Drawing.Point(4, 8);
            this.Panel1.Name = "Panel1";
            this.Panel1.Size = new System.Drawing.Size(348, 172);
            this.Panel1.TabIndex = 2;
            // 
            // Button6
            // 
            this.Button6.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.Button6.Location = new System.Drawing.Point(16, 180);
            this.Button6.Name = "Button6";
            this.Button6.Size = new System.Drawing.Size(168, 24);
            this.Button6.TabIndex = 5;
            this.Button6.Text = "Button6";
            // 
            // Button5
            // 
            this.Button5.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.Button5.Location = new System.Drawing.Point(16, 148);
            this.Button5.Name = "Button5";
            this.Button5.Size = new System.Drawing.Size(168, 24);
            this.Button5.TabIndex = 4;
            this.Button5.Text = "Button5";
            // 
            // Button4
            // 
            this.Button4.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.Button4.Location = new System.Drawing.Point(16, 116);
            this.Button4.Name = "Button4";
            this.Button4.Size = new System.Drawing.Size(168, 24);
            this.Button4.TabIndex = 3;
            this.Button4.Text = "Button4";
            // 
            // Button3
            // 
            this.Button3.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.Button3.Location = new System.Drawing.Point(16, 84);
            this.Button3.Name = "Button3";
            this.Button3.Size = new System.Drawing.Size(168, 24);
            this.Button3.TabIndex = 2;
            this.Button3.Text = "Button3";
            // 
            // Button2
            // 
            this.Button2.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.Button2.Location = new System.Drawing.Point(16, 52);
            this.Button2.Name = "Button2";
            this.Button2.Size = new System.Drawing.Size(168, 24);
            this.Button2.TabIndex = 1;
            this.Button2.Text = "Button2";
            // 
            // Button1
            // 
            this.Button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.Button1.Location = new System.Drawing.Point(16, 20);
            this.Button1.Name = "Button1";
            this.Button1.Size = new System.Drawing.Size(168, 24);
            this.Button1.TabIndex = 0;
            this.Button1.Text = "Button1";
            // 
            // ScrollablePanel
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(360, 194);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.Panel1});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "ScrollablePanel";
            this.Text = "Scrollable Panel";
            this.Panel1.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        static void Main() 
        {
            Application.Run(new ScrollablePanel());
        }
    }
}