0

I need to add a badge to a tab control at the top right corner. I have tried this and came up with this class

public class BadgeUtil
{
    private static List<Control> controls = new List<Control>();

    static public bool AddBadgeTo(Control ctl, string Text,int y,int x)
    {
        if (controls.Contains(ctl)) return false;

        Badge badge = new Badge();
       // badge.AutoSize = true;
        badge.Size = new Size(20, 20);
        badge.Text = Text;
        badge.BackColor = Color.FromArgb(255,128,64);
        controls.Add(ctl);
        ctl.Controls.Add(badge);
        SetPosition(badge, ctl,x,y);

        return true;
    }
    static private void SetPosition(Badge badge, Control ctl,int x,int y)
    {
        badge.Location = new Point(x,
                                   y);
    }
    class Badge : Label
    {
        Color BackColor = Color.SkyBlue;
        Color ForeColor = Color.White;
        Font font = new Font("Sans Serif", 9f);

        public Action<Control> ClickEvent;

        public Badge() { }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillEllipse(new SolidBrush(Color.FromArgb(255,0,0)), 
                this.ClientRectangle);
            e.Graphics.DrawString(Text, font, new SolidBrush(ForeColor), 3, 1);
        }

        protected override void OnClick(EventArgs e)
        {
            ClickEvent(this);
        }
    }
}

and used this way

BadgeUtil.AddBadgeTo(tabPage3, "3",0,120);

which is resulting to this un desired output. I would like my badge to be position on the blue circle of the tab control as shown in this image not where the badge is positioned.

output

IndieGameDev
  • 2,905
  • 3
  • 16
  • 29
Morgan Denis
  • 111
  • 1
  • 15
  • That's not that simple - you are adding Badge to `TabPage`, but you need to draw it over `TabControl`. Check this link: https://stackoverflow.com/a/36900582/2109769. They added close button `Tab`, if you remove click logic and change way it's drawn you will have your badge. – Quercus Nov 18 '20 at 09:53
  • @Quercus I am unable to relate your suggestion with my question – Morgan Denis Nov 18 '20 at 10:13
  • What you are trying to do, is to add badge to `TabPage`. `TabPage` is an empty area below tabs line. Control added there can't paint itself over tabs line. I recommend to override how tabs line is painted in `TabsControl` and attached the link to similar problem – Quercus Nov 18 '20 at 11:12

0 Answers0