Trouble Getting System.Drawing.Graphics to work in Form
I am trying to make a C# Windows form in Visual Studio so I can draw on the form (like a basic version of Microsoft Paint). I am working through an example in a C# 2012 book. I have written the code verbatim, but when I build and run the program, I cannot actually draw anything on the form. The code compiles successfully without any errors. Can anyone see where the code can be improved so that I can successfully draw on the form?
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 paint3
public partial class Form1 : Form
bool shouldPaint = false;
public Form1() // constructor
InitializeComponent();
private void Form1_MouseDown(object sender, MouseEventArgs e)
shouldPaint = true;
private void Form1_MouseUp(object sender, MouseEventArgs e)
shouldPaint = false;
private void Form1_MouseMove(object sender, MouseEventArgs e)
if (shouldPaint)
using (Graphics graphics = CreateGraphics())
graphics.FillEllipse(new SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4);
As far as Form1 is concerned, it's simply a blank form that is created when I click "New Windows Forms Application" in Visual Studio 2012. I haven't added any buttons, text boxes, or other controls to Form1.
c# .net winforms
migrated from programmers.stackexchange.com Jun 19 '13 at 2:41
This question came from our site for professionals, academics, and students working within the systems development life cycle.
add a comment |
I am trying to make a C# Windows form in Visual Studio so I can draw on the form (like a basic version of Microsoft Paint). I am working through an example in a C# 2012 book. I have written the code verbatim, but when I build and run the program, I cannot actually draw anything on the form. The code compiles successfully without any errors. Can anyone see where the code can be improved so that I can successfully draw on the form?
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 paint3
public partial class Form1 : Form
bool shouldPaint = false;
public Form1() // constructor
InitializeComponent();
private void Form1_MouseDown(object sender, MouseEventArgs e)
shouldPaint = true;
private void Form1_MouseUp(object sender, MouseEventArgs e)
shouldPaint = false;
private void Form1_MouseMove(object sender, MouseEventArgs e)
if (shouldPaint)
using (Graphics graphics = CreateGraphics())
graphics.FillEllipse(new SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4);
As far as Form1 is concerned, it's simply a blank form that is created when I click "New Windows Forms Application" in Visual Studio 2012. I haven't added any buttons, text boxes, or other controls to Form1.
c# .net winforms
migrated from programmers.stackexchange.com Jun 19 '13 at 2:41
This question came from our site for professionals, academics, and students working within the systems development life cycle.
1
You need to put it in the Paint event
– Jeremy Thompson
Jun 19 '13 at 2:45
Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on Stack Overflow. See "Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?.
– John Saunders
Jun 19 '13 at 2:55
add a comment |
I am trying to make a C# Windows form in Visual Studio so I can draw on the form (like a basic version of Microsoft Paint). I am working through an example in a C# 2012 book. I have written the code verbatim, but when I build and run the program, I cannot actually draw anything on the form. The code compiles successfully without any errors. Can anyone see where the code can be improved so that I can successfully draw on the form?
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 paint3
public partial class Form1 : Form
bool shouldPaint = false;
public Form1() // constructor
InitializeComponent();
private void Form1_MouseDown(object sender, MouseEventArgs e)
shouldPaint = true;
private void Form1_MouseUp(object sender, MouseEventArgs e)
shouldPaint = false;
private void Form1_MouseMove(object sender, MouseEventArgs e)
if (shouldPaint)
using (Graphics graphics = CreateGraphics())
graphics.FillEllipse(new SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4);
As far as Form1 is concerned, it's simply a blank form that is created when I click "New Windows Forms Application" in Visual Studio 2012. I haven't added any buttons, text boxes, or other controls to Form1.
c# .net winforms
I am trying to make a C# Windows form in Visual Studio so I can draw on the form (like a basic version of Microsoft Paint). I am working through an example in a C# 2012 book. I have written the code verbatim, but when I build and run the program, I cannot actually draw anything on the form. The code compiles successfully without any errors. Can anyone see where the code can be improved so that I can successfully draw on the form?
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 paint3
public partial class Form1 : Form
bool shouldPaint = false;
public Form1() // constructor
InitializeComponent();
private void Form1_MouseDown(object sender, MouseEventArgs e)
shouldPaint = true;
private void Form1_MouseUp(object sender, MouseEventArgs e)
shouldPaint = false;
private void Form1_MouseMove(object sender, MouseEventArgs e)
if (shouldPaint)
using (Graphics graphics = CreateGraphics())
graphics.FillEllipse(new SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4);
As far as Form1 is concerned, it's simply a blank form that is created when I click "New Windows Forms Application" in Visual Studio 2012. I haven't added any buttons, text boxes, or other controls to Form1.
c# .net winforms
c# .net winforms
edited Nov 12 '18 at 3:21
Jeremy Thompson
39.3k13102199
39.3k13102199
asked Jun 19 '13 at 2:32
Mr. BusyMr. Busy
183
183
migrated from programmers.stackexchange.com Jun 19 '13 at 2:41
This question came from our site for professionals, academics, and students working within the systems development life cycle.
migrated from programmers.stackexchange.com Jun 19 '13 at 2:41
This question came from our site for professionals, academics, and students working within the systems development life cycle.
1
You need to put it in the Paint event
– Jeremy Thompson
Jun 19 '13 at 2:45
Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on Stack Overflow. See "Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?.
– John Saunders
Jun 19 '13 at 2:55
add a comment |
1
You need to put it in the Paint event
– Jeremy Thompson
Jun 19 '13 at 2:45
Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on Stack Overflow. See "Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?.
– John Saunders
Jun 19 '13 at 2:55
1
1
You need to put it in the Paint event
– Jeremy Thompson
Jun 19 '13 at 2:45
You need to put it in the Paint event
– Jeremy Thompson
Jun 19 '13 at 2:45
Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on Stack Overflow. See "Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?.
– John Saunders
Jun 19 '13 at 2:55
Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on Stack Overflow. See "Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?.
– John Saunders
Jun 19 '13 at 2:55
add a comment |
2 Answers
2
active
oldest
votes
private void Form1_Paint(object sender, EventArgs e)
if (shouldPaint)
using (Graphics graphics = CreateGraphics())
graphics.FillEllipse(new SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4);
I am trying to make a C# Windows form in Visual Studio so I can draw on the form (like a
basic version of Microsoft Paint). I am working through an example in
a C# 2012 book.
Alex Fr provided an excellent set of drawing tools in his DrawTools article and these tools serve as a basis for Draw Tool Redux.
You want to download this stuff its, check out a tool I recently wrote by adding to Draw Tool Redux:

