tags:

views:

40

answers:

1

How can I put my created graphic codes on the paint event? I'm having troubles displaying It to the paint. I tried to add Points to a List but the lines I have drawn are merging. Help!

Here's the code:

private Point _ps = new Point();
private Point _pe = new Point();
private TransparentPanel _thispan;
private Rectangle _SelectRect;
private Graphics _g;
private Pen _pen;
List<Point> _listPS = new List<Point>();
List<Point> _listPE = new List<Point>();

private void _newTransPan_MouseDown(object sender, MouseEventArgs e)
{
    _SelectRect.Width = 0;
    _SelectRect.Height = 0;
    _SelectRect.X = e.X;
    _SelectRect.Y = e.Y;

    _ps.X = e.X;
    _ps.Y = e.Y;
    _pe = _ps;
}

private void _newTransPan_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        _thispan = (TransparentPanel)sender;

        ControlPaint.DrawReversibleLine(_thispan.PointToScreen(_ps),  _thispan.PointToScreen(_pe), Color.Red);
        _pe = new Point(e.X, e.Y);
        ControlPaint.DrawReversibleLine(_thispan.PointToScreen(_ps), _thispan.PointToScreen(_pe), Color.Red);
    }
}

private void _newTransPan_MouseUp(object sender, MouseEventArgs e)
{

    _thispan = (TransparentPanel)sender;

    _g = _thispan.CreateGraphics();
    _flagCol = _mdt._getColorVal();
    _pen = new Pen(_flagCol, _mdt._getPenVal());
    _pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
    _pen.DashCap = System.Drawing.Drawing2D.DashCap.Triangle;
    ControlPaint.DrawReversibleLine(_thispan.PointToScreen(_ps), _thispan.PointToScreen(_pe), Color.Red);
     _g.DrawLine(_pen, _ps, _pe);

     _listPS.Add(_ps);
     _listPE.Add(_pe);

 }

private void _newTransPan_Paint(object sender, PaintEventArgs e)
{
    _thispan = (TransparentPanel)sender;
    _g = _thispan.CreateGraphics();

    foreach (Point _tps in _listPS)
    {
        foreach (Point _tpe in _listPE)
        {
           _g.DrawLine(_pen, _tps, _tpe);
        }
    }
}
+2  A: 

In your _newTransPan_Paint event handler, you are connecting every start point with every end point. Of course this won’t produce a continuous segmented line; it’ll produce a scribble. You want to connect each start point only with its corresponding end point:

private void _newTransPan_Paint(object sender, PaintEventArgs e)
{
    var g = _newTransPan.CreateGraphics();

    for (int i = 0; i < _listPS.Count; i++)
        g.DrawLine(_pen, _listPS[i], _listPE[i]);
}

Once again, I would like to stress that you should use local variables. You will create yourself difficult-to-find bugs later if you don’t. There is a reason local variables exist; use them whenever you can, and use fields only if you need to. In your code:

  • _g and _pen should be local variables. You are assigning a new value to them in every method, so there is no data sharing going on.

  • If I’m not mistaken, you already have _newTransPan for your panel, so _thispan is redundant. You can probably remove it entirely and replace all occurrences of it with _newTransPan.

Timwi
Thanks man problem solved. Local variables :)
Rye