You are on page 1of 4

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 the Console but in
windows forms we need to create a field for each value, doing this we cannot do right thing because the
number of rows and columns can be differently and depends from N - Rows or M - Columns, for any new
N or M the number of Rows(columns) will be changed. In console we can handle the rows and columns
using the new line and spaces for each element whatever array or matrices elements.

Like this it will looks in Win Form:

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


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

However the basic is in that to create and range the textboxes and look typically like two
dimensional array.

The code who generate 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;//boshti x por i kemi thene me nje


zhvendosje pre 80 piksel qe te kemi nje dizajn me te mire, nuk eshte e thene
te jete keshtu gjithmon kjo mund te ndryshoj

int b = merlokacionin.Y;//boshti i 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 valu 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 fo 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 a
values in Textboxes.

You might also like