Sunday, September 29, 2013

Create a Transpose Method for Converting 2D Array and Jagged Array



Transpose Method :
     
        Transpose method which will convert the rows in to the columns and columns in to the rows in array.In Mathematical notation convert a Matrix A to Transpose  A  -> A pow(T) . In this example we are going to see how to implement a Transpose for 2 dimensional array and Jagged array. 

What is 2 dimensional array ?
Array which have elements in rows and columns are known as 2 dimensional array.the columns in the each row should have same no of elements

array with 2 rows 3 columns

2  3  4
1  4  6

int[,] a=new int[2,3]; 

What is Jagged array ?
Array in which columns of each row need not to be have same no of elements

2  3  4
1  8
3  5  6  8

in the above example you can see each row have different number of elements in columns, first row have 3 elements, 2 row have 2 elements etc.

Lets we see the real implementation :
   
    class TArray<T> where T : struct,IComparable
    {
       internal static T[,] Transpose(T[,] a)
        {
            int row = a.GetUpperBound(0) + 1;
            int col = a.GetUpperBound(1) + 1;
            T[,] tarr = new T[col, row];

            for (int i = 0; i < row; i++)
            {
                for (intj = 0; j < col; j++)
                {
                    tarr[j, i] = a[i, j];
                }
            }
            return tarr;
        }    

        internal static Nullable<T>[,] Transpose(T[][] jagged)
        {
            int row = jagged.Length;
            T?[,] temparray = null;
            int col = 0;
            if (row >= 0)
            {
                for (intj = 0; j < row; j++)
                {
                    col = jagged[j].Length > col ? jagged[j].Length : col;
                }
            }
            temparray = new T?[col, row];

            for (int i = 0; i < row; i++)
            {
                for (intj = 0; j < col; j++)
                {
                    if (jagged[i].Length > j)
                    {
                        temparray[j, i] = jagged[i][j];
                    }
                    else
                    {
                        temparray[j, i] = default(T?);
                    }
                }
            }
            return temparray;           
        }
    }


 class Program
    {
        static void Main(string[] args)
        {
            int[,] array = new int[1, 3];
            array = new int [,]{ {1,2,3}};

            int [][] jagged=new int [3][];
            jagged[0] = new int[] { 1,2,3};
            jagged[1] = new int[] { 6,7};
            jagged[2] = new int[] { 1,6,3,8};
            Console.WriteLine();
            Console.WriteLine("Before Transpose");
            Console.WriteLine();
            Console.WriteLine("2 Dimensional Array");
            for(int i=0;i<=array.GetUpperBound(0);i++)
            {
                for (int j = 0; j<=array.GetUpperBound(1); j++)
                {
                    Console.Write(string.Format("[{0}]",array[i,j]));
                }
                Console.WriteLine();
            }

            Console.WriteLine();
            Console.WriteLine("Jagged Array");
            for (int i = 0; i <= jagged.GetUpperBound(0); i++)
            {
                for (int j = 0; j < jagged[i].Length; j++)
                {
                    Console.Write(string.Format("[{0}]",jagged[i][j]));
                }
                Console.WriteLine();
            }

            int [,] arrTranspose= TArray<int>.Transpose(array);          
            int?[,] jagTranspose = TArray<int>.Transpose(jagged);

            Console.WriteLine();
            Console.WriteLine("After Transpose");
            Console.WriteLine();
            Console.WriteLine("2 Dimensional Array");
            for (int i = 0; i <= arrTranspose.GetUpperBound(0); i++)
            {
                for (int j = 0; j <= arrTranspose.GetUpperBound(1); j++)
                {
                    Console.Write(string.Format("[{0}]",arrTranspose[i, j]));
                }
                Console.WriteLine();
            }

            Console.WriteLine();
            Console.WriteLine("Jagged Array");
            for (int i = 0; i <= jagTranspose.GetUpperBound(0); i++)
            {
                for (int j = 0; j <= jagTranspose.GetUpperBound(1); j++)
                {
                    if(jagTranspose[i,j].HasValue)
                    Console.Write(string.Format("[{0}]",jagTranspose[i,j].Value));
                    else
                     Console.Write(string.Format("[ ]"));
                }
                Console.WriteLine();
            }


            Console.Read();

        }
    }


