so i got this piece of code. (currPosX is defined earlier)
while (earliestDate < DateTime.Today)
{
currPosX = currPosX + 5;
e.Graphics.DrawLine(Pens.Black, currPosX, 0, currPosX, 10);
earliestDate = earliestDate.AddDays(1);
}
the graphics don’t draw. it’s really weird, since this only happens when the condition statement is a date comparison. I debugged, and it does go in the loop, and the values are messed with (currPosX for example). But, no display. one more weirdness, if I add a MessageBox.Show(“blabla”) in the loop, the message box pops up, and graphics are drawn. what’s going on here?
EDIT: just to remind you guys, when it’s a non-datetime condition, it works. meaning that this code works. it does display a series of lines
int i = 0;
while(i < 10)
{
currPosX = currPosX + 5;
e.Graphics.DrawLine(Pens.Black, currPosX, 0, currPosX, 10);
i++;
}
As your tests indicate, the issue has nothing to do with comparing DateTime’s. Since your code is entering the loop, and you know the painting is being done, something else must be going on. We’ll probably need to see more code to identify the problem
Trying to step through painting code is useless. The fact that the debugger and application window trade focus will completely screw things up. You are better off using tracepoints, not breakpoints.
But here’s some possibilities:
UPDATE
In response to your edit:
Your problem is that earliestDate will keep creeping forward because you are modifying it in your Paint event and the value will persist between Paint events. Paint events occur repeatedly every time the control is invalidated. You have two options.
I suggest option 1.