tutlogger

How to Create a Customized Windows Form on VB.Net




Aside from the 7 border styles you can select from the windows form properties which can be described as overused and classic because of its limited number of styles, you might try to experiment a new style and apply this simple trick on how to create a customized Windows form on VB.Net. This is easy to do and you can apply this in just a few minutes for every form in your VB project.

So let's get started by creating a new project on Visual Studio. In this tutorial, I will be using Visual Studio 2012 so it's up to you what version you choose except that I don't think the code you will be seeing here can be applied on VB 6. So I recommend to use at least Visual Studio 2008 or other higher versions of VS. Now, name the project as new_window_form or any name you prefer.





Step 2. In your project solution explorer window, double click Form1.vb to open the form's Design tab. Re size your form if you want.




Step 3. Grab a panel from your toolbox window and place it inside your form. Name it window_panel , set the panel's back color to white, border style to fixed single and dock to fill.



Step 4. Grab another panel from your toolbox window and place it inside your first panel named window_panel. Name it window_header , set the panel's back color to black, height to 40 and dock to top. This panel will be your Title bar.



Step 5. To put a close button inside your title bar, you need to grab another panel from your toolbox window and place it inside your second panel named window_header. Name it window_header_close and dock it to right. Grab a button from your toolbox and place it inside the window_header_close panel. Change the button's text with a bold letter X and change the text fore color to red. Re size the button to fit the X letter inside  and set the button's flat style to flat. For better UI, you may use a close image icon instead of a letter X.



Write the code for the close button. Double click your window_header_close button and paste this code:

Me.Close()

For minimize and maximize buttons, paste this code:

Me.WindowState = FormWindowState.Minimized
or
Me.WindowState = FormWindowState.Maximized


Step 6. Now, select your Form1 and set its form border style to none.



Your new form will look like this.




Step 6. After making the design of your form, you need to write a few lines of code in order that your form can be moved at any point in your computer screen. To do that, go to your Form1 code and just below this line of code Public Class Form1, paste this code:

Dim mouse_move As System.Drawing.Point






Step 7. Now go back to your Form1 design tab, double click your window_header panel to view code. Change the window_header declaration to MouseMove then place this code inside.

 If (e.Button = Windows.Forms.MouseButtons.Left) Then
            Dim mposition As Point
            mposition = Control.MousePosition
            mposition.Offset(mouse_move.X, mouse_move.Y)
            Me.Location = mposition
  End If







Step 8. After putting the code inside window_header's MouseMove event, drop down again the declaration menu of window_header and select MouseDown. Add this 1 line code inside that event.

mouse_move = New Point(-e.X, -e.Y)




Finally!, hit the run command to start testing your newly customized windows form. I hope you like my tutorial on How to Create a Customized Windows Form on VB.Net. If you want to download the sample project used in this tutorial, click the download link below.









SHARE THIS POST

3 comments:

  1. The main motive of the Hadoop big data solution is to spread the knowledge so that they can give more big data engineers to the world.

    ReplyDelete
  2. Very Good tutotial!! I suggest you to modify the Maximized code button to this:
    If Me.WindowState = FormWindowState.Normal Then
    Me.WindowState = FormWindowState.Maximized
    Else
    Me.WindowState = FormWindowState.Normal
    End If

    ReplyDelete

PRIVACY POLICY | TERMS OF SERVICE | COPYRIGHT POLICY