0

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

Héctor M.
  • 2,302
  • 4
  • 17
  • 35
  • You need to create an [MCVE](https://stackoverflow.com/help/mcve). Currently, the relationship between controls (Label and NotificationsBell) is unclear. – default locale Apr 03 '18 at 07:07
  • Suggest simply adding a container (e.g. panel) for everything related to this icon, then add the bell and label to that panel and set horizontal alignment of both to center – LordWilmore Apr 03 '18 at 07:10
  • Did you try *inspecting* SO's label to see what is used there? Hint: there's `+100` then just look the classes used and properties related – Rafalon Apr 03 '18 at 07:10
  • You forget to add `Left` of the bell to the result. – Reza Aghaei Apr 03 '18 at 07:13
  • @LordWilmore I have thought of a container panel, but if the width of the label exceeds the size of the panel, then it is cut with the limits of that panel – Héctor M. Apr 03 '18 at 07:14
  • @RezaAghaei No, because the bell is in a fixed position and the label is the one who must line up centered on the bell – Héctor M. Apr 03 '18 at 07:17
  • Probably you didn't get what I mean. See the answer.... – Reza Aghaei Apr 03 '18 at 07:18

1 Answers1

1

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;
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398