views:

462

answers:

5

I'm running into a problem trying to anchor a textbox to a form on all 4 sides. I added a textbox to a form and set the Multiline property to True and the Anchor property to Left, Right, Up, and Down so that the textbox will expand and shrink with the form at run time. I also have a few other controls above and below the textbox.

The anchoring works correctly in Visual Studio 2005 (i.e. I can resize the form and have the controls expand and shrink as expected), but when I run the project, the bottom of the textbox is extended to the bottom of the form, behind the other controls that would normally appear beneath it. This problem occurs when the form loads, before any resizing is attempted. The anchoring of the textbox is correct for the top, left, and right sides; only the bottom is malfunctioning.

Has anybody heard of this and if so, were you able to find a solution?

Thanks!

UPDATE:

Here is some of the designer code as per Greg D's request (I am only including the stuff that had to do with the textbox itself, not the other controls):

Friend WithEvents txtRecommendationText1 As System.Windows.Forms.TextBox

<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    Me.txtRecommendationText1 = New System.Windows.Forms.TextBox

    ' ...snip...

    'txtRecommendationText1

    Me.txtRecommendationText1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                Or System.Windows.Forms.AnchorStyles.Left) _
                Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.txtRecommendationText1.Location = New System.Drawing.Point(4, 127)
    Me.txtRecommendationText1.Multiline = True
    Me.txtRecommendationText1.Name = "txtRecommendationText1"
    Me.txtRecommendationText1.Size = New System.Drawing.Size(223, 149)
    Me.txtRecommendationText1.TabIndex = 10
End Sub

ANOTHER UPDATE:

The textbox I originally posted about was not inherited from a baseclass form (although it was added to a custom User Control class; I probably should have mentioned that earlier), but I recently ran into the same problem on a totally unrelated set of controls that were inherited from a baseclass form. It's easy to blame these problems on possible bugs in the .NET framework, but it's really starting to look that way to me.

+1  A: 

Hi Patrick,

what is the anchor property of the controls appearing below the textbox? is is set to bottom and right/left?

Does the form have the same size in Visual Studio ad during execution or is the window bigger then?

Regards, divo

0xA3
Yeah, the controls beneath the textbox are 3 labels and 2 comboboxes, the labels are anchored at the left and bottom and the comboboxes are anchored at the left, bottom and right sides. The form is bigger at run time than at design time.
Patrick
+2  A: 

Hi, Patrick-

Does the form snap back to the expected layout when you resize it after it's been initialized weirdly? Also, have you set a Height or MinimumHeight/MaximumHeight property for the text box?

If possible, a few snippets from the designer code might be useful. :)

One possibility that I've run into in the past is DPI. If you're running/testing your code on a machine with a different DPI setting than the machine that you're developing on, you may observe some strange things.

The anchor functionality essentially establishes a fixed distance between the edge of a control and the edge of the control's parent. Is your textbox embedded within another control (e.g., a panel) that doesn't have its anchors properly set? Right clicking on the text box in the designer should pop up a menu that lets you select any controls that exist underneath it, also.

Does your program include any custom resize logic, or does it modify the size of the textbox programmatically outside of designer-generated code? That might also result in weird behavior. I've assumed maintenance for a number of pieces of software at my organization where the original developers spent a great deal of time implementing (buggy) resize logic that I had to tear out so that I could just let the designer-generated code do the work for me.

Greg D
I have set a height but not a min/max height. I deleted the textbox and re-added it under a different name, so I don't think there is any custom resize code. Will post some designer code in a few...
Patrick
+3  A: 

Is your Form localized? Check the resource files for an entry with Textbox.Size, delete is and reset the size.
Is your Form inherited and is the Textbox on the baseform? Try setting the Textbox's access modifier to Protected or Public.
Have you implemented custom resize logic? Turn it off and see if the problem is still there.
Have you entered a Textbox.MinimumSize/MaximumSize? Remove or change the value.

It might also be a combination of these things...

Vincent Van Den Berghe
+2  A: 

The textbox I originally posted about was not inherited from a baseclass form (although it was added to a custom User Control class; I probably should have mentioned that earlier), but I recently ran into the same problem on a totally unrelated set of controls that were inherited from a baseclass form. It's easy to blame these problems on possible bugs in the .NET framework, but it's really starting to look that way to me.

Patrick
+2  A: 

It's very likely because of the 'AutoScaleMode' property being set in InitializeComponent(). Try setting it to 'None' and see if that fixes it. I've had these problem a couple of times now.