Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I change the border color, width, or style of a control?

Override the control's OnPaint method. Here is a sample custom Button.

[C#]

using System.Drawing;
using System.Windows.Forms;

public class ButtonWithCustomBorder : Button
{
  protected override void OnPaint( PaintEventArgs e )
  {
    base.OnPaint( e );
    
    Color borderColor = Color.Blue;
    int borderWidth = 1; 
    int borderStyle = ButtonBorderStyle.Solid;

    ControlPaint.DrawBorder(
      e.Graphics, e.ClipRectangle,
      borderColor, borderWidth, borderStyle,
      borderColor, borderWidth, borderStyle,
      borderColor, borderWidth, borderStyle, 
      borderColor, borderWidth, borderStyle ); 
  }
}

[Visual Basic]

Public Class ButtonWithCustomBorder
  Inherits Button

  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    MyBase.OnPaint(e)

    Dim borderWidth As Integer = 1
    Dim borderColor As Color = Color.Blue
    Dim borderStyle As ButtonBorderStyle = ButtonBorderStyle.Solid

    ControlPaint.DrawBorder( _
      e.Graphics, e.ClipRectangle, _
      borderColor, borderWidth, borderStyle, _
      borderColor, borderWidth, borderStyle, _
      borderColor, borderWidth, borderStyle, _
      borderColor, borderWidth, borderStyle)
  End Sub
End Class

Contributed from George Shepherd's Windows Forms FAQ



Page view counter