Hi,

I have code:

public func(z)
{
    int a = z;
    pnlGraphics.Paint += new PaintEventHandler(panel1_Paint);
}

private void panel1_Paint(object sender, PaintEventArgs e)
{

}

how can I send val a to panel1_Paint?

Recommended Answers

All 3 Replies

What is it you want to do with your panel? Here is an example that draws some text whenever it is repainted, and the form has a button to change the 'value' of the text:

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.Threading;

namespace ForumSolutions
{
    public partial class Form_Panel : Form
    {
        string text = "Merry Christmas!";

        public Form_Panel()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawString(text, ((Panel)sender).Font, new SolidBrush(Color.Red), e.ClipRectangle);
        }

        private void Form_Panel_Load(object sender, EventArgs e)
        {
            // force the panel to redraw....
            panel1.Refresh();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            text = "Happy New Year!";
            panel1.Refresh();
        }
    }
}

I wanna make some little animation for example move rectangle:

prototype

public void func1(int x, int y, int a, int b)
{
   .....
   for(int i=0;i<30;i++){
       FillRectangle(, x, y-i, a, b);
       Thread.Sleep(100);
   }
   ....
}

how can I do func1 correct?

If it is a rectangle you want to move/draw, you can call the Graphics.FillRectangle method in the OnPaint :

private void panel1_Paint2(object sender, PaintEventArgs e)
        {
            e.Graphics.FillRectangle(Brushes.Red, rect);
        }

For the above Rectangle , I created a class variable called rect :

Rectangle rect = new Rectangle(0, 0, 20, 20);

Then, all you need to do is call a method similar to the snippet of code you posted, passing the parameters/coordinates as arguments:

private void MoveRectangle(int x, int y, int width, int height)
        {
            rect.X = x;
            rect.Y = y;
            rect.Width = width;
            rect.Height = height;
            panel1.Refresh();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 30; i++)
            {
                MoveRectangle(i * 3, 0, 20, 20);
                Thread.Sleep(200);
            }        
        }

The above will move a small red-square horizontally.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.