65.9K
CodeProject is changing. Read more.
Home

Visual Matrix

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.25/5 (12 votes)

Aug 15, 2001

viewsIcon

74036

downloadIcon

519

How to solve any sort of matrix using C#.

Sample Image - codepr1.gif

Introduction

User can easily add ,sub any limit of matrices. Edit boxes are created dynamically. Source Code is available Below

//
// 
namespace WindowsApplication20
{
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.WinForms;
    using System.Data;

    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.WinForms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components;
        private System.WinForms.Button button4;
        private System.WinForms.MenuItem menuItem5;
        private System.WinForms.MenuItem menuItem4;
        private System.WinForms.MenuItem menuItem3;
        private System.WinForms.MenuItem menuItem2;
        private System.WinForms.MenuItem menuItem1;
        private System.WinForms.TextBox textBox4;
        private System.WinForms.TextBox textBox3;
        private System.WinForms.Label label4;
        private System.WinForms.Label label3;
        private System.WinForms.GroupBox groupBox2;
        private System.WinForms.Label label2;
        private System.WinForms.Label label1;
        private System.WinForms.TextBox textBox2;
        private System.WinForms.TextBox textBox1;
        private System.WinForms.GroupBox groupBox1;
        private System.WinForms.LinkLabel linkLabel1;
        private System.WinForms.ToolTip toolTip1;
        private System.WinForms.ContextMenu contextMenu2;
        private System.WinForms.TrayIcon trayIcon1;
        private System.WinForms.ContextMenu contextMenu1;
        private System.WinForms.Button button2;
        private System.WinForms.Button button1;
        private System.WinForms.TextBox[,] j1= new TextBox[100,100] ;
        private System.WinForms.TextBox[,] j2= new TextBox[100,100] ;
        private System.WinForms.TextBox[,] j3= new TextBox[100,100] ;
        private System.WinForms.Label[] lb = new Label[10];

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

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

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        public override void Dispose()
        {
            base.Dispose();
            components.Dispose();
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.Resources.ResourceManager resources = 
                new System.Resources.ResourceManager (typeof(Form1));
            this.components = new System.ComponentModel.Container ();
            this.menuItem1 = new System.WinForms.MenuItem ();
            this.menuItem2 = new System.WinForms.MenuItem ();
            this.groupBox1 = new System.WinForms.GroupBox ();
            this.menuItem5 = new System.WinForms.MenuItem ();
            this.label1 = new System.WinForms.Label ();
            this.button2 = new System.WinForms.Button ();
            this.menuItem3 = new System.WinForms.MenuItem ();
            this.toolTip1 = new System.WinForms.ToolTip (this.components);
            this.groupBox2 = new System.WinForms.GroupBox ();
            this.trayIcon1 = new System.WinForms.TrayIcon ();
            this.button1 = new System.WinForms.Button ();
            this.textBox1 = new System.WinForms.TextBox ();
            this.label4 = new System.WinForms.Label ();
            this.textBox4 = new System.WinForms.TextBox ();
            this.textBox2 = new System.WinForms.TextBox ();
            this.textBox3 = new System.WinForms.TextBox ();
            this.button4 = new System.WinForms.Button ();
            this.contextMenu1 = new System.WinForms.ContextMenu ();
            this.contextMenu2 = new System.WinForms.ContextMenu ();
            this.menuItem4 = new System.WinForms.MenuItem ();
            this.linkLabel1 = new System.WinForms.LinkLabel ();
            this.label2 = new System.WinForms.Label ();
            this.label3 = new System.WinForms.Label ();
            //@this.TrayHeight = 90;
            //@this.TrayLargeIcon = false;
            //@this.TrayAutoArrange = true;
            menuItem1.Text = "Addition";
            menuItem1.Index = 0;
            menuItem1.Click += new System.EventHandler (this.menuItem1_Click);
            menuItem2.Text = "Subtraction";
            menuItem2.Index = 1;
            menuItem2.Click += new System.EventHandler (this.menuItem2_Click);
            groupBox1.TabIndex = 5;
            groupBox1.TabStop = false;
            groupBox1.Text = "Matrix A";
            groupBox1.Size = new System.Drawing.Size (88, 56);
            menuItem5.Text = "Scientific";
            menuItem5.Index = 1;
            menuItem5.Click += new System.EventHandler (this.menuItem5_Click);
            label1.Location = new System.Drawing.Point (16, 16);
            label1.Text = "Row";
            label1.Size = new System.Drawing.Size (32, 16);
            label1.TabIndex = 2;
            button2.Location = new System.Drawing.Point (216, 40);
            button2.AllowDrop = true;
            button2.Size = new System.Drawing.Size (75, 23);
            button2.TabIndex = 2;
            button2.Text = "Solution";
            button2.ContextMenu = this.contextMenu1;
            button2.Click += new System.EventHandler (this.button2_Click);
            menuItem3.Text = "Multiplication";
            menuItem3.Index = 2;
            menuItem3.Click += new System.EventHandler (this.menuItem3_Click);
            //@toolTip1.SetLocation (new System.Drawing.Point (309, 7));
            toolTip1.Active = true;
            groupBox2.Location = new System.Drawing.Point (96, 0);
            groupBox2.TabIndex = 6;
            groupBox2.TabStop = false;
            groupBox2.Text = "Matrix B";
            groupBox2.Size = new System.Drawing.Size (80, 56);
            //@trayIcon1.SetLocation (new System.Drawing.Point (115, 7));
            trayIcon1.Text = "Visual Matrix";
            trayIcon1.Visible = true;
            trayIcon1.Icon = (System.Drawing.Icon) 
                            resources.GetObject ("trayIcon1.Icon");
            trayIcon1.ContextMenu = this.contextMenu2;
            button1.Location = new System.Drawing.Point (208, 8);
            button1.Size = new System.Drawing.Size (80, 24);
            button1.TabIndex = 1;
            button1.Text = "Draw";
            button1.Click += new System.EventHandler (this.button1_Click);
            textBox1.Location = new System.Drawing.Point (56, 8);
            textBox1.Text = "0";
            textBox1.TabIndex = 0;
            textBox1.Size = new System.Drawing.Size (24, 20);
            label4.Location = new System.Drawing.Point (9, 33);
            label4.Text = "Col.";
            label4.Size = new System.Drawing.Size (32, 16);
            label4.TabIndex = 8;
            textBox4.Location = new System.Drawing.Point (48, 32);
            textBox4.Text = "0";
            textBox4.TabIndex = 7;
            textBox4.Size = new System.Drawing.Size (24, 20);
            textBox2.Location = new System.Drawing.Point (56, 32);
            textBox2.Text = "0";
            textBox2.TabIndex = 1;
            textBox2.Size = new System.Drawing.Size (24, 20);
            textBox3.Location = new System.Drawing.Point (48, 8);
            textBox3.Text = "0";
            textBox3.TabIndex = 9;
            textBox3.Size = new System.Drawing.Size (24, 20);
            button4.Location = new System.Drawing.Point (296, 8);
            button4.Size = new System.Drawing.Size (75, 23);
            button4.TabIndex = 7;
            button4.Text = "Refresh";
            button4.Click += new System.EventHandler (this.button4_Click);
            //@contextMenu1.SetLocation (new System.Drawing.Point (7, 7));
            contextMenu1.Popup += 
                    new System.EventHandler (this.contextMenu1_Popup);
            contextMenu1.MenuItems.All = 
                    new System.WinForms.MenuItem[3] {this.menuItem1, 
                    this.menuItem2, this.menuItem3};
            //@contextMenu2.SetLocation (new System.Drawing.Point (201, 7));
            contextMenu2.Popup += new System.EventHandler (this.contextMenu2_Popup);
            contextMenu2.MenuItems.All = 
                new System.WinForms.MenuItem[2] {this.menuItem4, this.menuItem5};
            menuItem4.Text = "About";
            menuItem4.Index = 0;
            menuItem4.Click += new System.EventHandler (this.menuItem4_Click);
            linkLabel1.Text = "JunaidMajeed";
            linkLabel1.Size = new System.Drawing.Size (100, 23);
            toolTip1.SetToolTip (linkLabel1, "Mail Me");
            linkLabel1.TabIndex = 4;
            linkLabel1.TabStop = true;
            linkLabel1.Location = new System.Drawing.Point (272, 264);
            linkLabel1.LinkClick += new System.EventHandler (this.linkLabel1_LinkClick);
            label2.Location = new System.Drawing.Point (25, 35);
            label2.Text = "Col.";
            label2.Size = new System.Drawing.Size (32, 16);
            label2.TabIndex = 6;
            label3.Location = new System.Drawing.Point (0, 14);
            label3.Text = "Row";
            label3.Size = new System.Drawing.Size (32, 16);
            label3.TabIndex = 7;
            this.Text = "Visual Matrix";
            this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
            this.BorderStyle = System.WinForms.FormBorderStyle.FixedToolWindow;
            this.ClientSize = new System.Drawing.Size (370, 278);
            this.Click += new System.EventHandler (this.Form1_Click);
            this.Controls.Add (this.button4);
            this.Controls.Add (this.groupBox2);
            this.Controls.Add (this.groupBox1);
            this.Controls.Add (this.linkLabel1);
            this.Controls.Add (this.button2);
            this.Controls.Add (this.button1);
            groupBox1.Controls.Add (this.label2);
            groupBox1.Controls.Add (this.label1);
            groupBox1.Controls.Add (this.textBox2);
            groupBox1.Controls.Add (this.textBox1);
            groupBox2.Controls.Add (this.textBox4);
            groupBox2.Controls.Add (this.textBox3);
            groupBox2.Controls.Add (this.label4);
            groupBox2.Controls.Add (this.label3);
        }

