Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do I load a block on a form at startup

How do I load a block on a form at startup

Scheduled Pinned Locked Moved C#
questiongraphics
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Brian_TheLion
    wrote on last edited by
    #1

    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);

    B OriginalGriffO L L 4 Replies Last reply
    0
    • B Brian_TheLion

      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);

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • B Brian_TheLion

        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);

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        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 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

        B 1 Reply Last reply
        0
        • B Brian_TheLion

          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);

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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 its Graphics for free, see the PaintEventArgs 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...

          1 Reply Last reply
          0
          • B Brian_TheLion

            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);

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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

            B 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              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!

              B Offline
              B Offline
              Brian_TheLion
              wrote on last edited by
              #6

              Thanks Griff. I'll try what you suggested.

              1 Reply Last reply
              0
              • L Lost User

                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

                B Offline
                B Offline
                Brian_TheLion
                wrote on last edited by
                #7

                Can you give me an example please

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups