Solved

Controls does not position correctly

Posted on 2013-11-04
3
274 Views
Last Modified: 2013-11-05
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
Comment
Question by:daghoff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 39622122
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
 

Author Comment

by:daghoff
ID: 39624655
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 39624734
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question