How do I load a block on a form at startup
-
I am able to load a block on to a form when I click a button (the button is on the form) but I want to move my code so that when the form loads the block on the form will also load. Where would I put the code? /// Graphics gs = this.CreateGraphics(); Pen p = new Pen(new SolidBrush(Color.Red)); gs.DrawRectangle(p, Goal);
-
I am able to load a block on to a form when I click a button (the button is on the form) but I want to move my code so that when the form loads the block on the form will also load. Where would I put the code? /// Graphics gs = this.CreateGraphics(); Pen p = new Pen(new SolidBrush(Color.Red)); gs.DrawRectangle(p, Goal);
Brian_TheLion wrote:
I want to move my code so that when the form loads the block on the form will also load.
Unclear what you are doing here: 1. you can add, or remove, the Paint EventHandler for any Form in code ... to draw on the Form. 2. the standard Load EventHandler you define is called automatically when the Form is shown. If you create new Forms at run-time, you can install a Paint EventHandler once you have a valid reference to the new Form.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
-
I am able to load a block on to a form when I click a button (the button is on the form) but I want to move my code so that when the form loads the block on the form will also load. Where would I put the code? /// Graphics gs = this.CreateGraphics(); Pen p = new Pen(new SolidBrush(Color.Red)); gs.DrawRectangle(p, Goal);
Don't just draw the block like that - partly because it'll disappear when you minimize the form or drag another form over the top, and partly because you need to Dispose of all Graphics contexts you create as soon as you are finished with them. The best way of doing that is via a
using
block:using (Graphics gs = this.CreateGraphics())
{
using (Brush b = new SolidBrush(Color.Red))
{
using (Pen p = new Pen(b))
{
gs.DrawRectangle(p, Goal);
...
}
}
...
}Note that that also applies to Pens, Brushes, and other Graphics elements. If you want to draw on your form, then handle the
Form.Paint
event and use the graphics context provided:private void FrmMain\_Paint(object sender, PaintEventArgs e) { using (Brush b = new SolidBrush(Color.Red)) { using (Pen p = new Pen(b)) { e.Graphics.DrawRectangle(p, Goal); } } }
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
I am able to load a block on to a form when I click a button (the button is on the form) but I want to move my code so that when the form loads the block on the form will also load. Where would I put the code? /// Graphics gs = this.CreateGraphics(); Pen p = new Pen(new SolidBrush(Color.Red)); gs.DrawRectangle(p, Goal);
Use the Paint handler, it is the only place where drawing ever should occur. Avoid calling
CreateGraphics
, it is an expensive operation. The Paint handler gives itsGraphics
for free, see thePaintEventArgs
that comes with it. For all those small but disposable objects (Pens, Brushes, Fonts, ...) consider NOT creating them all the time by either: - using the pre-existing ones, e.g.Pens.Red
,Brushes.Red
, etc. (and since you didn't create those, you are not supposed to dispose of them either); - creating your own, possibly more specialized ones (e.g. a thicker Pen) but then create them only once and keep them alive in class variables; when doing so, there is no need to dispose of them when your program exits. :)Luc Pattyn [My Articles] If you can't find it on YouTube try TikTok...
-
I am able to load a block on to a form when I click a button (the button is on the form) but I want to move my code so that when the form loads the block on the form will also load. Where would I put the code? /// Graphics gs = this.CreateGraphics(); Pen p = new Pen(new SolidBrush(Color.Red)); gs.DrawRectangle(p, Goal);
Add the code to the Form's Load event: [Form.Load Event (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form.load?view=netcore-3.1)
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
-
Don't just draw the block like that - partly because it'll disappear when you minimize the form or drag another form over the top, and partly because you need to Dispose of all Graphics contexts you create as soon as you are finished with them. The best way of doing that is via a
using
block:using (Graphics gs = this.CreateGraphics())
{
using (Brush b = new SolidBrush(Color.Red))
{
using (Pen p = new Pen(b))
{
gs.DrawRectangle(p, Goal);
...
}
}
...
}Note that that also applies to Pens, Brushes, and other Graphics elements. If you want to draw on your form, then handle the
Form.Paint
event and use the graphics context provided:private void FrmMain\_Paint(object sender, PaintEventArgs e) { using (Brush b = new SolidBrush(Color.Red)) { using (Pen p = new Pen(b)) { e.Graphics.DrawRectangle(p, Goal); } } }
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
Thanks Griff. I'll try what you suggested.
-
Add the code to the Form's Load event: [Form.Load Event (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form.load?view=netcore-3.1)
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
Can you give me an example please