Solved

Controls does not position correctly

Posted on 2013-11-04
3
261 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
  • 2
3 Comments
 
LVL 85

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 85

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now