0

I am attempting to have a simple program that will paint a rectangle or a circle dependant on the radio button selected. The drawing will change once the 'Draw' button is pressed. However I seem to be having an issue with the button, when I hover over the button the OnPaint method is triggered and you end up with both drawings on the form. Here's the code:

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

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Pen myPen = new Pen(Color.Blue, 5);
            Graphics formGraphics = this.CreateGraphics();

            if (RectangleRadio.Checked)
                formGraphics.DrawRectangle(myPen, new Rectangle(10, 10, 300, 200));

            if (CircleRadio.Checked)
                formGraphics.DrawEllipse(myPen, 10, 10, 300, 300);

            myPen.Dispose();
            formGraphics.Dispose();
        }

        private void DrawButton_Click(object sender, EventArgs e)
        {

            this.Invalidate();
        }
Its
  • 5
  • 2

1 Answers1

0

Just change the button FlatStyle to Flat

DrawButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

This stops UI invalidation during hover

hessam hedieh
  • 780
  • 5
  • 18