Output:

Before Transpose
2 Dimensional Array
[1][2][3]

Jagged Array
[1][2][3]
[6][7]
[1][6][3][8]

After Transpose
2 Dimensional Array
[1]
[2]
[3]

Jagged Array
[1][6][1]
[2][7][6]
[3][ ][3]
[ ][ ][8]


I hope this article will help you to convert the 2d array and jagged array to the transpose.
        

Saturday, September 21, 2013

Create a Resize Manager - which Auto Adjust the Windows Forms and the controls and font size based on the Resolution of the system


In this article we are going to see the how to resize the winforms and there controls and the font size based on the resolution of the system. For that we are going to create a component which will support the design time , where we can supply the value to the component in design time. 

Resize Manager a component which will resize the form and the controls along with font size. For do this create a two properties in menu item which will accept the form as container and type of resolution at the design time.

For ex : if we design a winforms in 1024x768 then if we run this application in 1366x768 resolution system the resize manager will auto resize the forms and controls along with font size.

For this create  a  Form and drag the Four buttons inside a panel and now drag and drop the resize manager component from the toolbox and set the property of container with form and set the Resolution of current system.


Above image shows how the component sees in Toolbox of visual studio,


After drag and drop the component and select the properties, now the above image specifies what are the values to be specified, Set the Form1 in the Container and R1600X900 in Resolution.

Code for Resize Manager : 
    Resize Manager Reads the each and every control and set the value of size and font value from the base size.

