Anchoring Problem

lothos12345

Junior Contributor
Joined
May 2, 2002
Messages
294
Location
Texas
Everytime I try to resize my textboxes based on my form, I recieve a negative occurance. I have 3 textboxes on my form, each of them directly beneath the other. I anchored the textboxes on all four sides." top, left, right, and bottom. When the form is resized the middle textbox disappears and the other 2 become huge and take up the form. I have tried several different variations of anchoring, but to no avail. Any suggestions would greatly be appreiciated. The code is in VB.NET.
 
No... That is not going to work. If you anchor to all four sides of the form, the textboxes will resize with the form, but their location (this is, .Location()) will stay the same. If the form becomes 50 pixels taller, so will the textboxes. They will expand and cover eachother (which one covers which depends on the order in which you added them to the form).

If you want them to expand only horizontally, un-anchor them from the bottom. If you do not want them to expand at all, but want them only to be centered (horizontally), anchor only from the top.

If you want to do something more complicated, for example, have each textbox take up one third of the form and have them all expand with the form without overlapping, you have to program them to do this on the Resize event (make use of the .BeginUpdate and .EndUpdate methods).
 
I do want the textboxes to resize the way you stated in the later part of your reply. Thus I will have to use code to accomplish this task. Unforunately I am not quite sure how, can you please give me an example? Any help given is greatly appreciated.
 
Resizing textboxes

This code assumes that your textboxes are called TextBox1, TextBox2, and TextBox3. Each textbox should be anchored to the top, left, and right, and not the bottom.

Visual Basic:
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles MyBase.Resize
    Static ExtraHeight As Integer = Me.Height _
        - TextBox1.Height - TextBox2.Height - TextBox3.Height
    '^ Difference between form's height and total textbox height.
    'Since ExtraHeight is static it will only be calculated once, immediately 
    'after the form is loaded and shown (which raises the resize event)
    Dim HeightLeft As Integer = Me.Height - ExtraHeight
    'HeightLeft is what the sum of the textboxes' height should be.
    '
    SuspendLayout()
    TextBox1.Height = HeightLeft \\ 3
    TextBox2.Height = TextBox1.Height
    TextBox3.Height = (HeightLeft - TextBox1.Height * 2)
    'The reason that we don't just give textbox3 the same exact height is because
    'we want it to take up the extra 1 or 2 pixels leftover as a result of roundoff error.
    TextBox2.Top = TextBox1.Bottom + 8
    TextBox3.Top = TextBox2.Bottom + 8
    ResumeLayout()
End Sub
 
Have you tried the code? All controls have a .Top, .Bottom, and .Height property, which are the only properties that this code uses. Just make the appropriate variable name changes and this will work not only on textboxes and richtextboxes but any sizable control. Buttons, Labels, Groupboxes, you name it.
 
Kinda of working

I tried the code and it does resize in every direction except bottom. For some reason it is not resizing to fill the bottom of the form. Any suggestions? And as always any help is greatly appreiciated.
 
What do you mean it is not resizing to fill the bottom? Are they not resizing vertically at all? Or only the one on the bottom?
 
Sizing Problem

None of the boxes are resizing vertically. They resize horizontially just fine but not vertically. And as always any help given is greatly appreciated. Just a reminder Option Strict in turned on as per the project requirements. Not sure if that fact as any bearing on the resizing problem.
 
Good point, however, lothos said he was using RichTextBoxes, which don't exhibit this behavior. Also, it might be worth noting that a non-multiline textbox can be resized provided that the .AutoSize property is set to false.
 
Back
Top