SyntaxHighlighter

Tuesday, 8 November 2011

Add a Border to a Panel Using .NET Compact Framework

After spending quite some time working out how to add a border round a panel control in the .NET compact framework, I finally got it working. It's pretty easy to be honest.

VB.NET
Using g As Graphics = e.Graphics
    Using p As New Pen(Color.Black, 1)
        g.DrawRectangle(p, 0, 0, MyPanel.Width - 1, MyPanel.Height)
    End Using
End Using

C#
Using (Graphics g = e.Graphics) {
    Using (Pen p = New Pen(Color.Black, 1)) {
        g.DrawRectangle(p, 0, 0, MyPanel.Width - 1, MyPanel.Height);
    }
}

You need to make sure that this code is put in the Paint event of your panel.