        protected void menuItem5_Click (object sender, System.EventArgs e)
        {
            Form2 sci = new Form2();
            sci.Show();
        }

        protected void contextMenu2_Popup (object sender, System.EventArgs e)
        {


        }

        protected void menuItem3_Click (object sender, System.EventArgs e)
        {
            double[,] t1 = new double[100,100];

            if((textBox1.Text).ToInt32()==(textBox2.Text).ToInt32()&&
                (textBox3.Text).ToInt32()==(textBox4.Text).ToInt32())
            {

                for(int i=1;i<=(textBox2.Text).ToInt32();i++)
                {
                    for(int j=1;j<=(textBox3.Text).ToInt32();j++)
                    {
                        t1[i,j]=0;
                        for(int k=1;k<=(textBox3.Text).ToInt32();k++)
                        {
                            t1[i,j]=t1[i,j]+((j1[i,k].Text).ToDouble()) * 
                                ((j2[k,j].Text).ToDouble());
                        }
                    }

                }


                for(int i=1;i<=(textBox3.Text).ToInt32();i++)
                {
                    for(int j=1;j<=(textBox2.Text).ToInt32();j++)
                    {

                        this.j3[i,j] = new System.WinForms.TextBox ();
                        j3[i,j].Location = new System.Drawing.Point 
                                                (180+i*20, 120+j*23);
                        j3[i,j].TabIndex = 0;
                        j3[i,j].Size = new System.Drawing.Size (20, 23);
                        this.Controls.Add (this.j3[i,j]);
                        j3[i,j].Text = "";
                        j3[i,j].Text = t1[i,j].ToString();
                    }
                }
            }
            else
            {

                MessageBox.Show("This version is valid for Square " + 
                                "Matrix Multiplication");
            }




        }

