haughtonomous
haughtonomous

Reputation: 4850

How to programmatically move a control defined with the Windows Form Designer

I think this is an odd one. I have a screen (Windows Forms) created using the Forms Designer. I now want to programmatically change the width of one of the controls, ie at runtime, according to some or other criterion.

My problem is that I can't seem to do this. My approach is to assign a new value to the Width property somewhere after the form class InitializeComponent() method has been executed (say at the end of the form constructor). A watch shows me the control property has been changed, but the subsequently displayed screen ignores the new value and uses the value assigned in InitializeComponent().

Am I trying to do something impossible? Is there some voodoo in the background that blocks this for a control created by the Designer, or am I just going about it the wrong way?

Upvotes: 0

Views: 680

Answers (4)

haughtonomous
haughtonomous

Reputation: 4850

Have you tried handling an event for when the Form is loaded and in view? Something like: this.Loaded += MyLoadedEventHandler; In the method MyLoadEventHandler adjust the width. – Davio 2 hours ago

That did the trick.

Upvotes: 0

ChruS
ChruS

Reputation: 3747

Have you tried Invalidate method?

Upvotes: 0

Ria
Ria

Reputation: 10357

Check Control.AutoSize and Control.Anchor properties. specially:

Control.Anchor : Gets or sets the edges of the container to which a control is bound and determines how a control is resized with its parent.

Upvotes: 1

Kami
Kami

Reputation: 19447

If the control is a Label ensure that the Autosize property is set to false, otherwise .NET will resize the control to fit content.

Upvotes: 0

Related Questions