2016-04-08 56 views
0
int line = 0;///i want to reset this back to 0/// 
    private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) 
    { 
     int y = 0; 
     Console.WriteLine("1st " + line); 
     do 
     { 
      e.Graphics.DrawString(invoiceList[line], new Font("Arial", 10, FontStyle.Regular), Brushes.Black, 50, 200); 
      Console.WriteLine("2nd " + line); 
      line += 1; 
      Console.WriteLine("3rd " + line); 
      y += 1; 

      if (y > 0) 
      { 
       e.HasMorePages = line != invoiceList.Count(); 

       Console.WriteLine("4th " + line); 
       break; 
      } 
     } while (line < invoiceList.Count()); 
     Console.WriteLine("6th " + line); 
    } 

如何重置變量line?因爲當我按下從printPreviewDialog產生的System.ArgumentOutOfRangeException的打印按鈕時,它只是不斷加起來。編輯1: 對不起,我看起來不清楚。它的這條線e.Graphics.DrawString(invoiceList[line], new Font("Arial", 10, FontStyle.Regular), Brushes.Black, 50, 200);這給我一個錯誤。c#do-while循環如何重置變量?

所以第一this is what happen when i press the print button

如可以從圖片「第六(line)= 12」或索引12.

所以看到,當我從打印預覽再次按下打印按鈕打印的物理複製它會顯示this。對不起,我解釋事情真的很糟糕。

+0

在'event'中聲明這個變量('int line = 0;')。 –

回答

0
private void printDocument1_BeginPrint(object sender, PrintEventArgs e) 
    { 
     line = 0; 
    } 

感謝您的時間傢伙,我理解了它:P。

0

你必須聲明printDocument1_PrintPage裏面的變量。因此每次都會使用0進行初始化。因此事件簽名可能看起來像:

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) 
    { 
     int y = 0, line = 0; 
     //Rest of code here 
    } 

或者你可以把它的循環後重置爲0,如果是這樣的片段將是這樣的:

int line = 0; 
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) 
{ 
    int y = 0; 
    // code here 
    Console.WriteLine("6th " + line); 
    line=0; for the next time the value starts from 0 
} 
0

如果您需要i是外範圍內,您必須在內部範圍內的某處重置i = 0;

後您Console.WriteLine("6th " + line);您可以後添加一行,並只說i = 0;

這樣你的代碼做你問什麼,你仍然有對i外部範圍。

0

//設置0

 if (y > 0) 
     { 
      e.HasMorePages = line != invoiceList.Count(); 

      Console.WriteLine("4th " + line); 
      break; 
     } 
    } while (line < invoiceList.Count()); 
    Console.WriteLine("6th " + line); 
    line = 0; 
}