Search code examples
c#stringalignmentdraw

Draw string alignment in c#


ptStart.X += 80;

g.DrawString(Math.Round(decimal.Parse(dr["Amount"].ToString()), 0).ToString(), font, brush, ptStart);

through above code am getting below output.enter image description here

but i need right alignment. i used also string.Format("{0,8}"). but it is also not working


Solution

  • You should use a version of Graphics.DrawString that allows you to specify a StringFormat. See: https://msdn.microsoft.com/en-us/library/21kdfbzs(v=vs.110).aspx

    The alignment of the StringFormat is what you need to specify:

    StringFormat drawFormat = new StringFormat();
    drawFormat.Alignment = StringAlignment.Far;
    

    The Rectangle argument you specify allows you to specify the area that the text is to be drawn in.