Form Desinger :
namespace ResizeCom
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(booldisposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #regionWindows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private voidInitializeComponent()
        {
            this.components = newSystem.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManagerresources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.button1 = newSystem.Windows.Forms.Button();
            this.button2 = newSystem.Windows.Forms.Button();
            this.button3 = newSystem.Windows.Forms.Button();
            this.button4 = newSystem.Windows.Forms.Button();
            this.button5 = newSystem.Windows.Forms.Button();
            this.button6 = newSystem.Windows.Forms.Button();
            this.button7 = newSystem.Windows.Forms.Button();
            this.button8 = newSystem.Windows.Forms.Button();
            this.resizeManager1 = newResizeCom.ResizeManager(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.resizeManager1)).BeginInit();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button1.BackgroundImage")));
            this.button1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.button1.FlatAppearance.BorderSize = 0;
            this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.button1.Location = newSystem.Drawing.Point(64, 66);
            this.button1.Name = "button1";
            this.button1.Size = newSystem.Drawing.Size(101, 88);
            this.button1.TabIndex = 0;
            this.button1.UseVisualStyleBackColor = true;
            //
            // button2
            //
            this.button2.BackgroundImage = global::ResizeCom.Properties.Resources.icon3;
            this.button2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.button2.FlatAppearance.BorderSize = 0;
            this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.button2.Location = newSystem.Drawing.Point(278, 66);
            this.button2.Name = "button2";
            this.button2.Size = newSystem.Drawing.Size(101, 88);
            this.button2.TabIndex = 1;
            this.button2.UseVisualStyleBackColor = true;
            //
            // button3
            //
            this.button3.BackgroundImage = global::ResizeCom.Properties.Resources.icon2;
            this.button3.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.button3.FlatAppearance.BorderSize = 0;
            this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.button3.Location = newSystem.Drawing.Point(171, 66);
            this.button3.Name = "button3";
            this.button3.Size = newSystem.Drawing.Size(101, 88);
            this.button3.TabIndex = 2;
            this.button3.UseVisualStyleBackColor = true;
            //
            // button4
            //
            this.button4.BackgroundImage = global::ResizeCom.Properties.Resources.icon4;
            this.button4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.button4.FlatAppearance.BorderSize = 0;
            this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.button4.Location = newSystem.Drawing.Point(385, 66);
            this.button4.Name = "button4";
            this.button4.Size = newSystem.Drawing.Size(101, 88);
            this.button4.TabIndex = 3;
            this.button4.UseVisualStyleBackColor = true;
            //
            // button5
            //
            this.button5.BackColor = System.Drawing.SystemColors.Highlight;
            this.button5.FlatAppearance.BorderSize = 0;
            this.button5.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.button5.Font = newSystem.Drawing.Font("Rockwell", 12F);
            this.button5.Location = newSystem.Drawing.Point(278, 160);
            this.button5.Name = "button5";
            this.button5.Size = newSystem.Drawing.Size(101, 88);
            this.button5.TabIndex = 6;
            this.button5.Text = "LogOff";
            this.button5.UseVisualStyleBackColor = false;
            //
            // button6
            //
            this.button6.BackgroundImage = global::ResizeCom.Properties.Resources.icon5;
            this.button6.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.button6.FlatAppearance.BorderSize = 0;
            this.button6.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.button6.Location = newSystem.Drawing.Point(171, 161);
            this.button6.Name = "button6";
            this.button6.Size = newSystem.Drawing.Size(101, 88);
            this.button6.TabIndex = 5;
            this.button6.UseVisualStyleBackColor = true;
            //
            // button7
            //
            this.button7.BackColor = System.Drawing.Color.MidnightBlue;
            this.button7.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.button7.FlatAppearance.BorderSize = 0;
            this.button7.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.button7.Font = newSystem.Drawing.Font("Rockwell", 12F);
            this.button7.ForeColor = System.Drawing.SystemColors.Control;
            this.button7.Location = newSystem.Drawing.Point(64, 160);
            this.button7.Name = "button7";
            this.button7.Size = newSystem.Drawing.Size(101, 88);
            this.button7.TabIndex = 4;
            this.button7.Text = "Start Up";
            this.button7.UseVisualStyleBackColor = false;
            //
            // button8
            //
            this.button8.BackgroundImage = global::ResizeCom.Properties.Resources.icon6;
            this.button8.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.button8.FlatAppearance.BorderSize = 0;
            this.button8.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.button8.Font = newSystem.Drawing.Font("Rockwell", 12F);
            this.button8.Location = newSystem.Drawing.Point(505, 66);
            this.button8.Name = "button8";
            this.button8.Size = newSystem.Drawing.Size(156, 182);
            this.button8.TabIndex = 7;
            this.button8.UseVisualStyleBackColor = true;
            //
            // resizeManager1
            //
            this.resizeManager1.Container = this;
            this.resizeManager1.Resolution = ResizeCom.ScreenResolution.R1600X900;
            //
            // Form1
            //
            this.AutoScaleDimensions = newSystem.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
            this.ClientSize = newSystem.Drawing.Size(728, 365);
            this.Controls.Add(this.button8);
            this.Controls.Add(this.button5);
            this.Controls.Add(this.button6);
            this.Controls.Add(this.button7);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Font = newSystem.Drawing.Font("Microsoft Sans Serif", 8F);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += newSystem.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.resizeManager1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private ResizeManagerresizeManager1;
        private System.Windows.Forms.Buttonbutton5;
        private System.Windows.Forms.Buttonbutton6;
        private System.Windows.Forms.Buttonbutton7;
        private System.Windows.Forms.Buttonbutton4;
        private System.Windows.Forms.Buttonbutton3;
        private System.Windows.Forms.Buttonbutton2;
        private System.Windows.Forms.Buttonbutton1;
        private System.Windows.Forms.Buttonbutton8;

    }
}

Resize Manager :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;


