graphics

M

Michael

Hello, I am trying to follow a graphics tutorial which gives the
following code

private void DrawShapesForm_Paint(object sender, PaintEventArgs e)
{
// references to object we will use
Graphics graphicsObject = e.Graphics;
// ellipse rectangle and gradient brush
Rectangle drawArea1 = new Rectangle(5, 35, 30, 100);
LinearGradientBrush linearBrush =
new LinearGradientBrush(drawArea1, Color.Blue,
Color.Yellow, LinearGradientMode.ForwardDiagonal);
// draw ellipse filled with a blue-yellow gradient
graphicsObject.FillEllipse(linearBrush, 5, 30, 65, 100);
}
//---------------------------------------
Now on a form I have placed a single command button and want the
above to execute when I click it. In the (Paint) events tab of the button I
have
enabled DrawShapesForm_Paint.
So far nothing is happening when I click the button. Am using VS2008.
What am I doing wrong, or perhaps failing to do?
TIA Mick
 
M

Michael

Martijn Mulder said:
Send the whole program (including Main) and I'll fix it
// Form1.Designer.cs
namespace WindowsFormsApplication4
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(305, 75);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Paint += new
System.Windows.Forms.PaintEventHandler(this.DrawShapesForm_Paint);
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(410, 306);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
}
}

// end Form1.Designer.cs

// start Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;


namespace WindowsFormsApplication4
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

// end Program.cs

//Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void DrawShapesForm_Paint(object sender, PaintEventArgs e)
{
// references to object we will use
Graphics graphicsObject = e.Graphics;
// ellipse rectangle and gradient brush
Rectangle drawArea1 = new Rectangle(5, 35, 30, 100);
LinearGradientBrush linearBrush =
new LinearGradientBrush(drawArea1, Color.Blue,
Color.Yellow, LinearGradientMode.ForwardDiagonal);

// draw ellipse filled with a blue-yellow gradient
graphicsObject.FillEllipse(linearBrush, 5, 30, 65, 100);



}

private void button1_Click(object sender, EventArgs e)
{
//No Code in here yet, I just want to call DrawShapesForm_Paint
//drawing when clicked
}
}
}

// Thanks Martin for having a look...Mick
 
M

Martijn Mulder

Michael said:
Martijn Mulder said:
Send the whole program (including Main) and I'll fix it
// Form1.Designer.cs
namespace WindowsFormsApplication4
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(305, 75);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Paint += new
System.Windows.Forms.PaintEventHandler(this.DrawShapesForm_Paint);
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(410, 306);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
}
}

// end Form1.Designer.cs

// start Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;


namespace WindowsFormsApplication4
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

// end Program.cs

//Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void DrawShapesForm_Paint(object sender, PaintEventArgs e)
{
// references to object we will use
Graphics graphicsObject = e.Graphics;
// ellipse rectangle and gradient brush
Rectangle drawArea1 = new Rectangle(5, 35, 30, 100);
LinearGradientBrush linearBrush =
new LinearGradientBrush(drawArea1, Color.Blue,
Color.Yellow, LinearGradientMode.ForwardDiagonal);

// draw ellipse filled with a blue-yellow gradient
graphicsObject.FillEllipse(linearBrush, 5, 30, 65, 100);



}

private void button1_Click(object sender, EventArgs e)
{
//No Code in here yet, I just want to call DrawShapesForm_Paint
//drawing when clicked
}
}
}

// Thanks Martin for having a look...Mick




Here is the modified button1_Click method:


private void button1_Click(object sender, EventArgs e)
{
this.Paint += new
System.Windows.Forms.PaintEventHandler(DrawShapesForm_Paint);
Invalidate();
}
 
M

Martijn Mulder

Uh. Close, but no cigar. The call to Invalidate() is correct, but the
event subscription should be done elsewhere. Otherwise, every time you
click the button, you get the event handler added again. In other words,
on any invalidate of the control the event handler will be called as many
times as the button has been clicked so far.


The requirement was to have the delegate execute after clicking the button,
so even if, as a side effect, a monkey jumped out of your ear, the solution
is correct as long as the oval is drawn.
 
J

Jeff Johnson

The requirement was to have the delegate execute after clicking the
button, so even if, as a side effect, a monkey jumped out of your ear, the
solution is correct as long as the oval is drawn.

The problem is that the poster is being shown a very bad way of programming.
The Paint event of an object is very different from most other events,
because it gets triggered in "uncommon" ways. The user shouldn't even be
trying to execute it from a button at all! It should just "be there."

If the poster does want to validate that something is happening when he
clicks a button, then he should change the color of the gradient when the
button in clicked or something like that.
 
C

Chris Dunaway

Hello, I am trying to follow a graphics tutorial which gives the
following code

private void DrawShapesForm_Paint(object sender, PaintEventArgs e)
{
// references to object we will use
Graphics graphicsObject = e.Graphics;
// ellipse rectangle and gradient brush
Rectangle drawArea1 = new Rectangle(5, 35, 30, 100);
LinearGradientBrush linearBrush =
new LinearGradientBrush(drawArea1, Color.Blue,
Color.Yellow, LinearGradientMode.ForwardDiagonal);
// draw ellipse filled with a blue-yellow gradient
graphicsObject.FillEllipse(linearBrush, 5, 30, 65, 100);
}
//---------------------------------------
Now on a form I have placed a single command button and want the
above to execute when I click it. In the (Paint) events tab of the button I
have
enabled DrawShapesForm_Paint.
So far nothing is happening when I click the button. Am using VS2008.
What am I doing wrong, or perhaps failing to do?
TIA Mick

You need to wire the Paint event of the Form to this method *not* the
paint event of the button. Then the form will be painted properly.

See this site for some good information on properly programming with
graphics and .Net:

www.bobpowell.net

Chris
 
M

Michael

Chris Dunaway said:
You need to wire the Paint event of the Form to this method *not* the
paint event of the button. Then the form will be painted properly.

See this site for some good information on properly programming with
graphics and .Net:

www.bobpowell.net

Chris
Thanks Chris. Did as you said and it works. I'll move on now and study
Bob Powell's site. Thanks to all who participated in my question.
Cheers Mick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top