        protected void button4_Click (object sender, System.EventArgs e)
        {

            for(int i=1;i<=(textBox2.Text).ToInt32();i++)
            {
                for(int j=1;j<=(textBox1.Text).ToInt32();j++)
                {

                    j1[i,j].Clear();
                    j1[i,j].Hide();
                }
            }


            for(int i=1;i<=(textBox4.Text).ToInt32();i++)
            {
                for(int j=1;j<=(textBox3.Text).ToInt32();j++)
                {

                    j2[i,j].Clear();
                    j2[i,j].Hide();
                }
            }

            for(int i=1;i<=(textBox2.Text).ToInt32();i++)
            {
                for(int j=1;j<=(textBox1.Text).ToInt32();j++)
                {

                    j3[i,j].Clear();
                    j3[i,j].Hide();
                }
            }








        }

        protected void menuItem2_Click (object sender, System.EventArgs e)
        {
            double[,] temp = new double[100,100];
            if((textBox1.Text).ToInt32() == (textBox3.Text).ToInt32() && 
                (textBox2.Text).ToInt32() == (textBox4.Text).ToInt32())
            {

                for(int i=1;i<=(textBox2.Text).ToInt32();i++)
                {
                    for(int j=1;j<=(textBox1.Text).ToInt32();j++)
                    {

                        this.j3[i,j] = new System.WinForms.TextBox ();

                        j3[i,j].Location = new System.Drawing.Point 
                            (180+i*20, 120+j*23);
                        j3[i,j].TabIndex = 0;
                        j3[i,j].Size = new System.Drawing.Size (20, 23);
                        this.Controls.Add (this.j3[i,j]);
                        j3[i,j].Text = "";

                        temp[i,j] = (j2[i,j].Text).ToDouble() - 
                            (j1[i,j].Text).ToDouble();

                        j3[i,j].Text = temp[i,j].ToString();

                    }
                }
            }
            else
            {
                MessageBox.Show("this addition is not possible:");


            }

        }

