Windows Develop Bookmark and Share   
 index > Windows Forms General > Paint problem
 

Paint problem

I am trying to call a panting method i created inside a button located on my windows form. I cant figure out how to call my draw method in a form buto, Here is my code:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Drawing.Drawing2D;

namespace WindowsFormsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void onPaint(object sender, PaintEventArgs e)

{

drawer(e);

}

private void drawer(PaintEventArgs e)

{

Graphics g = e.Graphics;

Brush yel = new SolidBrush(Color.Yellow);

Brush grn = new SolidBrush(Color.Green);

Brush blu = new SolidBrush(Color.SkyBlue);

Rectangle sun = new Rectangle(Width - 100, 15, 80, 80);

Rectangle grass = new Rectangle(0, 300, Width, Height);

Rectangle sky = new Rectangle(0, 0, Width, Height);

g.FillRectangle(blu, sky);

g.FillRectangle(grn, grass);

g.FillEllipse(yel, sun);

}

private void button1_Click(object sender, EventArgs e)

{

///I wish to call my draw method here

}

}

}

JR Hedrick  Friday, December 21, 2007 3:48 AM

Hi,

The paint event occurs when a control needs repainting. And the Paint Event handler generally takes two parameters, for example:

Code Block

private void btnTest_Paint(object sender, PaintEventArgs e)

{

//�/span>

}

Where Sender represents the source of the event; e is a PaintEventArgs variable that contains the event data.

From your code, if you’d like to repaint some control, you can set OnPaint as the Paint event handler of that control, and then invoke the InValidate method of that control. The system will automatically produce the parameters and invoke OnPaint to repaint that control.

Take an example as illustration, assume that you’d like to repaint a button control “btnTestPaint� you can add following code to “button1_Click�

Code Block

btnTestPaint.Paint += new PaintEventHandler(onPaint);

btnTestPaint.Invalidate();

Hope this helps!

Yan-Fei Wei  Monday, December 24, 2007 8:05 AM

Hi,

The paint event occurs when a control needs repainting. And the Paint Event handler generally takes two parameters, for example:

Code Block

private void btnTest_Paint(object sender, PaintEventArgs e)

{

//�/span>

}

Where Sender represents the source of the event; e is a PaintEventArgs variable that contains the event data.

From your code, if you’d like to repaint some control, you can set OnPaint as the Paint event handler of that control, and then invoke the InValidate method of that control. The system will automatically produce the parameters and invoke OnPaint to repaint that control.

Take an example as illustration, assume that you’d like to repaint a button control “btnTestPaint� you can add following code to “button1_Click�

Code Block

btnTestPaint.Paint += new PaintEventHandler(onPaint);

btnTestPaint.Invalidate();

Hope this helps!

Yan-Fei Wei  Monday, December 24, 2007 8:05 AM

You can use google to search for other answers

Custom Search

More Threads

• Move a picturebox around with the mouse
• Add images to Listview control in C#.net with 3.5 frame work.
• InitializeComponent method
• Grid comes on top of sine waves
• How to use Class object to show data on report.
• Returning data from Control to Owner form? (C++)
• Moving user controls on a panel has strange consequences...
• Update UserControl's width inside FlowLayoutPanel automatically
• 3rd Party winform tool set ?
• Adding controls to the same location on a Canvas (in code)