Click here to Skip to main content
15,894,405 members
Articles / Programming Languages / C#

Using Single Dimensional Array to Represent Two Dimensional Data

Rate me:
Please Sign up or sign in to vote.
4.50/5 (9 votes)
30 Aug 2013CPOL2 min read 62.5K   560   11  
Shows how to treat a single dimensional array for both single and two dimensional data at the same time
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;

namespace nPuzzel
{
    class PuzzelPiece : Button
    {
        bool useImage = false;
        public ImageBrush PieceBrush { get; set; }
        public int PieceIndex { get; set; }
        public Point location;

        public bool UseImage 
        {
            get { return useImage; }
            set
            {
                useImage = value;
                SetBackground();
            }
        }

        public PuzzelPiece(int index, ImageBrush pieceBrush)
        {
            this.PieceBrush = pieceBrush;
            this.PieceIndex = index;
            SetBackground();
        }

        public Point Location
        {
            get { return location; }
            set
            {
                this.location = value;
                this.SetValue(Canvas.LeftProperty, location.X);
                this.SetValue(Canvas.TopProperty, location.Y);
            }
        }

        public void SetBackground()
        {
            if (IsEnabled)
            {
                if (UseImage)
                {
                    this.Background = PieceBrush;
                    this.Content = "";
                }
                else
                {
                    this.Background = Brushes.WhiteSmoke;
                    this.Content = PieceIndex + 1;
                }
            }
            else
            {
                this.Background = Brushes.WhiteSmoke;
                this.Content = "";
            }
        }

        public void MakeEmpty()
        {
            this.Content = "";
            this.Background = Brushes.WhiteSmoke;
            this.IsEnabled = false;
        }

        public void MakeActive()
        {
            this.IsEnabled = true;
            if (UseImage)
            {
                this.Background = PieceBrush;
                this.Content = "";
            }
            else
            {
                this.Background = Brushes.WhiteSmoke;
                this.Content = PieceIndex + 1;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead https://mathiversity.com
Unknown
I am a full-stack developer. My skills include JavaScript, C#/.Net, MS Azure cloud etc. I love to work on complex programming tasks requiring deep analysis, planning and use of efficient algorithms and data structures.

Comments and Discussions