?
Solved

Controls does not position correctly

Posted on 2013-11-04
3
Medium Priority
?
276 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

719 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