0

I was trying to create a round button. For this, we need to create a class, here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace CircularButton
{
    internal class CircularButton :Button
    {
        protected override void OnPaint(PaintEventArgs pevent)
        {
            GraphicsPath g = new GraphicsPath();
            g.AddEllipse(0, 0, ClientSize.Width, ClientSize.Height);
            this.Region = new System.Drawing.Region(g);
            base.OnPaint(pevent);
        }
    }
}

After creating and coding this class, we need to rebuild the solution, after rebuilding, we must see the new CİRCLE BUTTON tool in the TOOLBOX:

enter image description here

However, I do not get neither this circule button option nor the "applicationName Components" tab:

enter image description here

I do not have both. How can I solve this problem?

Source of the images I use is this video: https://www.youtube.com/watch?v=HG7hi9s7YhQ

Edit: If I right-click toolbox and click on "show all" ,it gives my tab which is named as "HarryPotter" and there is "CircleButton" tool in it, but I can not drag it to Forms because it is inactive(the relevant icon seems dark):

enter image description here

  • Where did you get those images if the Control is not there? Different version of VS? Other? What version of VS 2022 is in use? No exception informing that the Control is faulty and it has been removed? – Jimi Sep 26 '22 at 10:33
  • @Jimi No Exception handling issue here. I got these images from a youtube video tutorial. – Programmable Physics Sep 26 '22 at 10:41
  • just subclassing `Button` is not sufficient. The class needs attributes to tell the IDE that it is supposed to show up in the toolbox, and/or reside in a "Control Library" type project. – Cee McSharpface Sep 26 '22 at 10:47
  • All right. What kind of Project includes this custom Control? Did you add the class to a standard Windows Forms Project? Something else? Where is the Constructor? Is this all you have there? -- Change the class to `public` in the meanwhile -- Note that the Region must not be set in the OnPaint override, see here: [How to draw a shape using GraphicsPath to create the Region of a Custom Control?](https://stackoverflow.com/a/69075782/7444103) – Jimi Sep 26 '22 at 10:52
  • An example of a circular custom Control: [Translucent circular Control with text](https://stackoverflow.com/a/51435842/7444103). Of course, it doesn't need to be translucent, you can paint it the way you want -- This one doesn't use a Region, it just paints the Control's surface - which is initially removed - to create the circle. You have no issues (or, much less) with anti-aliasing this way – Jimi Sep 26 '22 at 11:16
  • @Jimi I made it public but it did not work. I opened this class on a standard windows forms project, yes, nothing different here, I did exactly what the video shows, nothing more: https://www.youtube.com/watch?v=HG7hi9s7YhQ – Programmable Physics Sep 26 '22 at 12:06
  • Read my previous comments; if you have time, take a look at the sample custom Control I've linked. Most of all, remove that Region definition from the `OnPaint()` override. Move that code to the `OnResize()` override -- In this context, make sure your custom Control uses the same `namespace` as the Project's. Add an empty Constructor: `public CircularButton() { }` -- Note that, built like that, your Button will look quite ugly – Jimi Sep 26 '22 at 12:19
  • @Jimi hey thank you for your help, but I restarted VS2022 couple of times and rebuild the project and it worked, I will put this weird solution to this question. – Programmable Physics Sep 26 '22 at 12:43
  • I'm glad your Control appeared, but what I described is still necessary – Jimi Sep 26 '22 at 14:22

1 Answers1

0

Change to public class, then rebuild (possibly Clean Solution first, then Rebuild solution) and reopen designer for a form. It should now appear in toolbox.

enter image description here

Before doing these steps, if you right-click on tool box and select show all it will show the tool tab you created and the custom tool you created with you class, but it will be inactive and appear with dark name and dark icon.