Howto add a custom border to a FormBorderStyle=None - form?

30.5k Views Asked by At

I have a form with the property FormBorderStyle set to 'None' and with a custom bar on top side for dragging and buttons.

Now I'd like to give the form a border because it's a child form and the parent form has the same background color as the child, so it's hard to see the child form. And no, I can't/won't change the background color.

Help

5

There are 5 best solutions below

0
On

Maybe you can use a BackgroundImage transparent except in the borders.

3
On

There is a way without a need to set a background image and/or fixed sized form. So this is the most proper and simple way I guess. Say you have a form named Form1, all you need to do is:

Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, Color.Black, ButtonBorderStyle.Solid)
End Sub

An alternative, if you want to use the default border provided by your Windows version:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
    Me.Text = ""
    Me.ControlBox = False
End Sub
0
On

After seeing the answer by theGD, I did the same thing for a TableLayouPanel on a form:

Private Sub TableLayoutPanel1_Paint(sender As Object, e As PaintEventArgs) Handles TableLayoutPanel1.Paint
    ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, Color.DarkOrange, ButtonBorderStyle.Solid)
End Sub
1
On

You can use the Visual Basic .NET Power Packs which you can download here. It has this Control called LineShape that you can put onto your border-less form's edges, like this one program that I am currently working on. Notice that the borders of the form are just LineShape.

The north border is just a LineShape with a BorderWidth set to 60 and the other borders' BorderWidths are set to 10.

0
On

You can use this on the form paint event:

ControlPaint.DrawBorder(e.Graphics, Me.ClientRectangle, Color.Black, ButtonBorderStyle.Solid)

This will draw the client border only, also if you are resizing the form, or maximizing the form use Me.Refresh() on form resize events so that the form redraws its borders.