views:

41

answers:

2

Any help? Here's my code:

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

namespace Client
{
    public partial class MainChat : Form
    {
        bool ShouldPaint = false;

        public MainChat()
        {
            InitializeComponent();
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(400, 400);

            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
            this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (ShouldPaint)
            {
                Graphics graphics = CreateGraphics();
                graphics.FillEllipse(new SolidBrush(Color.Black), e.X, e.Y, 10, 10);
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            ShouldPaint = false;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            ShouldPaint = true;
        }
    }
}
+2  A: 

fix the line

Graphics graphics = CreateGraphics();

to

Graphics graphics = pictureBox1.CreateGraphics();

The way you had it gets the graphics of the form like saying this.CreateGraphics() that is why it draws on the form. All controls have a CreatGraphics() method for custom painting.

Samuel
Thanks! Worked like a charm! I'll select this as solved when 10 minutes are up. There's a wait timer.
Serg
A: 

Your code is asking to paint the form and not the control.

The graphics object that you are getting is of the form and not control.

Nayan