循环与条件问题 [英] Loop with condition issue
问题描述
嗨
尝试用条件创建某种类型的循环。
我需要的是在一定数量的文本时添加了一行,然后移动到新页面,然后继续添加更多,并且每次都能执行相同的操作。
如果尝试了一些循环'','if',' foreach'和'for'但是想想我放置它的方式可能是不正确的。
下面的代码运行数据网格上的每一行。这是有效的。
这个foreach应该运行让我们说10次然后新页面直到'N'。 N不能定义数据库总是会改变多少金额。
o这是我发现最适合我打印的方式
thx任何帮助
我尝试过:
Hi
Trying to create some type of loop with a condition.
what i need is when at a certain amount of text lines is added it moves to new page then continue adding more with the ability to do the same every time.
If tried a few loop 'while's, 'if's, 'foreach' and 'for' but think the way i placed it might have been incorrect.
the code below run for every row on a datagrid. this works.
this foreach should run lets say for 10 times then new page untill 'N'. N cant be define how many amount as the database will always change.
o this is a way i found works best for me to print also
thx for any help
What I have tried:
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
PartN = row.Cells[0].Value.ToString();
SerialN = row.Cells[1].Value.ToString();
Descrip = row.Cells[2].Value.ToString();
RTBox.SelectionFont = new Font("Courier New", 12);
RTBox.AppendText(Descrip);
RTBox.SelectionFont = new Font(barcodeName, 26);
RTBox.AppendText(Environment.NewLine + PartN);
RTBox.SelectionFont = new Font("Courier New", 12);
RTBox.AppendText(Environment.NewLine + PartN);
RTBox.SelectionFont = new Font("Courier New", 12);
if (SerialN == "")
{
}
else
{
RTBox.AppendText(Environment.NewLine + "S/N: " + SerialN);
}
RTBox.AppendText(Environment.NewLine + Environment.NewLine);
//RTBox.SelectedRtf = @"{\rtf1 \page}";
}
推荐答案
您只需要一个定义页面上行数的变量,一个将行计为已处理的行。然后将计数器初始化为0并在每次添加行时计数,例如:
You just need some variable that defines the number of lines on a page, and one that counts the lines as processed.. You then initialise the counter to 0 and count up each time you add a line, something like:
const int maxLines = 66;
int pageNumber = 0;
int lineNumber = 0;
foreach (item that will be processed)
{
process(theItem);
lineNumber += 1;
if (lineNumer == maxLines)
{
pageNumber += 1;
do end of page processing
lineNumber = 0;
}
}
这篇关于循环与条件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!