The EyeDropper Colour Picker I got from http://www.codeproject.com/Articles/36540/Adobe-Eyedropper-Control
The Transparent Textbox I got from: http://www.codeproject.com/Articles/4390/AlphaBlendTextBox-A-transparent-translucent-textbo
DoesForm1_Paintsolve the OP's problem? What's all this other stuff?
– Robert Harvey♦
Jun 19 '13 at 2:57
I think they're called pearls around here, but more specifically they are the instructions to build a better program than Microsoft Paint. Looking at the code from the book, I thought OP could do with info on a C# implementation of the MFC sample DRAWCLI. Sharing your research helps everyone :)
– Jeremy Thompson
Jun 19 '13 at 3:09
add a comment |
Try the paint events for drawing. this will help you.
The link is just an example. you need to implement as per the requirements
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17182062%2ftrouble-getting-system-drawing-graphics-to-work-in-form%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
private void Form1_Paint(object sender, EventArgs e)
if (shouldPaint)
using (Graphics graphics = CreateGraphics())
graphics.FillEllipse(new SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4);
I am trying to make a C# Windows form in Visual Studio so I can draw on the form (like a
basic version of Microsoft Paint). I am working through an example in
a C# 2012 book.
Alex Fr provided an excellent set of drawing tools in his DrawTools article and these tools serve as a basis for Draw Tool Redux.
You want to download this stuff its, check out a tool I recently wrote by adding to Draw Tool Redux:

The EyeDropper Colour Picker I got from http://www.codeproject.com/Articles/36540/Adobe-Eyedropper-Control
The Transparent Textbox I got from: http://www.codeproject.com/Articles/4390/AlphaBlendTextBox-A-transparent-translucent-textbo
DoesForm1_Paintsolve the OP's problem? What's all this other stuff?
– Robert Harvey♦
Jun 19 '13 at 2:57
I think they're called pearls around here, but more specifically they are the instructions to build a better program than Microsoft Paint. Looking at the code from the book, I thought OP could do with info on a C# implementation of the MFC sample DRAWCLI. Sharing your research helps everyone :)
– Jeremy Thompson
Jun 19 '13 at 3:09
add a comment |
private void Form1_Paint(object sender, EventArgs e)
if (shouldPaint)
using (Graphics graphics = CreateGraphics())
graphics.FillEllipse(new SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4);
I am trying to make a C# Windows form in Visual Studio so I can draw on the form (like a
basic version of Microsoft Paint). I am working through an example in
a C# 2012 book.
Alex Fr provided an excellent set of drawing tools in his DrawTools article and these tools serve as a basis for Draw Tool Redux.
You want to download this stuff its, check out a tool I recently wrote by adding to Draw Tool Redux:

