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




thanks. you’ve given what i want
If I understand your requirements right, an easier solution would be to use the SystemInformation class:
int TitlebarHeight = SystemInformation.CaptionHeight;
int BorderWidth = SystemInformation.BorderSize;
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.
BorderWidth = SystemInformation.FrameBorderSize.Width
returns the same value as
BorderWidth = (Me.Width – Me.ClientSize.Width) /2
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.
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!
Great, I did not know about that until now. Thx!
Hiya,
I know i’m living in the past but any idea how to do this in VB6 please?
many thanks …
Old reply, but in VB6 it is .Width – .ScaleWidth & .Height – .ScaleHeight
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;
Thanks Sanjeevi!
Good job with the sharing.
thanks
vb.net does not need ;
Dim BorderWidth as Integer = (Me.Width – Me.ClientSize.Width) /2;
Dim TitlebarHeight as Integer = Me.Height – Me.ClientSize.Height – 2 * BorderWidth;
Thanks …very clean and simple code.
int BorderWidth = SystemInformation.FrameBorderSize.Width;
int TitlebarHeight = SystemInformation.CaptionHeight + BorderWidth;
works better
Akıllı ol doğru düzgün yaz minusları
Many thanks to Sanjeevi and darvkdoid
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.
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
********************************************************************************
********************************************************************************
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)
********************************************************************************
********************************************************************************
You can also turn on “View All Files” in the solution explorer and open the Designer.vb file