Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

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
0
daghoff
Asked:
daghoff
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Leave the Visibility() set to True when you add it, but the immediately toggle it back to False:
        tbox_2.Location = New Point(300, 10)
        tbox_2.Text = "This is tbox_2"
        tbox_2.Visible = True
        tbox_2.Anchor = AnchorStyles.Left Or AnchorStyles.Top
        pnl_Main.Controls.Add(tbox_2)
        tbox_2.Visible = False

Open in new window


Now it will be in the correct position when you make it visible.
0
 
daghoffAuthor Commented:
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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now