-1

Calling a RedrawLines() method in the Paint Event that was effective but I made some minor changes that should have had no effect and now I'm having issues. First of all, when I switch tabs, each of which contain my UserControl, the lines are not redrawing as they did before. Furthermore, when I use the MouseWheel, the lines are not drawn entirely, as in they are cut off at the top and bottom of the UserControl. Yet, when I use the ScrollBar, they are drawn in their entirety. Any idea?

Here is part of my DrawLine() method after getting the points necessary:

System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(Color.Black);
myPen.Width = 3;
System.Drawing.Graphics formGraphics = this.CreateGraphics();

formGraphics.DrawLine(myPen, p1.X, p1.Y, p2.X, p2.Y);
myPen.Dispose();
formGraphics.Dispose();

so then I have a RedrawLines method that calls this accurately

private void RedrawLines(){
    Graphics g = Graphics.FromHwnd(this.Handle);
    g.Clear(Color.White);
    g.Dispose();
    for (int i =0; i < Set_Of_Connections.Count; i++)
    {
         DrawLine(Set_Of_Connections[i].ins.cb, Set_Of_Connections[i].outs.cb, Color.Green);
    }
}

call this in the Paint Event:

private void Switch_Paint(object sender, PaintEventArgs e)
{
    RedrawLines();
}

But like I said this probably isn't going to help you at all.

Riftus
  • 59
  • 1
  • 10
  • 3
    Didn't I see this question yesterday? Post some code in either case that reproduces the problem. – LarsTech Oct 25 '12 at 19:08
  • I appreciate your enthusiasm, but I'm not going to post any code because there doesn't appear to be anything wrong with it. It's more of a inner-workings problem. – Riftus Oct 25 '12 at 21:50

1 Answers1

0

Try it like this:

public UserControl1() {
  InitializeComponent();
  this.DoubleBuffered = true;
  this.ResizeRedraw = true;
}

protected override void OnPaint(PaintEventArgs e) {
  base.OnPaint(e);

  e.Graphics.Clear(Color.White);
  for (int i = 0; i < Set_Of_Connections.Count; ++i)
  {
     DrawLine(e.Graphics, 
              Set_Of_Connections[i].ins.cb,
              Set_Of_Connections[i].outs.cb,
              Color.Green);
  }
}

protected override void OnMouseWheel(MouseEventArgs e) {
  base.OnMouseWheel(e);
  this.Invalidate();
}

protected override void OnScroll(ScrollEventArgs se) {
  base.OnScroll(se);
  this.Invalidate();
}

You should pass the Graphic object from the Paint event to your DrawLine method and use the graphic object there instead of using the CreateGraphic function, which is just a temporary drawing and will cause flickering since it will ignore any DoubleBuffering settings.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Ok this seems like a good idea, but how do I utilize this same PaintEventArgs e when I need to call my DrawLine in other places outside the OnPaint event? – Riftus Oct 29 '12 at 15:02
  • @user1721558 You shouldn't be calling `DrawLine` from anywhere else. If you need to update the control that does the `DrawLine`, you need to call `this.Invalidate()`, which will tell windows to send the paint message. – LarsTech Oct 29 '12 at 15:16
  • I'm not getting anything...since my usercontrol does the DrawLine, do I need to call Invalidate() somewhere as you say above? What might I be doing wrong? The OnPaint event is not firing it seems. – Riftus Oct 29 '12 at 15:35
  • @user1721558 Notice in my code that I am calling `Invalidate` on the `OnMouseWheel` and `OnScroll` methods. Calling `'Invalidate` ends up calling the `OnPaint` method through the windows messaging system. You do not call `OnPaint` yourself. – LarsTech Oct 29 '12 at 15:43
  • I figured out my problem...I was disposing of the graphics object...oops...thanks a lot! – Riftus Oct 29 '12 at 15:59