|
-
Mar 25th, 2018, 02:54 PM
#1
Thread Starter
Fanatic Member
Text Box docked to top of form, do I have to manually adjust the height?
I have a TextBox that's docked to the top of the form but I also want it to expand and contract height with the form as well. Is this something I need to do manually in the form_resize event?
Also, the Form height (568) and the TextBox height (546) do not seem to correlate to what I see on the screen and when I try to keep them at a distance of 22 in the resize event, it's all over the place.
-
Mar 25th, 2018, 05:33 PM
#2
Re: Text Box docked to top of form, do I have to manually adjust the height?
You want a 22px gap at the bottom of the form?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 25th, 2018, 05:41 PM
#3
Re: Text Box docked to top of form, do I have to manually adjust the height?
The Height of a TextBox is dependent on the Font by default. If you want to change the Height independent of the Font then you need to set Multiline to True.
Also, if you want the Height to change automatically as the Height of the form changes then you need to set the Anchor to include the Bottom edge but you cannot do that and set Dock to Top. If you want the TextBox to resize automatically then set Dock to None, manually move and resize the TextBox so that it fills the top of the form and then set Anchor to all four edges.
That said, having no gap between the edge of the form and the edge of a TextBox is rather unusual and makes for a bad UI you should have a small margin at least. I'd tend to accept the default Padding of the TextBox. Docking controls right to the edge of a form is generally reserved for containers like a Panel.
-
Mar 25th, 2018, 05:59 PM
#4
Re: Text Box docked to top of form, do I have to manually adjust the height?
You can use a TableLayoutPanel. That will allow docking, handle resizing, and allow a fixed 22px gap at the bottom of the form...
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tlp As New TableLayoutPanel
tlp.RowCount = 2
tlp.ColumnCount = 1
tlp.Dock = DockStyle.Fill
Me.Controls.Add(tlp)
Dim tb As New TextBox With {.Dock = DockStyle.Fill, .Multiline = True, .BackColor = Color.Red}
tlp.Controls.Add(tb)
Dim p As New Panel With {.Dock = DockStyle.Fill}
tlp.Controls.Add(p)
tlp.RowStyles.Add(New RowStyle(SizeType.Percent, 100))
tlp.RowStyles.Add(New RowStyle(SizeType.Absolute, 22))
tlp.SetColumn(tb, 0)
tlp.SetRow(tb, 0)
tlp.SetColumn(p, 0)
tlp.SetRow(p, 1)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 25th, 2018, 06:12 PM
#5
Re: Text Box docked to top of form, do I have to manually adjust the height?
 Originally Posted by .paul.
You can use a TableLayoutPanel. That will allow docking, handle resizing, and allow a fixed 22px gap at the bottom of the form...
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tlp As New TableLayoutPanel
tlp.RowCount = 2
tlp.ColumnCount = 1
tlp.Dock = DockStyle.Fill
Me.Controls.Add(tlp)
Dim tb As New TextBox With {.Dock = DockStyle.Fill, .Multiline = True, .BackColor = Color.Red}
tlp.Controls.Add(tb)
Dim p As New Panel With {.Dock = DockStyle.Fill}
tlp.Controls.Add(p)
tlp.RowStyles.Add(New RowStyle(SizeType.Percent, 100))
tlp.RowStyles.Add(New RowStyle(SizeType.Absolute, 22))
tlp.SetColumn(tb, 0)
tlp.SetRow(tb, 0)
tlp.SetColumn(p, 0)
tlp.SetRow(p, 1)
End Sub
End Class
That's certainly not wrong but it seems a bit of overkill to use a TableLayoutPanel for one control when you could achieve the same effect simply by setting properties of that control.
-
Mar 25th, 2018, 06:23 PM
#6
Re: Text Box docked to top of form, do I have to manually adjust the height?
 Originally Posted by jmcilhinney
That's certainly not wrong but it seems a bit of overkill to use a TableLayoutPanel for one control when you could achieve the same effect simply by setting properties of that control.
It seems a lot, but to guarantee a 22px border below the textbox at any height, it's the most effective method i know...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 25th, 2018, 06:34 PM
#7
Re: Text Box docked to top of form, do I have to manually adjust the height?
 Originally Posted by .paul.