        protected void menuItem1_Click (object sender, System.EventArgs e)
        {

            double[,] temp = new double[100,100];
            if((textBox1.Text).ToInt32() == (textBox3.Text).ToInt32() && 
                (textBox2.Text).ToInt32() == (textBox4.Text).ToInt32())
            {

                for(int i=1;i<=(textBox2.Text).ToInt32();i++)
                {
                    for(int j=1;j<=(textBox1.Text).ToInt32();j++)
                    {

                        this.j3[i,j] = new System.WinForms.TextBox ();

                        j3[i,j].Location = new System.Drawing.Point 
                            (180+i*20, 120+j*23);
                        j3[i,j].TabIndex = 0;
                        j3[i,j].Size = new System.Drawing.Size (20, 23);
                        this.Controls.Add (this.j3[i,j]);
                        temp[i,j] = (j2[i,j].Text).ToDouble() + 
                            (j1[i,j].Text).ToDouble();
                        j3[i,j].Text = temp[i,j].ToString();

                    }
                }
            }
            else
            {
                MessageBox.Show("this addition is not possible:");


            }


        }

        protected void menuItem4_Click (object sender, System.EventArgs e)
        {
            Form3 abt = new Form3();
            abt.Show();

        }

        protected void contextMenu1_Popup (object sender, System.EventArgs e)
        {

        }

        protected void linkLabel1_LinkClick (object sender, System.EventArgs e)
        {
            System.Diagnostics.Process.Start("mailto:junaidmajeed161@hotmail.com");

        }

        protected void button3_Click (object sender, System.EventArgs e)
        {
            MessageBox.Show("Under Developement");

        }

        protected void button2_Click (object sender, System.EventArgs e)
        {
            MessageBox.Show("press Right Mouse Button:");

        }

        protected void Form1_Click (object sender, System.EventArgs e)
        {

        }

        protected void button1_Click (object sender, System.EventArgs e)
        {
            if((textBox1.Text).ToInt32() ==0 || (textBox2.Text).ToInt32() ==0 || 
                (textBox3.Text).ToInt32() ==0 || (textBox4.Text).ToInt32() ==0)
            {
                MessageBox.Show("pls fill Row Column Boxes");

            }
            else
            {



                int tb1;
                tb1 = (textBox1.Text).ToInt32();


                for(int i=1;i<=(textBox2.Text).ToInt32();i++)
                {
                    for(int j=1;j<=(textBox1.Text).ToInt32();j++)
                    {

                        this.j1[i,j] = new System.WinForms.TextBox ();

                        j1[i,j].Location = new System.Drawing.Point 
                            (18+i*20, 36+j*23);
                        j1[i,j].TabIndex = 0;
                        j1[i,j].Size = new System.Drawing.Size (20, 23);
                        this.Controls.Add (this.j1[i,j]);
                    }
                }

                for(int i=1;i<=(textBox4.Text).ToInt32();i++)
                {
                    for(int j=1;j<=(textBox3.Text).ToInt32();j++)
                    {

                        this.j2[i,j] = new System.WinForms.TextBox ();

                        j2[i,j].Location = new System.Drawing.Point 
                            (20+i*20, 120+j*23);
                        j2[i,j].TabIndex = 0;
                        j2[i,j].Size = new System.Drawing.Size (20, 23);
                        this.Controls.Add (this.j2[i,j]);
                    }
                }
            }


        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        public static void Main(string[] args) 
        {
            Application.Run(new Form1());
        }
    }
}
//
System Tray is also built in and Context Menus are used for navigation