Home Forums WinForms controls Xceed Grid for WinForms Fix for right alignment? Reply To: Fix for right alignment?

User (Old forums)
Member
Post count: 23064

Okey.. Now I have found a way to solve the problem. It consists of overriding the PaintForeground method for the cell-class. Looks like this:

protected override void PaintForeground(Xceed.Grid.GridPaintEventArgs e)
{
if (this.HorizontalAlignment == Xceed.Grid.HorizontalAlignment.Right)
{
if (this.Value == null || this.Value is System.DBNull)
return;
string text = System.Convert.ToString(this.Value);
if (text == “”)
return;

int width = MeasureDisplayStringWidth(e.Graphics,text,this.Font);
System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(this.ForeColor);
System.Drawing.StringFormat format = new System.Drawing.StringFormat();
e.Graphics.DrawString(text,this.Font,brush,e.ClientRectangle.Width – width,e.ClientRectangle.Top,format);
}
else
{
base.PaintForeground(e);
}
}

static public int MeasureDisplayStringWidth(System.Drawing.Graphics graphics, string text,System.Drawing.Font font)
{
System.Drawing.StringFormat format = new System.Drawing.StringFormat ();
System.Drawing.RectangleF rect = new System.Drawing.RectangleF(0, 0, 1000, 1000);
System.Drawing.CharacterRange[] ranges = { new System.Drawing.CharacterRange(0, text.Length) };
System.Drawing.Region[] regions = new System.Drawing.Region[1];

format.SetMeasurableCharacterRanges(ranges);

regions = graphics.MeasureCharacterRanges(text, font, rect, format);
rect = regions[0].GetBounds(graphics);

return (int)(rect.Right + 1.0f);
}

Imported from legacy forums. Posted by David (had 3718 views)