Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am not a professional coder. This code is working fine as it is now.
----------
But I want to add an event to this class, that will look similar to a clasic button event, like this:
private void BasicButon_Click(object sender, EventArgs e) { ... }
and fire it up from Form1.
I have no idea how to implement it.
Please modify my code and inject in it this event functionality, then post it as an answer. Thank you.

What I have tried:

C#
//custom class:
    class BasicButon
    {

        int size = 2;
        public int grf = 1;

        public void Draw(int x, int y,  PaintEventArgs e)
        {
            Point[] triunghi1 = { new Point(x + 12 * size, y + 3 * size), new Point(x + 12 * size, y - 3 * size), new Point(x + 17 * size, y) };

            switch (grf)
            {
                case 1:
                    e.Graphics.FillPolygon(new SolidBrush(Color.Red), triunghi1);//red
                    break;
                case 2:
                    e.Graphics.FillPolygon(new SolidBrush(Color.Blue), triunghi1);//blue
                    break;
            }
        }

    }


//and in Form1:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            bb.grf = 1;
        }

        BasicButon bb = new BasicButon();

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            bb.Draw(20, 20, e);
        }

    }
Posted
Updated 1-Dec-19 9:33am

1 solution

Instead of re-inventing the wheel, it might be easier to use a standard Button Control and override the OnPaint() method, see examples here:
Creating a Button at Run-time in C#[^]
Creating Custom Controls In C#[^]
Round Button in C#[^]

Oh, I almost forgot this CodeProject RoundedButton Control article that I improved. Getting the borders to be smooth can be tricky:
RoundedButton Control - Demystifying DrawArc[^]
 
Share this answer
 
v3
Comments
_Q12_ 1-Dec-19 17:11pm    
Thank you very much mister Rick. I will try the code from your links and i will update here what i found.

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