Please, answer me,
I reading book "Windows Forms Programming in C# " and part :
void GridForm_Layout(object sender, LayoutEventArgs e) {
// Suspend layout until we're finished moving things
this.SuspendLayout();
// Arrange the buttons in a grid on the form
Button[] buttons = new Button[] { button1, ..., };
int cx = ClientRectangle.Width/3;
int cy = ClientRectangle.Height/3;
for( int row = 0; row != 3; ++row ) {
for( int col = 0; col != 3; ++col ) {
Button button = buttons[col * 3 + row];
button.SetBounds(cx * row, cy * col, cx, cy);
}
}
// Set form client size to be multiple of width/height
SetClientSizeCore(cx * 3, cy * 3);
// Resume the layout
this.ResumeLayout();
}
QUESTION
the bold fonts part above is :
1) private construction of the author
or
2 ) part an mathematicals (linear algebra) rules applying on arrays---
where I can learn about this rule (calcualte index array members, and packing
two or three array in one array)
thank you forward
Ben WatsonPosted May 22, 2008, 8:46 PM
Button button = buttons[col * 3 + row];
Yes, that is an equation to convert a 2-D array position into a 1-D array position. The general formula is
pos1d = col * numColumns + row
Work it out on a grid to see why this works. For example, the first element in a 2-d array with 3 columns should be the first element in the 1-d array: 0 == 0 * 3 + 0. The second element in the 2nd row should be the 5th element (because there are three in the preceding row and one before it in the current row). 4 == 1 * 3 + 1.