c# - Can't properly align my receipt lines -
i having problems aligning 2 variables in receipt building graphics.drawstring(). trying achieve:
<indent>cash....................500.00 <indent>master card............1000.00 <indent>american express.....10,000.00
this have
foreach (var item in gc.payment_repo) //entity framework { int typelength = item.type.length; int amountlength = item.amount.length; graphics.drawstring(" " + item.type.padright(20, '.') + item.amount.padleft(typelength)); }
here working concept using rectangle
, stringformat
align text. sample below must in onpaint()
of control
or printdocument
. can create form
, override onpaint
, paste code below:
list<dynamic[]> rows = new list<dynamic[]>(); rows.add(new dynamic[] { "cash", 500 }); rows.add(new dynamic[] { "master card", 1000 }); rows.add(new dynamic[] { "american express", 10000 }); graphics g = e.graphics; font f = new font("courier new", 8f); //1st column stringformat sf1 = new stringformat(); sf1.alignment = stringalignment.near; sf1.linealignment = stringalignment.center; //2nd column stringformat sf2 = new stringformat(); sf2.alignment = stringalignment.far; sf2.linealignment = stringalignment.center; (int = 0; < rows.count; i++) { int x = 10; //change indentation (where want x position), 10px int y = f.height * i; int colwidth = 125; //you can change set each column's width rectangle r1 = new rectangle(x, y, colwidth, f.height); rectangle r2 = new rectangle(r1.right, y, colwidth, f.height); g.drawrectangle(pens.black, r1); //just debug rect area g.drawrectangle(pens.black, r2); g.drawstring(rows[i][0], f, brushes.black, r1, sf1); g.drawstring(rows[i][1].tostring("0.00"), f, brushes.black, r2, sf2); }
the idea use rectangle
area specify actual position of text want printed. while stringformat
helps in alignment.
Comments
Post a Comment