The EyeDropper Colour Picker I got from http://www.codeproject.com/Articles/36540/Adobe-Eyedropper-Control
The Transparent Textbox I got from: http://www.codeproject.com/Articles/4390/AlphaBlendTextBox-A-transparent-translucent-textbo
DoesForm1_Paintsolve the OP's problem? What's all this other stuff?
– Robert Harvey♦
Jun 19 '13 at 2:57
I think they're called pearls around here, but more specifically they are the instructions to build a better program than Microsoft Paint. Looking at the code from the book, I thought OP could do with info on a C# implementation of the MFC sample DRAWCLI. Sharing your research helps everyone :)
– Jeremy Thompson
Jun 19 '13 at 3:09
add a comment |
private void Form1_Paint(object sender, EventArgs e)
if (shouldPaint)
using (Graphics graphics = CreateGraphics())
graphics.FillEllipse(new SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4);
I am trying to make a C# Windows form in Visual Studio so I can draw on the form (like a
basic version of Microsoft Paint). I am working through an example in
a C# 2012 book.
Alex Fr provided an excellent set of drawing tools in his DrawTools article and these tools serve as a basis for Draw Tool Redux.
You want to download this stuff its, check out a tool I recently wrote by adding to Draw Tool Redux:

The EyeDropper Colour Picker I got from http://www.codeproject.com/Articles/36540/Adobe-Eyedropper-Control
The Transparent Textbox I got from: http://www.codeproject.com/Articles/4390/AlphaBlendTextBox-A-transparent-translucent-textbo
private void Form1_Paint(object sender, EventArgs e)
if (shouldPaint)
using (Graphics graphics = CreateGraphics())
graphics.FillEllipse(new SolidBrush(Color.BlueViolet), e.X, e.Y, 4, 4);
I am trying to make a C# Windows form in Visual Studio so I can draw on the form (like a
basic version of Microsoft Paint). I am working through an example in
a C# 2012 book.
Alex Fr provided an excellent set of drawing tools in his DrawTools article and these tools serve as a basis for Draw Tool Redux.
You want to download this stuff its, check out a tool I recently wrote by adding to Draw Tool Redux:

The EyeDropper Colour Picker I got from http://www.codeproject.com/Articles/36540/Adobe-Eyedropper-Control
The Transparent Textbox I got from: http://www.codeproject.com/Articles/4390/AlphaBlendTextBox-A-transparent-translucent-textbo
edited Jun 19 '13 at 2:55
answered Jun 19 '13 at 2:46
Jeremy ThompsonJeremy Thompson
39.3k13102199
39.3k13102199
DoesForm1_Paintsolve the OP's problem? What's all this other stuff?
– Robert Harvey♦
Jun 19 '13 at 2:57
I think they're called pearls around here, but more specifically they are the instructions to build a better program than Microsoft Paint. Looking at the code from the book, I thought OP could do with info on a C# implementation of the MFC sample DRAWCLI. Sharing your research helps everyone :)
– Jeremy Thompson
Jun 19 '13 at 3:09
add a comment |
DoesForm1_Paintsolve the OP's problem? What's all this other stuff?
– Robert Harvey♦
Jun 19 '13 at 2:57
I think they're called pearls around here, but more specifically they are the instructions to build a better program than Microsoft Paint. Looking at the code from the book, I thought OP could do with info on a C# implementation of the MFC sample DRAWCLI. Sharing your research helps everyone :)
– Jeremy Thompson
Jun 19 '13 at 3:09
Does
Form1_Paint solve the OP's problem? What's all this other stuff?– Robert Harvey♦
Jun 19 '13 at 2:57
Does
Form1_Paint solve the OP's problem? What's all this other stuff?– Robert Harvey♦
Jun 19 '13 at 2:57
I think they're called pearls around here, but more specifically they are the instructions to build a better program than Microsoft Paint. Looking at the code from the book, I thought OP could do with info on a C# implementation of the MFC sample DRAWCLI. Sharing your research helps everyone :)
– Jeremy Thompson
Jun 19 '13 at 3:09
I think they're called pearls around here, but more specifically they are the instructions to build a better program than Microsoft Paint. Looking at the code from the book, I thought OP could do with info on a C# implementation of the MFC sample DRAWCLI. Sharing your research helps everyone :)
– Jeremy Thompson
Jun 19 '13 at 3:09
add a comment |
Try the paint events for drawing. this will help you.
The link is just an example. you need to implement as per the requirements
add a comment |
Try the paint events for drawing. this will help you.
The link is just an example. you need to implement as per the requirements
add a comment |
Try the paint events for drawing. this will help you.
The link is just an example. you need to implement as per the requirements
Try the paint events for drawing. this will help you.
The link is just an example. you need to implement as per the requirements
answered Jun 19 '13 at 3:35
Karthik AMRKarthik AMR
1,3981625
1,3981625
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17182062%2ftrouble-getting-system-drawing-graphics-to-work-in-form%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You need to put it in the Paint event
– Jeremy Thompson
Jun 19 '13 at 2:45
Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on Stack Overflow. See "Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?.
– John Saunders
Jun 19 '13 at 2:55