Héctor M.
Héctor M.

Reputation: 2392

Align a control based on its center

I am developing a desktop application in C # Winforms where I implement a notification system that shows the amount of accumulated notifications.

My problem is that the label on the bell does not line up in the center

To give me to understand, I want to center the label as SO does with its reputation system

enter image description here

And for this he has used this code which the SO: Centering controls within a form in .NET (Winforms)?

this.CountLabel.Left = ((this.NotificationsBell.ClientSize.Width - this.CountLabel.Width) / 2);

But instead of centering my label, this is the result

enter image description here

So, how can I align my label from its center?

Note: NotificationsBell is the PictureBox with the image of the bell and CountLabel is the label with the number 100 and blue background

Upvotes: 0

Views: 74

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125197

To align center of control1 to center of control2 vertically, when both of them are hosted in the same parent, you can use following code:

control1.Left = control2.Left + (control2.Width - control1.Width) / 2;

Upvotes: 1

Related Questions