Home > C#, VB.Net > Title bar height and Form border width of .Net form

Title bar height and Form border width of .Net form


When creating the MonthPicker control I faced one problem that i have to calculate the Title bar height and border with of the form. Because I need to show another form below the user control. I know left & top position of form which has the user control and know left & top of the user control within the form. But what about the titlebar height and form border the parent form has.

Click the above pic to view all the areas

After a while, i found a solution and the code is shown below

in C#

  • int BorderWidth = (this.Width – this.ClientSize.Width) /2;
  • int TitlebarHeight = this.Height – this.ClientSize.Height – 2 * BorderWidth;

in VB.Net

  • Dim BorderWidth as Integer = (Me.Width – Me.ClientSize.Width) /2;
  • Dim TitlebarHeight as Integer = Me.Height – Me.ClientSize.Height – 2 * BorderWidth;

Happy programming

About these ads
Categories: C#, VB.Net
  1. vino
    January 12, 2007 at 12:47 PM | #1

    thanks. you’ve given what i want

  2. Sebastian Dietz
    January 29, 2007 at 6:05 PM | #2

    If I understand your requirements right, an easier solution would be to use the SystemInformation class:

    int TitlebarHeight = SystemInformation.CaptionHeight;
    int BorderWidth = SystemInformation.BorderSize;

  3. Josh
    March 22, 2007 at 4:11 PM | #3

    I’ve found that with SystemInformation.BorderSize is faulty though. It took me over an hour tracking down the bug. The code above would give me a 3 for the border size while SystemInformation would only give me a 1. I suggest using the code above over SystemInformation.

  4. Peter
    September 7, 2008 at 3:55 AM | #4

    BorderWidth = SystemInformation.FrameBorderSize.Width
    returns the same value as
    BorderWidth = (Me.Width – Me.ClientSize.Width) /2

  5. Austin
    January 14, 2009 at 8:11 PM | #5

    Josh,

    Is your control’s border style flat or 3D? There are two functions, BorderSize() and Border3DSize(). If you use the wrong one then you will not get your desired result. :) Regardless, the first function provided by Peter is what I would recommend.

  6. March 21, 2009 at 5:00 PM | #6

    What happens when you get really drunk and hook up with a transexual and she tells you that you have the losses ass she has ever seen and sticks a hamster up your ass that some how gets lost during all the excitement and while all this is going on your wife comes home with a twelve inch black cock dildo and demands to strap it on and fuck the tranny? Man, I can’t believe all this actually happened! please help!

  7. December 12, 2009 at 1:08 AM | #7

    Great, I did not know about that until now. Thx!

  8. Tyn
    December 18, 2009 at 11:42 AM | #8

    Hiya,

    I know i’m living in the past but any idea how to do this in VB6 please?

    many thanks …

    • Ashley Moore
      October 11, 2010 at 3:22 PM | #9

      Old reply, but in VB6 it is .Width – .ScaleWidth & .Height – .ScaleHeight

  9. Jerry
    May 25, 2010 at 6:21 PM | #10

    The above code did not work for me. But I used this and it worked correctly.

    int BorderWidth = SystemInformation.FrameBorderSize.Width;
    int TitlebarHeight = SystemInformation.CaptionHeight + BorderWidth;

  10. Victor
    July 2, 2010 at 2:21 AM | #11

    Thanks Sanjeevi!

    Good job with the sharing.

  11. Ali Esf
    May 13, 2011 at 2:27 PM | #12

    thanks

    vb.net does not need ; :D

    Dim BorderWidth as Integer = (Me.Width – Me.ClientSize.Width) /2;
    Dim TitlebarHeight as Integer = Me.Height – Me.ClientSize.Height – 2 * BorderWidth;

  12. July 27, 2011 at 10:44 AM | #13

    Thanks …very clean and simple code.

  13. darvkdoid
    October 14, 2011 at 8:53 PM | #14

    int BorderWidth = SystemInformation.FrameBorderSize.Width;
    int TitlebarHeight = SystemInformation.CaptionHeight + BorderWidth;

    works better

  14. Anonymous
    March 2, 2012 at 4:48 PM | #15

    Akıllı ol doğru düzgün yaz minusları

  15. Javier
    April 8, 2012 at 6:07 AM | #16

    Many thanks to Sanjeevi and darvkdoid

  16. pinkamena.D.Pie
    September 10, 2012 at 4:08 AM | #17

    Thank you Sanjeevi, solved my headache in 10 seconds flat. The SystemInformation properties just weren’t cutting it, your code gave the right values first time, every time.

  17. Zaq
    October 16, 2012 at 7:56 PM | #18

    this is what need to be done, remember border is on both sides

    width = (SystemInformation.Border3DSize.Width + SystemInformation.BorderSize.Width + SystemInformation.FrameBorderSize.Width) * 2

    height = (SystemInformation.Border3DSize.Height + SystemInformation.BorderSize.Height + SystemInformation.FrameBorderSize.Height) * 2

    if form has caption bar, also add to height SystemInformation.CaptionHeight

  18. Zaq
    October 16, 2012 at 8:06 PM | #19

    ********************************************************************************
    ********************************************************************************
    This Also works very well, no need to subtract or divide anything

    borderHeight As Integer = Me.Height – Me.ClientSize.Height
    borderWidth As Integer = Me.Width – Me.ClientSize.Width

    (win7 64b, visual studio 2012)
    ********************************************************************************
    ********************************************************************************

  19. asacremin
    March 21, 2013 at 8:22 PM | #20

    You can also turn on “View All Files” in the solution explorer and open the Designer.vb file

  20. Winston
    September 26, 2013 at 3:00 PM | #21

    The Custom Title Bar control provides customizable maximize, minimize and close buttons

  21. kendejiz
    November 1, 2013 at 3:08 PM | #22

    Thx Sanjeevi

    i try a lot for find a best solution so i get this

    int BorderWidth = (this.Width – this.ClientSize.Width) /2;
    int TitlebarHeight = this.Height – this.ClientSize.Height – BorderWidth;

    it works for every theme , i already test this code for many o.s. theme

    it’s easy to understand :
    First, get form Width and minus with workingArea, so you get border space (left & right). Then if you need only one side you must divide it by 2.

    Second, get form Height and minus with workingArea, so you get title’s space from Top with bottom border. Then if you need only title’s space(Height) you must minus it by border(from First) because of border of Form left, right and buttom are equal in every o.s. theme

    enjoy :)

  1. July 18, 2012 at 5:59 AM | #1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: