We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

pushbutton Background color

Hi I am trying to change a background color of some cells with "pushbutton" style using the following syntax : MyG.Item(1, 1).BackColor = Color.Red MyG.Item(1, 1).TextColor = Color.White but the text color changes only. The background remains grey . I need to change with different colors some buttons to indicate different states. Thanks Fabrizio.

9 Replies

AD Administrator Syncfusion Team June 29, 2003 07:26 AM

The button is drawn using the ControlPaint. DrawButton routine (or using WinXP Themed draw button code) and that will always have the same system color used for buttons. In order to change the color you could instead provide your own drawing logic by handling the DrawCellButtonBackground event. Here is an example: (from OnDrawCellButtonBackground classref of Essential Grid 1.6 version) [C#] public static void Draw3dFrame(Graphics g, int x0, int y0, int x1, int y1, int w, Color rgbTopLeft, Color rgbBottomRight) { Rectangle rc; for (int i = 0; i != w; i++) { // Top Brush brTL = new SolidBrush(rgbTopLeft); rc = Rectangle.FromLTRB(x0, y0, x1, y0+1); g.FillRectangle(brTL, rc); // Left rc = Rectangle.FromLTRB(x0, y0, x0+1, y1); g.FillRectangle(brTL, rc); brTL.Dispose(); Brush brBR = new SolidBrush(rgbBottomRight); // Bottom rc = Rectangle.FromLTRB(x0, y1, x1+1, y1+1); g.FillRectangle(brBR, rc); // Right rc = Rectangle.FromLTRB(x1, y0, x1+1, y1); g.FillRectangle(brBR, rc); brBR.Dispose(); if (i != w-1) { x0++; y0++; x1--; y1--; } } } protected override void OnDrawCellButtonBackground(GridCellButton button, Graphics g, Rectangle rect, ButtonState buttonState, GridStyleInfo style) { bool drawPressed = (buttonState & ButtonState.Pushed) != 0; Color hilight = SystemColors.ControlLightLight; Color shadow = SystemColors.ControlDarkDark; if (!drawPressed) { GridPaint.Draw3dFrame(g, rect.Left, rect.Top, rect.Right-1, rect.Bottom-1, 1, hilight, shadow); } else { Brush br = new SolidBrush(shadow); g.FillRectangle(br, Rectangle.FromLTRB(rect.Left, rect.Bottom-1, rect.Right-1, rect.Bottom)); g.FillRectangle(br, Rectangle.FromLTRB(rect.Right-1, rect.Top, rect.Right, rect.Bottom)); br.Dispose(); } } [VB] Public Shared Sub Draw3dFrame(g As Graphics, x0 As Integer, y0 As Integer, x1 As Integer, y1 As Integer, w As Integer, rgbTopLeft As Color, rgbBottomRight As Color) Dim rc As Rectangle Dim i As Integer While i <> w ' Top Dim brTL = New SolidBrush(rgbTopLeft) rc = Rectangle.FromLTRB(x0, y0, x1, y0 + 1) g.FillRectangle(brTL, rc) ' Left rc = Rectangle.FromLTRB(x0, y0, x0 + 1, y1) g.FillRectangle(brTL, rc) brTL.Dispose() Dim brBR = New SolidBrush(rgbBottomRight) ' Bottom rc = Rectangle.FromLTRB(x0, y1, x1 + 1, y1 + 1) g.FillRectangle(brBR, rc) ' Right rc = Rectangle.FromLTRB(x1, y0, x1 + 1, y1) g.FillRectangle(brBR, rc) brBR.Dispose() If i <> w - 1 Then x0 += 1 y0 += 1 x1 -= 1 y1 -= 1 End If i += 1 End While End Sub 'Draw3dFrame Protected Overrides Sub OnDrawCellButtonBackground(button As GridCellButton, g As Graphics, rect As Rectangle, buttonState As ButtonState, style As GridStyleInfo) Dim drawPressed As Boolean = (buttonState And ButtonState.Pushed) <> 0 Dim hilight As Color = SystemColors.ControlLightLight Dim shadow As Color = SystemColors.ControlDarkDark If Not drawPressed Then GridPaint.Draw3dFrame(g, rect.Left, rect.Top, rect.Right - 1, rect.Bottom - 1, 1, hilight, shadow) Else Dim br = New SolidBrush(shadow) g.FillRectangle(br, Rectangle.FromLTRB(rect.Left, rect.Bottom - 1, rect.Right - 1, rect.Bottom)) g.FillRectangle(br, Rectangle.FromLTRB(rect.Right - 1, rect.Top, rect.Right, rect.Bottom)) br.Dispose() End If End Sub 'OnDrawCellButtonBackground Stefan


FA fabrizio June 30, 2003 04:46 AM

Thanks Stefan. I try but the example is not directly usable. I define the following event : Private Sub MyG_DrawCellButtonBackground(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridDrawCellButtonBackgroundEventArgs) Handles GridControl1.DrawCellButtonBackground Dim drawPressed As Boolean = (e.ButtonState And e.ButtonState.Pushed) <> 0 Dim hilight As Color = SystemColors.ControlLightLight Dim shadow As Color = SystemColors.ControlDarkDark If Not drawPressed Then Draw3dFrame(e.Graphics, e.Button.Bounds.Left, e.Button.Bounds.Top, e.Button.Bounds.Right - 1, e.Button.Bounds.Bottom - 1, 1, hilight, shadow) Else Dim br = New SolidBrush(shadow) e.Graphics.FillRectangle(br, Rectangle.FromLTRB(e.Button.Bounds.Left, e.Button.Bounds.Bottom - 1, e.Button.Bounds.Right - 1, e.Button.Bounds.Bottom)) e.Graphics.FillRectangle(br, Rectangle.FromLTRB(e.Button.Bounds.Right - 1, e.Button.Bounds.Top, e.Button.Bounds.Right, e.Button.Bounds.Bottom)) br.Dispose() End If e.Cancel = True End Sub Now some grafic of buttons are modified but are no good, only partial buttons are drawing . The sub Draw3dFrame does not fill the rectagle of botton but only the border. In any case only forcing e.cancel = true the layout of buttons is modified. Can You give me some more litte help? Thanks Fabrizio


AD Administrator Syncfusion Team June 30, 2003 06:35 AM

Attached is a little sample based on Stefan's code. Is this what you need?


FA fabrizio June 30, 2003 07:58 AM

Thanks Clay , but i don't see any file attached Bye Fabrizio


AD Administrator Syncfusion Team June 30, 2003 04:42 PM

it has been a long day. Sorry...


FA Fabrizio June 30, 2003 10:08 PM

> it has been a long day. Sorry... > Thanks is perfect ! Every day is a to long day for me ! Bye Fabrizio


GW Greg Wright March 18, 2004 08:21 AM

I need to do something similar, but need to know which row the button be drawn is on, and the event args for DrawCellButtonBackground doesn''t contain the row index. Is this possible? Thanks. Greg


AD Administrator Syncfusion Team March 18, 2004 10:48 AM

Check e.Style.CellIdentity.RowIndex to see if that has what you need.


AD Administrator Syncfusion Team September 19, 2006 07:39 PM



>The button is drawn using the ControlPaint. DrawButton routine (or using WinXP Themed draw button code) and that will always have the same system color used for buttons.
>
>In order to change the color you could instead provide your own drawing logic by handling the DrawCellButtonBackground event.
>
>Here is an example: (from OnDrawCellButtonBackground classref of Essential Grid 1.6 version)
>
>[C#]
>public static void Draw3dFrame(Graphics g, int x0, int y0, int x1, int y1, int w, Color rgbTopLeft, Color rgbBottomRight)
>{
> Rectangle rc;
>
> for (int i = 0; i != w; i++)
> {
> // Top
> Brush brTL = new SolidBrush(rgbTopLeft);
> rc = Rectangle.FromLTRB(x0, y0, x1, y0+1);
> g.FillRectangle(brTL, rc);
>
> // Left
> rc = Rectangle.FromLTRB(x0, y0, x0+1, y1);
> g.FillRectangle(brTL, rc);
> brTL.Dispose();
>
> Brush brBR = new SolidBrush(rgbBottomRight);
>
> // Bottom
> rc = Rectangle.FromLTRB(x0, y1, x1+1, y1+1);
> g.FillRectangle(brBR, rc);
>
> // Right
> rc = Rectangle.FromLTRB(x1, y0, x1+1, y1);
> g.FillRectangle(brBR, rc);
> brBR.Dispose();
>
> if (i != w-1)
> {
> x0++;
> y0++;
> x1--;
> y1--;
> }
> }
>}
>protected override void OnDrawCellButtonBackground(GridCellButton button, Graphics g, Rectangle rect, ButtonState buttonState, GridStyleInfo style)
>{
> bool drawPressed = (buttonState & ButtonState.Pushed) != 0;
> Color hilight = SystemColors.ControlLightLight;
> Color shadow = SystemColors.ControlDarkDark;
> if (!drawPressed)
> {
> GridPaint.Draw3dFrame(g, rect.Left, rect.Top, rect.Right-1, rect.Bottom-1, 1,
> hilight, shadow);
> }
> else
> {
> Brush br = new SolidBrush(shadow);
> g.FillRectangle(br, Rectangle.FromLTRB(rect.Left, rect.Bottom-1, rect.Right-1, rect.Bottom));
> g.FillRectangle(br, Rectangle.FromLTRB(rect.Right-1, rect.Top, rect.Right, rect.Bottom));
> br.Dispose();
> }
>}
>
>[VB]
>
>Public Shared Sub Draw3dFrame(g As Graphics, x0 As Integer, y0 As Integer, x1 As Integer, y1 As Integer, w As Integer, rgbTopLeft As Color, rgbBottomRight As Color)
> Dim rc As Rectangle
>
> Dim i As Integer
>
> While i <> w
> '' Top
> Dim brTL = New SolidBrush(rgbTopLeft)
> rc = Rectangle.FromLTRB(x0, y0, x1, y0 + 1)
> g.FillRectangle(brTL, rc)
>
> '' Left
> rc = Rectangle.FromLTRB(x0, y0, x0 + 1, y1)
> g.FillRectangle(brTL, rc)
> brTL.Dispose()
>
> Dim brBR = New SolidBrush(rgbBottomRight)
>
> '' Bottom
> rc = Rectangle.FromLTRB(x0, y1, x1 + 1, y1 + 1)
> g.FillRectangle(brBR, rc)
>
> '' Right
> rc = Rectangle.FromLTRB(x1, y0, x1 + 1, y1)
> g.FillRectangle(brBR, rc)
> brBR.Dispose()
>
> If i <> w - 1 Then
> x0 += 1
> y0 += 1
> x1 -= 1
> y1 -= 1
> End If
> i += 1
> End While
>End Sub ''Draw3dFrame
>
>Protected Overrides Sub OnDrawCellButtonBackground(button As GridCellButton, g As Graphics, rect As Rectangle, buttonState As ButtonState, style As GridStyleInfo)
> Dim drawPressed As Boolean = (buttonState And ButtonState.Pushed) <> 0
> Dim hilight As Color = SystemColors.ControlLightLight
> Dim shadow As Color = SystemColors.ControlDarkDark
> If Not drawPressed Then
> GridPaint.Draw3dFrame(g, rect.Left, rect.Top, rect.Right - 1, rect.Bottom - 1, 1, hilight, shadow)
> Else
> Dim br = New SolidBrush(shadow)
> g.FillRectangle(br, Rectangle.FromLTRB(rect.Left, rect.Bottom - 1, rect.Right - 1, rect.Bottom))
> g.FillRectangle(br, Rectangle.FromLTRB(rect.Right - 1, rect.Top, rect.Right, rect.Bottom))
> br.Dispose()
> End If
>End Sub ''OnDrawCellButtonBackground
>
>
>Stefan

Loader.
Live Chat Icon For mobile
Up arrow icon