namespace ResizeCom
{
    public partial class ResizeManager: Component,ISupportInitialize
    {
        private ScreenResolutionresolution;
        public ScreenResolutionResolution
        {
            set { resolution = value; }
            get { returnresolution; }
        }
        private ContainerControl container = null;
        public ContainerControlContainer
        {
            set
            {
                container = value;
            }
            get {
                return container;
            }
        }

        public ResizeManager()
        {
            InitializeComponent();
        }

        public ResizeManager(IContainercontainer)
        {
            container.Add(this);

            InitializeComponent();
        }

        public void BeginInit()
        {
           
        }

        public void EndInit()
        {
            Size current = Screen.PrimaryScreen.Bounds.Size;
            Size original = ReValue(Resolution);
            if (current.Height != original.Height || current.Width != original.Width)
            {
                int per=CalculateResolution(current);
                Resize(Resolution,Container,per);
            }
        }

        public intCalculateResolution(Size display)
        {
            return (int)(Enum.Parse(typeof(ScreenResolution),("R"+ display.Width + "X" + display.Height)));
        }

        public SizeReValue(ScreenResolution sol)
        {
            string []si = sol.ToString().Substring(1).Split('X');
            return new Size(Convert.ToInt32(si[0]), Convert.ToInt32(si[1]));
        }

        public void Resize(ScreenResolution resolution,object element,intpercentage)
        {
            double value = (double)((percentage) % 100) / 100;
            if (element is ContainerControl || element is ScrollableControl)
            {
                ArrayList arr;
                if (element isContainerControl)
                {
                    ContainerControl containercntrl = (element as ContainerControl);
                    arr = new ArrayList(containercntrl.Controls);
                    containercntrl.Width = containercntrl.Width + (int)((float)containercntrl.Width*value);
                    containercntrl.Height = containercntrl.Height + (int)((float)containercntrl.Height * value);                   
                    //containercntrl.Font = new Font(containercntrl.Font.FontFamily, containercntrl.Font.Size + (int)(containercntrl.Font.Size * value));
                }
                else
                {
                    ScrollableControl scrollcntrl = (element) as ScrollableControl;
                    arr = new ArrayList(scrollcntrl.Controls);
                    scrollcntrl.Width = scrollcntrl.Width + (int)((float)scrollcntrl.Width * value);
                    scrollcntrl.Height = scrollcntrl.Height + (int)((float)scrollcntrl.Height * value);
                    scrollcntrl.Location = new Point(scrollcntrl.Location.X + (int)((float)scrollcntrl.Location.X * value), scrollcntrl.Location.Y + (int)((float)scrollcntrl.Location.Y * value));
                    //scrollcntrl.Font = new Font(scrollcntrl.Font.FontFamily, scrollcntrl.Font.Size + (int)(scrollcntrl.Font.Size * value));
                }
                foreach(Control cntrl inarr)
                {                   
                    cntrl.Width = cntrl.Width + (int)((float)cntrl.Width * value);
                    cntrl.Height = cntrl.Height +(int) ((float)cntrl.Height * value);
                    cntrl.Font =new Font(cntrl.Font.FontFamily,cntrl.Font.Size +(int) (cntrl.Font.Size *value));
                    cntrl.Location = new Point(cntrl.Location.X + (int)((float)cntrl.Location.X * value), cntrl.Location.Y + (int)((float)cntrl.Location.Y * value));
                    if (cntrl.Controls.Count > 0)
                    {
                        Resize(Resolution, cntrl, percentage);
                    }
                }
            }
        }

    }


    public enum ScreenResolution : int
    {
        R1920X1080=125,
        R1600X900=150,
        R1366X768=100,        
    }
}


To change the Percentage of current resolution change the values in Enum.

OUTPUT:

Form for R1024X768:



Form for R1366X768 :


From this article you can learn how to change the form size and the font size of each controls based on resolution.