Graphics
-
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 ---------------------
-
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 ---------------------
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
-
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
with .Net
--------------------- Areff Bahrami(KAVEH) Areff.HB@Gmail.com ---------------------
-
with .Net
--------------------- Areff Bahrami(KAVEH) Areff.HB@Gmail.com ---------------------
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
-
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 ---------------------
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...
-
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
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? -
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 ---------------------
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().