Click here to Skip to main content
15,849,907 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everybody,

I am new to this forum, and new to C# programming as well.

Right now i am working on a project, and facing some problem.

The default size of my windows form is 800x600, and it is unlocked to maximize. Now i have placed a label in the form, and i want to be at the center, and even if user maximize the window, the label must be at the center with respect to the new size.

Please help me with this.

Thanks and Regards

PS: more to come :)

use TableLayoutPanel[^] and position your controls within the cells of the table, which will care on resizing

use Anchor/Docking property of controls for correct relative alignment.
Share this answer
CA50 9-Mar-12 6:15am    
@pashad, can you be please post the syntex or code
lukeer 9-Mar-12 6:40am    
There's no syntax for that.
You do it via clicking, dragging, entering values in the Visual Studio Designer.
Prasad_Kulkarni 9-Mar-12 6:28am    
It is just a simple property of control (label or button) which you have to set from property window, no specific code is there for that.
Go to the link provided, & read 7th property you will get details.
If not then let me know.
Prasad_Kulkarni 9-Mar-12 6:30am    
// Add a button to a form and set some of its common properties.
private void AddMyButton()
// Create a button and add it to the form.
Button button1 = new Button();

// Anchor the button to the bottom right corner of the form
button1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);

// Assign a background image.
button1.BackgroundImage = imageList1.Images[0];

// Specify the layout style of the background image. Tile is the default.
button1.BackgroundImageLayout = ImageLayout.Center;

// Make the button the same size as the image.
button1.Size = button1.BackgroundImage.Size;

// Set the button's TabIndex and TabStop properties.
button1.TabIndex = 1;
button1.TabStop = true;

// Add a delegate to handle the Click event.
button1.Click += new System.EventHandler(this.button1_Click);

// Add the button to the form.
CA50 9-Mar-12 6:32am    
Got that, i have tried the anchor property, but when i resize the windows, the label, remains intact and doesn't change :(

I have also used tab control, and the anchor property works with that perfectly.
Use Html Center tag to place the label center

For ex:
<asp:label id="Label1" runat="server" text="Hello" xmlns:asp="#unknown">
Share this answer
CA50 9-Mar-12 6:15am    
@nithibs, since i am working in C#, will the html code work ??
lukeer 9-Mar-12 6:28am    
Since you are working with Windows Forms, it won't.
CA50 9-Mar-12 6:35am    
thanks, i thought so
If it's just for that one centered label,
1. Select it.
2. Go to the "Layout" button bar.
3. Select "Center horizontally" (or similar)
It's a symbol that looks like a horizontal track bar.
Now your label is centered.
4. Go to properties window
5. Change Anchor property to neither left nor right ("None" is Ok as well).
Now the label should stay in the center of its container.
Share this answer
CA50 9-Mar-12 6:52am    
but it must change at runtime, when the windows is resized !!
lukeer 9-Mar-12 7:58am    
Understood. That's just the effect the above should create.
What behaviour are you observing instead?
CA50 9-Mar-12 8:05am    
nothing, at the designer window, the label positions itself at the center, but when i resize the window (at runtime), the label doesn't change its position
lukeer 11-Mar-12 10:56am    
And what is the label's Anchor property set to?
CA50 11-Mar-12 10:59am    
top, bottom
This is the Common Problem faced with windows forms.For my project also i had faced the same problem. Started the development using the TableLayout. Then at final stage it was with above 100 TableLayout components. But it is not advised.

By Using the DataBindings Property of the control, we can solve it.
Create a Label object (objLabel)
Whenever the size of window changes calculate the percentage of change.
and update the label Width, Height, Location with the percentage of change.

Then clear the DataBindings of your label.


And add the Databindings of the created label to your label.

yourLableCtrl.DataBindings.Add("Width", objLabel, "Width");
yourLableCtrl.DataBindings.Add("Height", objLabel, "Height");
yourLableCtrl.DataBindings.Add("Location", objLabel, "Location");

I had tested for all the Resolutions, it worked also.
Share this answer
Thanks to all who have read this, i have found a solution for this problem, and it works for me perfectly :)

1. Insert the label within the 'panel' container
2. Next change the anchor property of the panel
3. In case of any problem, dock the panel to TOP

Cheers :)
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900