none
checking brush color in paint event override

    Question

  • Hi,

    I am overriding paint event of datagrid to change the backcolor of datagrid cell.

    I want to check if the cell color is not red then change cell color otherwise dont change color.

    In

    protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)

    Which argument should I put in If condition ??

    To set the color I set backBrush = Brushes.Red so I guess I should check the backBrush for the color but the problem is that there is no backBrush.Color property.

    How can I check Color property of System.Drawing.Brush class to see if its Red or not.

    This is the code I have in overridden paint function.

    protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source,

    int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)

    {

    string _color = "";

    try

    {

    _DataValue = this.GetColumnValueAtRow(source,rowNum).ToString();

    ColumnStyleEventArgs e = new ColumnStyleEventArgs(rowNum, _col, _DataValue, _color) ;

    EventColumnStyleEventHandler(this, e);

    if (e.ColorValue.ToLower() == "yellow")

    backBrush = Brushes.Yellow;

    else if (e.ColorValue.ToLower() == "red")

    backBrush = Brushes.Red;

    else if (e.ColorValue.ToLower() == "blue")

    backBrush = Brushes.LightBlue;

    else if (e.ColorValue.ToLower() == "green")

    backBrush = Brushes.Green;

    base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);

     

    }

    catch(Exception ex)

    {

    MessageBox.Show("Paint : " + ex.Message.ToString());

    }

    }

    Thanks,

     

    Monday, April 17, 2006 2:36 PM

All replies

  • Try casting the backBrush to a SolidBrush and then check the BackColor property.

    Tony

    Monday, April 17, 2006 3:05 PM
  • I did'nt find any BackColor property for SolidBrush. I only found COlor property.

    I am doing this

    if(((SolidBrush)backBrush) != Brushes.Red)

    but it never stops there. I added watch  and found out that

    ((SolidBrush)backBrush).Color.Name is always "Window"

    It should'nt be the case because the color of the cell was red in that case. Do I need to add something else in the if condition.

    I also tried this

    if(((SolidBrush)backBrush).Color != Color.Red) but same result, this condition never gets true as the Color is always "Window"

    Heres the problem again.

    I set colors of datagrid cells to red and other colors by overriding paint method.

    Once the colors are set in the beginning I dont want to reset the colors of the cell which are painted as RED.

    To do that I am trying to add an if condition in the override paint method to not to change the brush color if the current brush color is Red. This condition never becomes true as the Brush Color is always "Window" even though the cell being painted is red in Color.

    Thanks,

    Monday, April 17, 2006 4:15 PM