Hi,

I'm new to C# and trying to develop this program that allows the user to draw freeform with the mouse on a panel. There are 2 groupboxes. One is called Color and it has 4 radio buttons: red, blue, green and black. The second is Size and has 3 radio buttons: small, medium, and large. The user should be able to select a color and a brush size and draw on the panel. Here is what I have so far. Any help will be greatly appreciated.

public partial class drawingPanelForm : Form
{
bool shouldPaint = false; // determines whether to paint

private Color m_Color;
private Size m_Size;
public void FillEllipse(Brush brush,int x,int y,int width,int height);

Brush brush;
int x;
int y;
int width;
int height;

//default constructor
public drawingPanelForm()
{
InitializeComponent();
}
//should paint when mouse button is pressed down
private void drawingPanelForm_MouseDown(object sender, MouseEventArgs e)
{
//indicate that user is dragging the mouse
shouldPaint = true;
}
//stop painting when mouse button is released
private void drawingPanelForm_MouseUp(object sender, MouseEventArgs e)
{
//indicate that user released the mouse button
shouldPaint = false;
}
 
private void redRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Color = Color.Red;
 
}
private void blueRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Color = Color.Blue;
}
private void greenRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Color = Color.Green;
}
private void blackRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Color = Color.Black;
}
private void smallRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Size = new Size(4,4);
}
private void mediumRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Size = new Size(6, 6);
}
private void largeRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Size = new Size(12, 12);
}
private void drawPanel_MouseMove(object sender, MouseEventArgs e)
{
if (shouldPaint)//check if mouse button is being pressed
{
//draw where the mouse pointer is present
Graphics graphics = CreateGraphics();
graphics.FillEllipse(new SolidBrush(m_Color), e.X, e.Y, m_Width, m_Heigth);
graphics.Dispose();
}
 
 
 
}
}
}

Do you have to use a Panel? If not, then the pictureBox control is more suited for that type of use.

Edit:: Also where exactly are you setting 'm_width' and 'm_heigth' [sic]

mostly because you spelled height wrong but also if you haven't defined them they won't exist hence it has no size.

if you work with MS Visual Studio, try to use IntelliSense to get the right properties.
when using CreateGraphics(), you might also want to use

this.Invalidate();

to call the Paint-event anew.

hope this helped
pygmalion

I still can't get it to paint in the panel. Here is my adjusted code:

public partial class drawingPanelForm : Form
{
bool shouldPaint = false; // determines whether to paint
private Color m_Color;
public int m_Width;
public int m_Height;


//default constructor
public drawingPanelForm()
{
InitializeComponent();
}
//should paint when mouse button is pressed down
private void drawingPanelForm_MouseDown(object sender, MouseEventArgs e)
{
//indicate that user is dragging the mouse
shouldPaint = true;
}
//stop painting when mouse button is released
private void drawingPanelForm_MouseUp(object sender, MouseEventArgs e)
{
//indicate that user released the mouse button
shouldPaint = false;
}
 
private void redRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Color = Color.Red;
 
}
private void blueRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Color = Color.Blue;
}
private void greenRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Color = Color.Green;
}
private void blackRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Color = Color.Black;
}
private void smallRadioButton_CheckedChanged(object sender, EventArgs e)
{


m_Width = 4;
m_Height = 4;
}
private void mediumRadioButton_CheckedChanged(object sender, EventArgs e)
{
m_Width = 8;
m_Height = 8;
}
private void largeRadioButton_CheckedChanged(object sender, EventArgs e)
{
m_Width = 12;
m_Height = 12;
}
private void drawPanel_MouseMove(object sender, MouseEventArgs e)
{
if (shouldPaint)//check if mouse button is being pressed
{
//draw where the mouse pointer is present
Graphics graphics = CreateGraphics();
graphics.FillEllipse(new SolidBrush(m_Color), e.X, e.Y, m_Width, m_Height);
graphics.Dispose();
}

}
 

}
}

I'm trying to use a panel. You are right. I have adjusted my code. I'm not sure if I did the m_Width and m_Height right. Here's my code - it still will not draw anything.

