我有一个pictureBox1 绘画事件。在绘画事件中,我正在向文本文件写入一些信息:
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
SolidBrush brush;
Pen p=null;
Point connectionPointStart;
Point connectionPointEnd;
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
brush = new SolidBrush(Color.Red);
p = new Pen(brush);
for (int idx = 0; idx < wireObject1._point_X.Count; ++idx)
{
Point dPoint = new Point((int)wireObject1._point_X[idx], (int)wireObject1._point_Y[idx]);
dPoint.X = dPoint.X - 5; // was - 2
dPoint.Y = dPoint.Y - 5; // was - 2
Rectangle rect = new Rectangle(dPoint, new Size(10, 10));
g.FillEllipse(brush, rect);
// g.FillEllipse(brush, rect);
}
for (int i = 0; i < wireObject1._connectionstart.Count; i++)
{
int startIndex = wireObject1._connectionstart[i];
int endIndex = wireObject1._connectionend[i];
connectionPointStart = new Point((int)wireObject1._point_X[startIndex], (int)wireObject1._point_Y[startIndex]);
connectionPointEnd = new Point((int)wireObject1._point_X[endIndex], (int)wireObject1._point_Y[endIndex]);
p.Width = 4;
g.DrawLine(p, connectionPointStart, connectionPointEnd);
moveCounter++;
textBox1.Text = moveCounter.ToString();
if (moveCounter > 10)
{
w.WriteLine("Number Of Moves ===> " + moveCounter);
}
}
}
In the bottom i have a w.WriteLine....
In the top of Form1 i did StreamWriter w;
In the constructor i did: w = new StreamWriter(@"d:\test.txt");
In the pictureBox1 mouse up event im doing w.Close();
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (mouseMove == true)
{
Point NewPoint = e.Location;
wireObject1._point_X[(int)selectedIndex] = NewPoint.X;
wireObject1._point_Y[(int)selectedIndex] = NewPoint.Y;
mouseMove = false;
if (moveCounter == 0)
{
}
else
{
w.Close();
}
WireObjectCoordinatesCloneFrame();
}
}
因此,我将鼠标移动到文件中,将来自变量 moveCounter 的信息写入文件,该变量是 int 类型。
问题是当我制作鼠标向上事件并关闭pictureBox1中的文件时,我看到一个带有白色背景的BIG RED X。它自己的文件在硬盘上没有问题,包含我想要的所有信息。
但是为什么大红色 x 出现在 pictureBox1 中?如果我从绘画事件中删除 w.WriteLine,那么我就看不到大红色 x。
我能做些什么来解决它?