Zebra0.com

csharp arrays


Text of video
// Programmer: Janet Joy
// Use timer to cycle through colors in an array
//To do: add another color, change the speed
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ColorChange
{
    public partial class Form1 : Form
    {
        //Parallel arrays are global
        string[] myColors = { "red", "white", "blue","green" };
        Color[] colors = { Color.Red, Color.White, Color.Blue,Color.Green };
        int position = 0; //initial value
        public Form1()
        {
            InitializeComponent();
            timer1.Enabled = true;  //unless you did this in the property window
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //Cycle through the colors in the array when the timer goes off
            this.BackColor = colors[position];
            this.Text = myColors[position];
            // Add 1, but go back to 0 it the position is out of bounds
            position++;
            if (position >= colors.Length) position = 0;
        }

       
    }
}

End of lesson, Next lesson: Dialogs in C#