0

I am beginner in drawing anything using Windows Forms and C# and I have a question(I couldnt have found answer on the internet). So here is my problem : I am trying to build simple application where i use trackbar to manipulate the size of rectangle.

    namespace Drawing
{
    public partial class Form1 : Form
    {
        int size = 10;
        public Form1()
        {
            InitializeComponent();
            CenterToScreen();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {

            Graphics g = pe.Graphics;
            Rectangle rect = new Rectangle(50,50, size,size);
            LinearGradientBrush IBrush = new LinearGradientBrush(rect, Color.Green, Color.Yellow,
                LinearGradientMode.BackwardDiagonal);
            g.FillRectangle(IBrush, rect);

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
           size = trackBar1.Value;
 // I was trying to put here OnPaint method but it doesnt work.
        }


    }
}

And I just cant figure out how to call OnPaint method when tracBar1_Scroll occurs? Any suggestions? Thank you in advance.

Marcin Majewski
  • 1,007
  • 1
  • 15
  • 30

3 Answers3

3

Call this.Invalidate() this will trigger the event which makes the OnPaint go off.

Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
1

One possibility is to put your drawing code into a separate function which is called from both Form1_Paint and trackBar1_Scroll.

This method would take a Graphics object as a parameter. Something like this:

private void DrawStuff( Graphics g )
{
    // your drawing logic goes here
}

And then modify _Paint and _Scroll as follows:

private void Form1_Paint( object sender, PaintEventArgs e )
{
    DrawStuff( e.Graphics );
}

private void trackBar1.Scroll( object sender, EventArgs e )
{
    Graphics g = this.CreateGraphics();

    DrawStuff( g );

    g.Dispose();
}

Alternatively, you could force your form to invalid which would also trigger _Paint to be called:

private void trackBar1.Scroll( object sender, EventArgs e )
{
    this.Invalidate();
}

Note that Invalidate() has 6 overloads, which allow you varying levels of control over what needs to be invalidated for drawing.

Jens H
  • 4,590
  • 2
  • 25
  • 35
kmontgom
  • 1,419
  • 13
  • 18
  • It actually didnt work for me. I mean when i scroll the trackbar up it is making the rectangle but then when i scroll down instead of modifing the size of rectangle it makes another one. – Marcin Majewski Aug 30 '12 at 11:36
  • I made this work by using solution from Chris Hutchinson ( http://stackoverflow.com/questions/4124638/how-to-delete-a-drawn-circle-in-c-sharp-windows-form ). Thanks for this idea for my problem. – Marcin Majewski Aug 30 '12 at 11:52
0

you should also have a look at this link:

http://msdn.microsoft.com/en-us/library/system.windows.forms.trackbar.aspx

it has all the information about trackbar which you might find useful, such as the InvokePaintBackground method, or BackColour

Simagen
  • 409
  • 2
  • 8
  • 18