Home Forums WinForms controls Xceed Grid for WinForms Forecolor only for a part of a cell

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

    Is it possible to change the forecolor for only a part of a cell? So first 2 words of the cell are in red color while the other words are in blue color?

    Imported from legacy forums. Posted by Kai (had 4839 views)

    User (Old forums)
    Member
    Post count: 23064

    To do this, you can create a class that implements the <b>Xceed.Grid.ICellViewer</b> interface, and set the <b>CellViewer</b> property of the column(s) to an instance of this class. In your class, you should write code that paints the content of the cell like you want. This is how it should look (untested code):<code>using Xceed.Grid;
    public class MyCellViewer : ICellViewer
    {
    public int GetFittedWidth(Cell cell) { return -1; }
    public int GetFittedHeight(Cell cell, AutoHeightMode mode) { return -1; }
    public bool PaintCellValue(GridPaintEventArgs e, Cell cell) {
    // here you put your paint code.
    // here are some objects you might need:
    // string text = cell.GetTextToPaint(); // the text of the cell
    // Graphics g = e.Graphics; // the Graphics object to paint on
    // Rectangle rect = e.DisplayRectangle; // the display rectangle of the cell
    }
    }</code>

    Imported from legacy forums. Posted by Tommy (had 200 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Thommy,

    thanks for your reply, but actually this did not help me too much. I already thought I need to use a new cellviewer, but how could I define the colors, or better: How could I define which words are then displayed in which color?…. Sorry for bothering you again… :~

    Imported from legacy forums. Posted by Kai (had 324 views)

    User (Old forums)
    Member
    Post count: 23064

    Well, that’s up to you. You decide which words are painted in which color. If you create a custom CellViewer, in the PaintCellValue method you have to paint everything yourself. You’ll have to use the methods provided by the Graphics object.

    Imported from legacy forums. Posted by Tommy (had 191 views)

    User (Old forums)
    Member
    Post count: 23064

    Hmm… so I need to create a cellviewer for each different line? During design time I do not know the text which should be displayed, but there are about 20 different rows and I always want to display the first 1,2 or 3 words in red and the others in blue. Is this easily possible?

    Imported from legacy forums. Posted by Kai (had 274 views)

    User (Old forums)
    Member
    Post count: 23064

    You only need 1 CellViewer. Inside the CellViewer, you should write logics that analyze the cell value and decide what to paint in what color. What rules are there? Is it always the first X words that are in red?

    Imported from legacy forums. Posted by Tommy (had 181 views)

    User (Old forums)
    Member
    Post count: 23064

    It’s some kind of match data, e.g. Team A – Team B. So I could use the – as the seperator. May I ask you to post just some short code how to do this? I’m not sure with these paint thinks… Many many thanks in advance!!

    Imported from legacy forums. Posted by Kai (had 267 views)

    User (Old forums)
    Member
    Post count: 23064

    Try this (untested) code:<code>public bool PaintCellValue(GridPaintEventArgs e, Cell cell) {
    string value = cell.GetTextToPaint();
    if (value.Length == 0) return;
    string textRed = “”, textBlue = “”;
    int indexDash = value.IndexOf(‘-‘);
    if (indexDash < 0) { textRed = value; }
    else if (indexDash == 0) { textBlue = value.Substring(1); }
    else {
    textRed = value.Substring(0, indexDash);
    textBlue = value.Substring(indexDash+1);
    }
    Graphics g = e.Graphics;
    Font font = cell.Font;
    Rectangle r = e.DisplayRectangle;
    r.Inflate(-1, -1);
    SizeF sizeRed = g.MeasureString(textRed, font);
    SizeF sizeBlue = g.MeasureString(textBlue, font);
    SizeF sizeDash = g.MeasureString(” – “, font);
    using(SolidBrush brushRed = new SolidBrush(Color.Red)) {
    RectangleF rectangleRed = (RectangleF)r;
    rectangleRed.Width = sizeRed.Width;
    g.DrawString(textRed, font, brushRed, rectangleRed);
    }
    using(SolidBrush brushDash = new SolidBrush(Color.Black)) {
    RectangleF rectangleDash = (RectangleF)r;
    rectangleDash.Width = sizeDash.Width;
    rectangleDash.Offset(sizeRed.Width + 1f, 0f);
    g.DrawString(” – “, font, brushDash, rectangleDash);
    }
    using(SolidBrush brushBlue = new SolidBrush(Color.Blue)) {
    RectangleF rectangleBlue = (RectangleF)r;
    rectangleBlue.Width = sizeBlue.Width;
    rectangleBlue.Offset(sizeRed.Width + sizeDash.Width + 2f, 0f);
    g.DrawString(textBlue, font, brushBlue, rectangleBlue);
    }
    }</code>

    Imported from legacy forums. Posted by Tommy (had 6101 views)

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