How to Generate Fields of Matrices Dynamically Windows Form C#



When we have to work with arrays and matrices or any multidimensional array, the first thing what we need to do is take control of rows (columns). This is not very complicated in a console application but in a windows forms application we need to create a field for each value; doing this we cannot do correctly because the number of rows and columns can be different and depends from N - Rows or M - Columns, for any new N or M the number of rows(columns) will be changed. In a console application we can handle the rows and columns using a new line and spaces for each element or whatever are the array or matrices elements.

Like this it will looks in Win Form:

  1. Giving value of N(Rows) and M(Columns)

    Generate1.gif
     
  2. Clicking Create will automatically create fields in range and look like two dimensional array:

    Generate.gif

However the basic design is to create and arrange the textboxes to look typically like a two dimensional array.

The code that generates "Matrices" is the following:

Point merlokacionin = this.label.Location;// This take the location in form . We have added a label with no text just to take the location and under it to add fields.
 
            int a = merlokacionin.X + 80;//The coordinate X of the label we give to the int variable a
 
            int b = merlokacionin.Y;//The coordinate Y
 
            if ((textBox1.Text == "") || (textBox2.Text == ""))
                MessageBox.Show(" To create a Matrices you need to give up the numbers of columns and rows.,"opps",MessageBoxButtons.OK,MessageBoxIcon.Error);

            else
            {
                n = Convert.ToInt32(textBox1.Text);// Take the value N -Rows
                m = Convert.ToInt32(textBox2.Text);// Take the value M-Columns
            }
            int i, j;

            int[,] Matrica;

            Matrica = new int[n, m];//

            for (i = 0; i < n; i++)
            {
                a = merlokacionin.X + 10; // fields will be at coordinate X      //                                         in 10
point at right from label1.
 
                b = b + 30;//
the coordinate of b is changed with adding in 30 points , so the //field will be added in  30 points under the label1.

                for (j = 0; j < m; j++)
                {

                    textbox[i, j] = new TextBox();    //  it create the new //textbox field, the creating fields will continue while as we have the //columns and rows.

                    string emriTextBox = "A" + Convert.ToString(i);//We set the name for textbox

                    this.textbox[i, j].Name = emriTextBox; //give name to textbox

                    a = a + 25;// change the coordinate of X and every 25 points will be the next field                   
                    textbox[i, j].Width = 25;
                    textbox[i, j].Height = 25;
                    textbox[i, j].Location = new Point(a, b + 30);//Now the field take the location where it will be pasted.

                    this.Controls.Add(textbox[i, j]);

                }

            }

After this we can manipulate with matrices making it to find the minimum the maximum or sorting the matrices with bubble sort, select sort or any other operation with matrices and arrays. All this using values in Textboxes.

erver'>

Similar Articles