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

Graphics

Scheduled Pinned Locked Moved C#
helpcomgraphicsquestion
7 Posts 4 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.
  • A Offline
    A Offline
    Areff
    wrote on last edited by
    #1

    Hi, I have problem in graphics in this code no compiletime error occurs. private void Lines(System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; Pen pen = new Pen(Color.Red,4); // draw line from (10,10) to (25,25) g.DrawLines(pen,10,10,25,25); } But when i call this method with Onclick event of a button this error occurs in runtime: "Object refrence not set a instance of an object." and when i want to use his code Graphics g = new Graphics(); this error occurs : No overloaded method 'Graphics' takes '0' arguments I want to call a method and paint some lines in my form but i dont want to override OnPaint method of form like this : protected override void OnPaint( PaintEventArgs paintEvent ) { // get graphics object Graphics g = paintEvent.Graphics; Pen pen = new Pen(Color.Red,4); g.DrawLines(pen,10,10,25,25); } How i can do this ?

    --------------------- Areff Bahrami(KAVEH) Areff.HB@Gmail.com ---------------------

    C V B 3 Replies Last reply
    0
    • A Areff

      Hi, I have problem in graphics in this code no compiletime error occurs. private void Lines(System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; Pen pen = new Pen(Color.Red,4); // draw line from (10,10) to (25,25) g.DrawLines(pen,10,10,25,25); } But when i call this method with Onclick event of a button this error occurs in runtime: "Object refrence not set a instance of an object." and when i want to use his code Graphics g = new Graphics(); this error occurs : No overloaded method 'Graphics' takes '0' arguments I want to call a method and paint some lines in my form but i dont want to override OnPaint method of form like this : protected override void OnPaint( PaintEventArgs paintEvent ) { // get graphics object Graphics g = paintEvent.Graphics; Pen pen = new Pen(Color.Red,4); g.DrawLines(pen,10,10,25,25); } How i can do this ?

      --------------------- Areff Bahrami(KAVEH) Areff.HB@Gmail.com ---------------------

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Areff wrote:

      I want to call a method and paint some lines in my form but i dont want to override OnPaint method of form

      That's the problem. You want to do it all wrong. You're getting that error as you're passing null as the argument, or you're passing a PaintEventArgs that doesn't contain a Graphics object. Either way, not overriding the paint method means you want to write bad code that won't work properly. Why would you want to do that ?

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      A V 2 Replies Last reply
      0
      • C Christian Graus

        Areff wrote:

        I want to call a method and paint some lines in my form but i dont want to override OnPaint method of form

        That's the problem. You want to do it all wrong. You're getting that error as you're passing null as the argument, or you're passing a PaintEventArgs that doesn't contain a Graphics object. Either way, not overriding the paint method means you want to write bad code that won't work properly. Why would you want to do that ?

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        A Offline
        A Offline
        Areff
        wrote on last edited by
        #3

        with .Net

        --------------------- Areff Bahrami(KAVEH) Areff.HB@Gmail.com ---------------------

        C 1 Reply Last reply
        0
        • A Areff

          with .Net

          --------------------- Areff Bahrami(KAVEH) Areff.HB@Gmail.com ---------------------

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          I have no idea what you mean by that. Using C# means you'll be using .NET. That doesn't change what I said.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

          1 Reply Last reply
          0
          • A Areff

            Hi, I have problem in graphics in this code no compiletime error occurs. private void Lines(System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; Pen pen = new Pen(Color.Red,4); // draw line from (10,10) to (25,25) g.DrawLines(pen,10,10,25,25); } But when i call this method with Onclick event of a button this error occurs in runtime: "Object refrence not set a instance of an object." and when i want to use his code Graphics g = new Graphics(); this error occurs : No overloaded method 'Graphics' takes '0' arguments I want to call a method and paint some lines in my form but i dont want to override OnPaint method of form like this : protected override void OnPaint( PaintEventArgs paintEvent ) { // get graphics object Graphics g = paintEvent.Graphics; Pen pen = new Pen(Color.Red,4); g.DrawLines(pen,10,10,25,25); } How i can do this ?

            --------------------- Areff Bahrami(KAVEH) Areff.HB@Gmail.com ---------------------

            V Offline
            V Offline
            V 0
            wrote on last edited by
            #5

            You can get a Graphics object from a windows (or control) handle. I think the syntax is: Graphics.FromHandle(...); Pass it a handle from a panel or button, or whatever tot draw on that control... Hope this helps...

            V.
            Moviereview Archive

            1 Reply Last reply
            0
            • C Christian Graus

              Areff wrote:

              I want to call a method and paint some lines in my form but i dont want to override OnPaint method of form

              That's the problem. You want to do it all wrong. You're getting that error as you're passing null as the argument, or you're passing a PaintEventArgs that doesn't contain a Graphics object. Either way, not overriding the paint method means you want to write bad code that won't work properly. Why would you want to do that ?

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

              V Offline
              V Offline
              V 0
              wrote on last edited by
              #6

              Christian Graus wrote:

              not overriding the paint method means you want to write bad code that won't work properly

              I always use the Graphics.FromHandle function to get the Graphics object, why should this be bad code?

              V.
              Moviereview Archive

              1 Reply Last reply
              0
              • A Areff

                Hi, I have problem in graphics in this code no compiletime error occurs. private void Lines(System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; Pen pen = new Pen(Color.Red,4); // draw line from (10,10) to (25,25) g.DrawLines(pen,10,10,25,25); } But when i call this method with Onclick event of a button this error occurs in runtime: "Object refrence not set a instance of an object." and when i want to use his code Graphics g = new Graphics(); this error occurs : No overloaded method 'Graphics' takes '0' arguments I want to call a method and paint some lines in my form but i dont want to override OnPaint method of form like this : protected override void OnPaint( PaintEventArgs paintEvent ) { // get graphics object Graphics g = paintEvent.Graphics; Pen pen = new Pen(Color.Red,4); g.DrawLines(pen,10,10,25,25); } How i can do this ?

                --------------------- Areff Bahrami(KAVEH) Areff.HB@Gmail.com ---------------------

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

                Use

                Graphics g = this.CreateGraphics()

                in your constructor, and declare g as a class member so you can use it elsewhere. I've used this approach a few times to support custom double buffering on a control. If you want your control to display everything flickerfree you should implement this too (draw into a bitmap and flip it over to your graphics now and then), this can be handy for controls that need a high refresh rate (30fps). If you're not doing that, use OnPaint().

                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