Link to home
Start Free TrialLog in
Avatar of daghoff
daghoff

asked on

Controls does not position correctly

I have a very annoying problem with controls (buttons, textboxes etc.) that are not positioned correctly inside a panel.

It only occurs if the panel is too small to show all the controls and the scrollbars appears automatically.

It looks as if the location of the controls somehow becomes relative to the visible part of the panel

I just can’t understand the logic here. Has anyone else experienced the same and how was it solved?

This annoying behaviour creates a lot of problems in the program I am currently working on. This program is quite large and complex so for this question I have created and attached a small project that demonstrates the problem. When it starts it creates a label far down in the panel forcing it to create the scroll bars. Then 2 textboxes are created and positioned in the panel with the Y coordinate = 10. They are kept invisible. Start the project and press button 1 and the first textbox becomes visible in the correct position. Then scroll down and pres button 2. Now textbox 2 becomes visible in an incorrect position, but correct if you wanted it to be positioned relative to the visible part of the panel. When scrolled up and down the 2nd. textbox preserves the incorrect position. Making it invisible and then visible again does not change its position.

What do I need to do to make sure controls like these textboxes in this example always are positioned absolute within the panel regardless of how the scrollbars are or if there are scrollbars at all?

Atached are two files. The code and the project itself. Please note that all files in the project have an extra txt extension since most of the files had extentions that prevented them to be uploaded.
Code.txt
EE-Demo.zip
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of daghoff
daghoff

ASKER

Thanks for a very simple and effective solution. It works great, but please let me ask 2 follow up question.

What is the purpose of the “tbox_2.Anchor = AnchorStyles.Left Or AnchorStyles.Top” codeline you have added to my example? In what situastion will it help preventing the controls from showing up in an incorect position?

In my program I sometimes need to move a control. It may have been visible or not before move. When I do it like this:
tbox_1.Location = New Point(tbox_1.Location.X + 100, tbox_1.Location.Y + 100)
it works fine, but when I do it like this:
tbox_1.Location = New Point(200, 110)
it once again position relativly to the visible part of the scrolled pannel. Is there a way to solve this problem as well? Sometimes it is to prefere to move without having to refere to the controls current position.
Setting Anchor() tells the control to maintain its distance to whatever side(s) you anchor it to.  With Top and Left set, the control will stick to the top and left edge of the container when it is resized.  This is the default behavior in Visual Studio, and what most people expect a control to do when the Location() property is set.

Without anchoring, the control sort of "floats", maintaining a position relative to the container but with respect to a sort of "percentage" of where it first started.  If it was a 1/4 of the way from the left, then it would still a be 1/4 of the way from the left when the container is resized.

For the second question, I think referencing the current location somehow forces the container to run its layout engine.  I'm not sure if there are other ways to fix the problem, other than the one I found where you initially set its visibility to true.