I have an application I am writing where I have to place street addresses on a label sheet that is pre defined. This label sheet has 3 columns and 10 rows. I have the loop that creates the labels correctly, but there is a requirement to allow the user the ability to choose what label on the sheet to begin on. I would have thought this was going to be an easy mathematical matrix equation, but I am unable to come up with or find a solution out there.
Here is an example:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
Given the matrix above, lets say the user decides to start on #6. I need to be able to tell my loop to start at that location: col:1 row:2.
If it helps, The loop I have looks like this, but I think this comes down to a mathematical equation I'm not thinking of:
for (var yCounter = 0; yCounter < _labelSettings.LabelPerColumn; yCounter++)
{
for (var xCounter = 0; xCounter < _labelSettings.ColumnsPerPage; xCounter++)
{
foreach (var table in customNugget.PdfTables)
{
table.YPosition = SheetSettings.PageHeight -
(verticalOffset + yCounter * ((verticalSize + verticalOffset)));
table.XPosition = horizontalOffset + xCounter * ((horizontalSize + horizontalOffset));
}
}
}
EDIT
private static int _cellsPerRow = 3;
private static int _startIndex;
static void Main(string[] args)
{
string userInput = Console.ReadLine();
_startIndex = int.Parse(userInput);
int startY = _startIndex / _cellsPerRow;
int startX = (_startIndex - 1) % _cellsPerRow;
Console.WriteLine(string.Format("The index you chose lives in the following matrix location:\n Row: {0} Column: {1}", startY, startX));
Console.WriteLine("Press any key to continue...");
Console.Read();
}
Well assuming you know the number of cells in each row (which I think should be _labelSettings.ColumnsPerPage
), you can calculate start values like so:
int cells = 5;//number of cells per row
int startAt = 6;//cell number to start at
int startY = (startAt - 1) / cells;//1
int startX = (startAt - 1) % cells;//0
You can then use these start values to initialize your loops, however you need to be careful to ensure your inner loop only uses the calculated start value on the first run. To do this you can just reset startX
after the first execution of your y loop. Like so:
for (var yCounter = startY; yCounter < _labelSettings.LabelPerColumn; yCounter++)
{
for (var xCounter = startX; xCounter < _labelSettings.ColumnsPerPage; xCounter++)
{
//do stuff
}
startX = 0;//reset so that the next execution starts at the beginning of the row
}
Here is proof of the logic, though it is in javascript
EDIT: The calculation for startY
has been modifed to correctly handle starting with the last number is a row (Thanks to @Zach for pointing the error out)