User control Anchor property issue
Asked Answered
C

4

6

Context :

I created an User Control. For some reason, I want to use this control in different size. To keep the initial "Template" of my User Control when re-sizing, I use the property Anchor on my different element inside the control.

So when I create my control at design time, it is possible to me to hand re-size the control and keep the original "Template" of it.

When the control is created, it look like this :

Initial

And after re-size :

Re-Sized

As you can see, the property Anchor work well.

  • The label and the picture stay in the middle.
  • The "?" stay to the left corner.

The problem :

The problem I have is, when the control is reloaded, created with a different size as the initial one, all the elements inside return to their initial position :

Problem

I don't know if this is the better way to do what I try to achieve. Keep in mind that I add and re-sizing the control's during the design time.

Thank you.

EDIT :

I think my problem is caused by the designer. Ex : I add my control in the designer, I re-size it, I run the solution. All is working good. But when I go to the code of the page, and then, return to the designer, the element inside the control returned to their initial position.

EDIT 2 :

Ok I have found a solution, I simply moved all the element of the User control inside a Panel. For some reason that I can't explain, it work perfectly. The control's stay at the same location.

Cryptozoite answered 30/5, 2013 at 18:3 Comment(6)
You could use the ResizeEnd Event to apply the same properties they used to have in ResizeBegin. When you say reloaded, is it that the control gets a complete new instance ? If so, it should always call InitializeComponent which apply the properties defined in the designer.Warty
Im not really sure what to try, because my problem seem to be cause in the designer. Ex : I add my control in my designer, I re-size it, I run the solution. All is working good. But when I go to the code of the page, and then, return to the designer, the element inside the control return to their initial position...Misunderstood
This is too vague. You'll need to explain how exactly this control got "re-loaded" but no longer using its original design size. And why the normal PerformLayout() call that's issued after controls are loaded is no longer made.Type
Use the debugger to verify the value of Anchor for you control after it is reloaded. Also, the Dock property overrides the Anchor property, so you have to verify this as well.Warty
Ok, I am looking at this now, thank you.Misunderstood
As you can see I found a solution to my problem, thank you for your help.Misunderstood
C
5

The solution is ta add a Panel to the User Control and dock it to "Fill", then place the element inside of this panel. For some reason that I can't explain, the designer keep the location of the re-sized control's elements.

Cryptozoite answered 30/5, 2013 at 18:37 Comment(1)
I answered my question to help anyone who will have the same problem that I had. Please, if you can give a better solution, don't hesitate to edit my answer.Misunderstood
C
3

The anchoring, docking and auto-sizing of a UserControl seem to be terribly confusing. I found UserControl does not auto resize with the Form which suggests that you set the AutoSize property to False, which I did, and it still didn't correct my problem. But when I tried your solution, I also noticed there are two copies of the AutoSize property! I had set the AutoSize in the UserControl designer to False, but the Form designer where the UserControl instance was added also had an AutoSize on the instance, and that one had a different value (it was still True). When I set that to False also, then everything worked (with the panel in place). Then I removed the panel you suggested, and everything still worked. So I guess the trick is to make sure you check all the properties of the UserControl in the UserControl designer and in the form designer where the control is used. Then you shouldn't need a panel.

Currin answered 12/5, 2014 at 17:20 Comment(0)
A
1

I've had similar problem in VS2015 project, and unfortunately - none of your answers helped. Clean and working solution was found here, in Jignesh Thakker answer. For quicker navigation here it is how it was done in my project (c++/cli, not c#, but idea is the same):

System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
/* some code */

myUserControl = gcnew MyUserControl();
myUserControl->Dock = DockStyle::Fill;

tabPage1->Dock = DockStyle::Fill;
tabPage1->Controls->Add(myUserControl);

/* some code */

}
Arin answered 25/11, 2017 at 11:19 Comment(0)
L
0

Set the Localizable property of the parent form at VS designer to false. This solves the problem at design time. (Save, close and reopen the form after switching the property)

If you need a localized application switch the Localizable property to true after finish up working at the layout and don't care about the wired representation in the VS designer. At run time it's shown correctly.

Tested in VS2013

Loredo answered 28/1, 2015 at 16:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.