Using a Scrollbar value as a parameter for a PaintEventArgs loop

84 views Asked by At

I am trying to create a windows.form that uses a horizontal scrollbar as a control for time. The time value is then used as an array index for 85 arrays and each array value (that isn't zero) contains a bearing that is then displayed as a line on a map. The issue I have is that I am quite new to C# and event handling so I can't find a way to link the event (and value) of hScrollBar1_Scroll to the PaintEventArgs loop. Below is a sample of the code that I've tried:

private void hScrollBar1_Scroll(object sender, ScrollEventArgs e, [PaintEventArgs f])
int t = e.NewValue;
//There's a few lines of code here that convert the value of the scrollbar to time and
//display it in a box.
{
    for (int s = 1; s <= 85; s++)
    {
        if (ldata[s, t, 0] != 0)
        {
            DrawLinesPoint(s, t, null);
        }
    }

The reason DrawLinesPoint() is in a loop is because there are 85 sites that could all be displaying bearings at the same time.

At first I tried using "PaintEventArgs f" as a parameter alongside "ScrolleventArgs e" but didn't know how to handle the event, so DrawlinesPoint() used "null" instead of "f".

public void DrawLinesPoint(int s, int t, PaintEventArgs e)
{

    Graphics g = e.Graphics;
    int c = rdata[s,0];
    Pen pen;
    switch (c)
    {
        ...
        //These bearings can be three different colours
    }
    int x1 = rdata[s,1];
    int y1 = rdata[s,2];
    int x2 = rdata[s,1] + ldata[s,t,0];
    int y2 = rdata[s,2] + ldata[s,t,1];


    g.DrawLine(pen, x1, y1, x2, y2);
}

The array rdata[] is 2 dimensional and holds the reference data for sites and ldata[] is 3 dimensional and contains the imported bearings data.

The map must be cleared and the displayed bearings changed each time the time is changed through the scrollbar.

Can anyone help with this code? There's a good chance that I'm doing this the wrong way entirely so any help would be appreciated.

1

There are 1 answers

1
MrApnea On BEST ANSWER

You don't call the Paint event from your own code. Windows decides when to draw (or you can force it to draw by calling the Invalidate method).

What you should do instead is override the control's OnPaint method (which gets called when painting needs to be done) and add your drawing code there:

    protected override void OnPaint(PaintEventArgs e) {
        // Add your drawing code here
    }

From there, you can call your other methods that have the logic.

To get a more detailed answer we need more code, like rdata and ldata. But i guess you can figure that out.