Hi,I'm trying to make my own simple paint application but I'm having trouble with the
"openfiledialog and paint" thing. The code below draws lines when the mouse is down and moving. The problem is whenever I press the button that opens the dialogbox everything I painted on the form disappears except the button. The white rectangle is supposed to be drawn once by the form and written by a mouse like a pen writing on paper. Do you guys have any idea how I can solve this problem??

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 Doodle
{
    public partial class Form1 : Form
    {

        public Graphics g;
        SolidBrush padBrusher = new SolidBrush(Color.White);
        SolidBrush brusher = new SolidBrush(Color.Red);
        Pen blackPen=new Pen(Color.Black,.1f);
        Rectangle pad=new Rectangle(10,10,200,200);
        public bool mdown=false;
        Point pOne, pTwo;
        bool allowPaint = true;
     
     
        
      
    

        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

        private void mouseDown(object sender, MouseEventArgs e)
        {
            mdown = true;
            pOne =this.PointToClient(new Point(MousePosition.X, MousePosition.Y));
            
        }

        private void mouseMove(object sender, MouseEventArgs e)
        {

            if (mdown == true)
            {
                pTwo = pOne;
                pOne = this.PointToClient(new Point(MousePosition.X, MousePosition.Y));
                g.DrawLine(blackPen, pOne, pTwo);

 
                
            }
        
        }

        private void painter(object sender, PaintEventArgs e)
        {


            if (allowPaint == true)
            {
                g = this.CreateGraphics();
                g.FillRectangle(padBrusher, pad);
                g.DrawRectangle(blackPen, pad);
                allowPaint = false;
            }          
        }

        private void mouseUp(object sender, MouseEventArgs e)
        {
            mdown = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }


    }
}

Why do you create a Graphics object of your own?
You get one for free out of the PaintEventArgs e parameter from your pzinter event handler.(line 62)

Why do you create a Graphics object of your own?
You get one for free out of the PaintEventArgs e parameter from your pzinter event handler.(line 62)

Is there an alternative way for that??? could you show me the code??? I'm an amateur with paint events. I tried to put the code inside the painter to Form1_Load but it won't load the rectangle when I run the program so I put it in the painter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.