Home Forums WinForms controls Xceed Grid for WinForms Deletion Button for row Reply To: Deletion Button for row

User (Old forums)
Member
Post count: 23064

Below is the code for the button cell itself. I’ve written it in .NET 2.0 there is a better way to draw the button as well with the button renderer but you get the picture.

namespace Example
{

/// <summary>
/// Cell that contains a button
/// </summary>
/// <remarks>Draws a button in a cell that can be clicked</remarks>
internal class ButtonCell : Xceed.Grid.DataCell
{

#region ” Fields “

/// <summary>
/// The text that will be drawn on the cell
/// </summary>
private string m_buttonText = string.Empty;

#endregion

#region ” Constructor “

/// <summary>
/// Create the new filter concat cell row
/// </summary>
public ButtonCell()
: base()
{ }

/// <summary>
/// Create an new button cell based on our template cell
/// </summary>
/// <param name=”template”></param>
protected ButtonCell(ButtonCell templateCell)
: base(templateCell)
{ }

/// <summary>
/// Create a new cell from the parent column.
/// </summary>
/// <param name=”parentColumn”>The parent column to create from</param>
public ButtonCell(Xceed.Grid.Column parentColumn)
: base(parentColumn)
{ }

#endregion

#region ” Properties “

/// <summary>
/// The text that wil be dispalyed in the button in this cell
/// </summary>
public string ButtonText
{
get { return m_buttonText; }
set
{
if(null == value)
{ m_buttonText = string.Empty; }
else
{ m_buttonText = value; }
}
}

#endregion

#region ” Methods “

/// <summary>
/// Create an instance of a button cell that will be added to a grid
/// </summary>
/// <returns></returns>
protected override Xceed.Grid.Cell CreateInstance()
{
return new ButtonCell(this);
}

/// <summary>
/// Draws the button normally so the user isn’t over it
/// </summary>
/// <param name=”g”>The graphics to draw with</param>
/// <param name=”buttonArea”>The area to draw</param>
/// <param name=”buttonText”>The text to draw on the button</param>
private void DrawButtonNormal(System.Drawing.Graphics g)
{

// If visual styles supported then draw a better looking button
if(System.Windows.Forms.Application.RenderWithStyles)
{

System.Windows.Forms.VisualStyles.VisualStyleRenderer buttonDraw = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Button.PushButton.Normal);
buttonDraw.DrawBackground(g, DisplayRectangle);

}
else
{

System.Windows.Forms.ControlPaint.DrawButton(g, DisplayRectangle, System.Windows.Forms.ButtonState.Flat);

}
DrawString(g, false);

}

/// <summary>
/// Draws a button when the mouse is hovering over it
/// </summary>
/// <param name=”g”>The graphics to draw with</param>
/// <param name=”buttonArea”>The button area</param>
/// <param name=”buttonText”>The text to draw on the button</param>
private void DrawButtonHot(System.Drawing.Graphics g)
{

// If visual styles supported then draw!
if(System.Windows.Forms.VisualStyles.VisualStyleRenderer.IsSupported)
{

System.Windows.Forms.VisualStyles.VisualStyleRenderer buttonDraw = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Button.PushButton.Hot);
buttonDraw.DrawBackground(g, DisplayRectangle);

}
else
{

System.Windows.Forms.ControlPaint.DrawButton(g, DisplayRectangle, System.Windows.Forms.ButtonState.Normal);

}

DrawString(g, false);

}

/// <summary>
/// Draws a button when the user is pressing it
/// </summary>
/// <param name=”g”>The graphics to draw with</param>
/// <param name=”buttonArea”>The button area</param>
/// <param name=”buttonText”>The text to draw on the button</param>
private void DrawButtonPressed(System.Drawing.Graphics g)
{

// If visual styles supported then draw!
if(System.Windows.Forms.VisualStyles.VisualStyleRenderer.IsSupported)
{

System.Windows.Forms.VisualStyles.VisualStyleRenderer buttonDraw = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Button.PushButton.Pressed);
buttonDraw.DrawBackground(g, DisplayRectangle);

}
else
{

System.Windows.Forms.ControlPaint.DrawButton(g, DisplayRectangle, System.Windows.Forms.ButtonState.Pushed);

}

DrawString(g, true);

}

/// <summary>
/// Draws the button string. If this is the last row in the grid the text
/// should be Add otherwise remove
/// </summary>
/// <param name=”g”>The graphics to draw with</param>
/// <param name=”textArea”>The area to draw in</param>
/// <param name=”text”>The text to draw</param>
/// <param name=”isPressed”>If the button is pressed then move the text slightly</param>
private void DrawString(System.Drawing.Graphics g, bool isPressed)
{

System.Drawing.StringFormat format = new System.Drawing.StringFormat();
format.Alignment = System.Drawing.StringAlignment.Center;
format.LineAlignment = System.Drawing.StringAlignment.Center;
System.Drawing.Rectangle textArea = DisplayRectangle;
if(isPressed)
textArea.Offset(1, 1); SDJXCEED@20

Imported from legacy forums. Posted by Chris (had 5113 views)