public partial class drawingPanelForm : Form
{
bool shouldPaint = false; // determines whether to paint
private Color m_Color;
public int m_Width;
public int m_Height;


//default constructor
public drawingPanelForm()
{
InitializeComponent();
}
//should paint when mouse button is pressed down
private void drawingPanelForm_MouseDown(object sender, MouseEventArgs e)
{
//indicate that user is dragging the mouse
shouldPaint = true;
}
//stop painting when mouse button is released
private void drawingPanelForm_MouseUp(object sender, MouseEventArgs e)
{
//indicate that user released the mouse button
shouldPaint = false;
}
 
private void redRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Color = Color.Red;
}
private void blueRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Color = Color.Blue;
}
private void greenRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Color = Color.Green;
}
private void blackRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Color = Color.Black;
}
private void smallRadioButton_CheckedChanged(object sender, EventArgs e)
{


m_Width = 4;
m_Height = 4;
}
private void mediumRadioButton_CheckedChanged(object sender, EventArgs e)
{
m_Width = 8;
m_Height = 8;
}
private void largeRadioButton_CheckedChanged(object sender, EventArgs e)
{
m_Width = 12;
m_Height = 12;
}
private void drawPanel_MouseMove(object sender, MouseEventArgs e)
{
if (shouldPaint)//check if mouse button is being pressed
{
//draw where the mouse pointer is present
Graphics graphics = CreateGraphics();
graphics.FillEllipse(new SolidBrush(m_Color), e.X, e.Y, m_Width, m_Height);
graphics.Dispose();
}

}


}
}

Do you have to use a Panel? If not, then the pictureBox control is more suited for that type of use.

Edit:: Also where exactly are you setting 'm_width' and 'm_heigth' [sic]

mostly because you spelled height wrong but also if you haven't defined them they won't exist hence it has no size.

See if this code snippet helps you to get some basic drawing done, it will then rule out whether your code is all wrong. And you can implement everything else from there:

http://www.daniweb.com/code/snippet647.html

I really needed the answer to this question too and was banging my head against the wall. Since this comes up at the top of the Google search for draw in Panel, I will do my karmic good deed and say what the problem is to help other poor souls like me. You'll notice at the end of the script there is the line

Graphics graphics = CreateGraphics();
Even though this is within the event for the drawPanel_mousemove if you don't specify in CreateGraphics(), C# will use the form. Here is how I did it.

Graphics graphics = drawPanel.CreateGraphics();

Hope this helps some peeps out. :)

Thanks for posting the solution. It was a great help to me.

Hi,

I'm new to C# and trying to develop this program that allows the user to draw freeform with the mouse on a panel. There are 2 groupboxes. One is called Color and it has 4 radio buttons: red, blue, green and black. The second is Size and has 3 radio buttons: small, medium, and large. The user should be able to select a color and a brush size and draw on the panel. Here is what I have so far. Any help will be greatly appreciated.

public partial class drawingPanelForm : Form
{
bool shouldPaint = false; // determines whether to paint

private Color m_Color;
private Size m_Size;
public void FillEllipse(Brush brush,int x,int y,int width,int height);

Brush brush;
int x;
int y;
int width;
int height;

//default constructor
public drawingPanelForm()
{
InitializeComponent();
}
//should paint when mouse button is pressed down
private void drawingPanelForm_MouseDown(object sender, MouseEventArgs e)
{
//indicate that user is dragging the mouse
shouldPaint = true;
}
//stop painting when mouse button is released
private void drawingPanelForm_MouseUp(object sender, MouseEventArgs e)
{
//indicate that user released the mouse button
shouldPaint = false;
}
 
private void redRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Color = Color.Red;
 
}
private void blueRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Color = Color.Blue;
}
private void greenRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Color = Color.Green;
}
private void blackRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Color = Color.Black;
}
private void smallRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Size = new Size(4,4);
}
private void mediumRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Size = new Size(6, 6);
}
private void largeRadioButton_CheckedChanged(object sender, EventArgs e)
{

m_Size = new Size(12, 12);
}
private void drawPanel_MouseMove(object sender, MouseEventArgs e)
{
if (shouldPaint)//check if mouse button is being pressed
{
//draw where the mouse pointer is present
Graphics graphics = CreateGraphics();
graphics.FillEllipse(new SolidBrush(m_Color), e.X, e.Y, m_Width, m_Heigth);
graphics.Dispose();
}
 
 

}
}
}

I don't know if that's the problem but I have a similar problem. Try this:

panelDrawing.BackColor = Color.Transparent;

There is a chance that all parts of your drawings are behind the panel and they are invisible because the panel hide them.

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.