Tuesday, August 4, 2015

Add a border in FormBorderStyle = None



Add a border in FormBorderStyle = None

When we are using Forms with the property FormBorderStyle = None is always consider adding a border into your Form. Why? Well, imagine a Windows Operative System that the Desktop Background is a black image. Now, imagine  your program with Forms with a black back color.
In this case, the user will be confused because he doesn´t know where is the form of your program. But if you add a white border it will be better right?

How to create a project in Visual Studio?

Exists many ways to create a new project in VS. For example, when you open your VS program, in the Start Page you will see the option New Project ... 
If you can´t see the Start Page you can also click in File > New Project > Select Visual Basic > Windows Forms Application
Rename your project name if you want and click OK.
It´s easy! 

Adding the border ...

For this case, I will create a rectangle with a same size and location that your form. You can also chose the width of the rectangle. In the end, it will work as a border. Simple it isn´t?
For this example, you need 2 event of your Form. Paint and Load. To open the file that contains the code press Ctrl + Alt + 0 
You will also need some lines of code out of Events space but after the Public Class of your Form like in the next image.

In that space add this lines
Private borderColor As Color = Color.FromArgb(9, 74, 178)
Private borderWidth As Integer = 2
Private formRegion As Rectangle

Now in the Paint Event:
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
   ControlPaint.DrawBorder(e.Graphics, formRegion, borderColor, _
  borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, _
  ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, _
         borderColor, borderWidth, ButtonBorderStyle.Solid)
End Sub

In the Load event: 
Don´t forget to check the size of your form. The first 2 numbers are the location. The next 2 numbers are the size.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 formRegion = New Rectangle(0, 0, 626, 372) 'size form 626; 372
End Sub

I hope this was helpfull!
See you next time!


No comments:

Post a Comment