Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
---I want to draw circle inside rectangle but there is one problem I can't solve.
---I'm new at programing so I need help.

this is the main code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace DrawCircleRectangle
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //pen is used for border color
        Pen red = new Pen(Color.Red);
        Pen green = new Pen(Color.Green);

        //To fill the shape
        System.Drawing.SolidBrush fillRed = new System.Drawing.SolidBrush(Color.Red);
        System.Drawing.SolidBrush fillYelloq = new System.Drawing.SolidBrush(Color.Yellow);

        Rectangle rectangle = new Rectangle(50, 50, 220, 90);
        Rectangle circle = new Rectangle(50,50,220,90); 
       

        public void Form1_Load(object sender, PrintPageEventArgs e)
        {
            using (Graphics g = e.Graphics)
            {
                g.DrawRectangle(red, rectangle);    //draw rectangle
                g.DrawEllipse(green, circle);       //draw circle
            }
        }
    }
}




---But the problem is Form1.Designer 


using System;

namespace DrawCircleRectangle
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// 
        /// <param name="disposing" />true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Name = "Form1";
            this.Text = "Form1";
            NewMethod();
            this.ResumeLayout(false);

        }

        private void NewMethod()
        {
            Form1 form11 = this;
            this.Load += new EventHandler(form11.Form1_Load);        }

        #endregion
    }
}

---I have bolded where the error occurred 
-- No overload for 'Form1_Load' matches delegate 'EventHandler'

What I have tried:

I have tried some solutions but they didn't work
Posted
Updated 3-Sep-19 2:30am

1 solution

C#
public void Form1_Load(object sender, PrintPageEventArgs e)

You're trying to add this method as event handler for Load, but that event requires the second argument to be of the type EventArgs, not PrintPageEventArgs.

PrintPageEventArgs is related to the PrintPageEventHandler, which has to do with PrintDocument[^] - if printing is what you're trying to achieve, you should explore that class. If you just want to draw on the form itself, you should use the Paint event[^].
 
Share this answer
 
Comments
NikolV 3-Sep-19 8:42am    
My first code was with EventArgs but then I had different error with method Graphics

( C# 'EventArgs' does not contain a definition for 'Graphics' and no accessible extension method 'Graphics' accepting a first argument of type 'EventArgs' could be found )

That's why I put PrintPageEventArgs
Thomas Daniels 3-Sep-19 8:43am    
Yes, Load does not pass any Graphics. The Paint event, however, does.
NikolV 3-Sep-19 8:53am    
So just to replace it with Paint event right?
Thomas Daniels 3-Sep-19 8:54am    
Yes, and replace PrintPageEventArgs with PaintEventArgs
BillWoodruff 3-Sep-19 20:16pm    
+5

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