Results 1 to 17 of 17

Thread: Text Box docked to top of form, do I have to manually adjust the height?

  1. #1

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526

    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.
    ~ mikeycorn

    With over 45,000 Questions in the User Submitted Database, it's the Most Popular Quiz Creation Software on the Net:

    PEST - The Personal Exam Self-Tester

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Text Box docked to top of form, do I have to manually adjust the height?

    Quote Originally Posted by .paul. View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Text Box docked to top of form, do I have to manually adjust the height?

    Quote Originally Posted by jmcilhinney View Post
    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...

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Text Box docked to top of form, do I have to manually adjust the height?

    Quote Originally Posted by .paul. View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Text Box docked to top of form, do I have to manually adjust the height?

    Quote Originally Posted by jmcilhinney View Post
    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...

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Text Box docked to top of form, do I have to manually adjust the height?

    Quote Originally Posted by .paul. View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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...

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Text Box docked to top of form, do I have to manually adjust the height?

    Quote Originally Posted by .paul. View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526

    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:

    Name:  Screenshot_2.jpg
Views: 956
Size:  25.1 KB
    ~ mikeycorn

    With over 45,000 Questions in the User Submitted Database, it's the Most Popular Quiz Creation Software on the Net:

    PEST - The Personal Exam Self-Tester

  13. #13

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526

    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.
    ~ mikeycorn

    With over 45,000 Questions in the User Submitted Database, it's the Most Popular Quiz Creation Software on the Net:

    PEST - The Personal Exam Self-Tester

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.     Me.TextBox1.SetBounds(0, 0, Me.ClientSize.Width, Me.ClientSize.Height - 22)
    3. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526

    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!
    ~ mikeycorn

    With over 45,000 Questions in the User Submitted Database, it's the Most Popular Quiz Creation Software on the Net:

    PEST - The Personal Exam Self-Tester

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526

    Re: Text Box docked to top of form, do I have to manually adjust the height?

    Perfect!
    ~ mikeycorn

    With over 45,000 Questions in the User Submitted Database, it's the Most Popular Quiz Creation Software on the Net:

    PEST - The Personal Exam Self-Tester

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
  •  



Click Here to Expand Forum to Full Width