Home Forums WinForms controls Xceed Grid for WinForms Fix for right alignment?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #12672 |

    I am experiencing problems when trying to align the contents in textcells to right. Text with the same length aligns fine to the right, but the longer text you enter into the cells the longer to the left it is being printed. If anyone has a fix for this bug I’d be very thankful!

    this is the only codeline I used for right alignment:
    newColumn.HorizontalAlignment = Xceed.Grid.HorizontalAlignment.Right;

    /David.

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

    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)

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.