It seems a lot, but to guarantee a 22px border below the textbox at any height, it's the most effective method i know...
It seems to me that the simplest method to do that is to set a 22 pixel margin in the designer and set the Anchor to include Bottom. You have me wondering now whether there's something I'm missing that would cause that to fail.
-
Mar 25th, 2018, 06:40 PM
#8
Re: Text Box docked to top of form, do I have to manually adjust the height?
 Originally Posted by jmcilhinney
It seems to me that the simplest method to do that is to set a 22 pixel margin in the designer and set the Anchor to include Bottom. You have me wondering now whether there's something I'm missing that would cause that to fail.
Well, to quote YOU Have you tried it??? There's no substitute for testing, and maybe that is one possible solution...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 25th, 2018, 07:09 PM
#9
Re: Text Box docked to top of form, do I have to manually adjust the height?
 Originally Posted by .paul.
Well, to quote YOU�� Have you tried it??? There's no substitute for testing, and maybe that is one possible solution...
Yes, I have tried it. Thanks for asking. It seemed the obvious solution and it worked in my testing, but that doesn't mean that there can't be some scenario that I didn't consider in which it wouldn't work. That seems to be the only reason to bring in another control and set many more properties to me. My point is that, if you're aware of a reason that that wouldn't be a good solution then I'm interested to hear it. If you aren't aware of an issue with it then I'd suggest that it is the best by virtue of being less complex than the alternative without losing anything in effectiveness or performance.
-
Mar 25th, 2018, 07:23 PM
#10
Re: Text Box docked to top of form, do I have to manually adjust the height?
As the original question is a little vague, i don't know what will occupy the 22px, but it could be a button, checkbox, or a label...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 25th, 2018, 07:38 PM
#11
Re: Text Box docked to top of form, do I have to manually adjust the height?
 Originally Posted by .paul.
As the original question is a little vague, i don't know what will occupy the 22px, but it could be a button, checkbox, or a label...
I'm not sure of the relevance. Anything occupying the space below the TextBox can be Anchored to the Bottom but not the Top. Anyway, the OP has their options.
-
Mar 25th, 2018, 08:37 PM
#12
Thread Starter
Fanatic Member
Re: Text Box docked to top of form, do I have to manually adjust the height?
Totally appreciate the passion you guys bring to it. I was a long-time VB6'er, just now getting back into it with VB 2017 and I'm a bit perplexed by some of it. I gave you the heights set before run-time: Form height (568) and the TextBox height (546). It doesn't seem to correlate with what the IDE shows:
-
Mar 25th, 2018, 08:40 PM
#13
Thread Starter
Fanatic Member
Re: Text Box docked to top of form, do I have to manually adjust the height?
Obviously, that's a lot more than a 22 pixel difference between TextBox and Form, but that's the same margin I would like to keep at the bottom regardless of the user expanding the height of the form or contracting it.
-
Mar 25th, 2018, 09:02 PM
#14
Re: Text Box docked to top of form, do I have to manually adjust the height?
The Height of the form includes the non-client area and that may change depending on OS and various other factors. It's the ClientSize.Height that represents the height of the client area and that should not change. If you wanted to do it in code then it could look like this:
vb.net Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.TextBox1.SetBounds(0, 0, Me.ClientSize.Width, Me.ClientSize.Height - 22) End Sub
You can obviously set the Location and Size in the designer though, and you would also set the Anchor to all four sides in the designer.
-
Mar 25th, 2018, 09:24 PM
#15
Thread Starter
Fanatic Member
Re: Text Box docked to top of form, do I have to manually adjust the height?
You can set it with one line in the From Load? My how things have progressed! Used to be I had to write code for every control in the Form Resize event.
Very cool. Thanks for the help!
-
Mar 25th, 2018, 09:37 PM
#16
Re: Text Box docked to top of form, do I have to manually adjust the height?
That code I provided will move and resize the TextBox so that it fills the entire form other than a 22-pixel strip at the bottom. It's setting the Anchor property that makes the control resize to continue to fill all but that 22-pixel strip when the form resizes.
-
Mar 25th, 2018, 09:40 PM
#17
Thread Starter
Fanatic Member
Re: Text Box docked to top of form, do I have to manually adjust the height?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|