Reputation: 21
I'm trying to determine how I would utilize a generic list to print out text with various size fonts.
I know I would have to loop through the list to send the object to the
Graphics.DrawString(String, Font, Brush, PointF)
method.
I'm just having trouble as how to set the objects in the list so I can loop through and print them.
I have a class(PrintString) that takes a string and a font as its constructor, then a list inside my print class that I set:
public List<PrintString> ps = new List<PrintString>();
I have no problem populating the list from my form:
ReceiptPrint receipt = new ReceiptPrint();
receipt.ps.Add(new PrintString(printHead,new Font("Arial", 20)));
receipt.ps.Add(new PrintString(dateTime, new Font("Arial", 14)));
receipt.Print();
The place where I'm getting stuck is inside my print class (ReceiptPrint) where I am trying to iterate through the list to pass the string and font to the DrawString method.
foreach (PrintString printString in ps)
{
e.Graphics.DrawString(ps??????????
}
Upvotes: 1
Views: 3957
Reputation: 21
It was a matter of passing the string and font I set in my PrintString class:
foreach (PrintString printString in ps)
{
e.Graphics.DrawString(printString.Text, printString.Font, Brushes.Black, printArea, format);
}
Upvotes: 1
Reputation: 4137
Create a class with members for anything unique to the particular item, such as Text, Font Size, and Location.
Then create instances of this class and add them to a new List<YourClassType>;
Finally, iterate over the list and draw the items.
Upvotes: 0