为什么我会重复价值观?有时相同的值连续3次?

时间:2012-06-26 07:02:51

标签: c#

我所做的是,在Form1的顶部我添加了一个新的int变量moveCounter。 在构造函数中,我使用0. moveCounter = 0;

启动变量

在pictureBox1 paint事件中,我做了:moveCounter ++; 而且在paint事件中,我将moveCounter值放入textBox:

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);    
               }

               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();
               }
        }

在Form1的顶部,我创建了一个StreamWriter w的变量;在我做的构造函数中:

w = new StreamWriter(@"d:\movesCounter.txt");

在pictureBox1鼠标移动事件中,我只检查moveCounter是否大于10,然后将值写入文本文件。

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseMove == true)
            {
                mouseDrag = true;
                Point NewPoint = e.Location;                    
                    wireObject1.MovePoint(selectedIndex, NewPoint, NewPoint); // when moving a point dragging the other point is vanished deleted. To check why !

                    label1.Text = "{X = " + NewPoint.X + "}" + " " + "{Y = " + NewPoint.Y + "}";
                    refreshFlag = true;
                    timer2.Start();

                    if (moveCounter > 10)
                    {
                        w.WriteLine("Number Of Moves ===>   " + moveCounter);
                    }
            }
            else
            {
                label19.Text = "{X = " + e.X + "}" + " " + "{Y = " + e.Y + "}";
            }
        }

在pictureBox1鼠标注册事件中,我关闭了StreamWriter:

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 > 10)
                {
                    w.Close();
                }
                WireObjectCoordinatesCloneFrame();
            }
    }

我想要做的是将moveCounter的值记录/写入文本文件。 我把moveCounter ++;在底部循环的paint事件中,当两个或多个点通过line / s连接时,此循环工作,当我在pictureBox区域客户端周围移动其中一个连接点时,我实时看到moveCounter值上升。

问题是在我的硬盘上的文本文件中,我得到的结果是这样的:

Number Of Moves ===>   11
Number Of Moves ===>   11
Number Of Moves ===>   11
Number Of Moves ===>   12
Number Of Moves ===>   12
Number Of Moves ===>   12
Number Of Moves ===>   13
Number Of Moves ===>   13
Number Of Moves ===>   13
Number Of Moves ===>   14
Number Of Moves ===>   14
Number Of Moves ===>   14
Number Of Moves ===>   15
Number Of Moves ===>   15
Number Of Moves ===>   16
Number Of Moves ===>   16
Number Of Moves ===>   17
Number Of Moves ===>   17
Number Of Moves ===>   18
Number Of Moves ===>   18
Number Of Moves ===>   19
Number Of Moves ===>   19
Number Of Moves ===>   20
Number Of Moves ===>   20
Number Of Moves ===>   20
Number Of Moves ===>   21
Number Of Moves ===>   21
Number Of Moves ===>   21

我无法弄清楚为什么我连续两次甚至三次获得一些值,如20 20 20或21 21 21或18 18,而不是每次获得一次。

这是值169.我想添加这个传递5秒,没关系,但是值169再次出现几次而不是一次。在所有文本文件中都是这样的。

Number Of Moves ===>   169
Passed 5 Seconds ===>   5
Number Of Moves ===>   169

可以解决什么问题?

EDITED **

这是pictureBox1鼠标按下事件,我将mouseMove bool变量设置为true,所以问题可能就在这里。

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                label1.Visible = true;
                label4.Visible = true;




                float t = wireObject1.GetIndexByXY(e.X, e.Y, 5);

                if (t == -1)
                {
                    button3.Enabled = false;
                }
                else
                {
                    button3.Enabled = true;
                    {
                        selectedIndex = t;
                        mouseMove = true;
                        OriginalX = wireObject1._point_X[(int)selectedIndex];
                        OriginalY = wireObject1._point_Y[(int)selectedIndex];



                        if (cyclicSelectedIndex.Count() == 2)
                        {

                            cyclicSelectedIndex[currentCyclicIndex] = (int)selectedIndex;
                            currentCyclicIndex++;
                            if (currentCyclicIndex == 2)
                            {
                                currentCyclicIndex = 0;
                            }


                            if ((cyclicSelectedIndex[0] == cyclicSelectedIndex[1]) || (cyclicSelectedIndex[0] == -1) || (cyclicSelectedIndex[1] == -1))

                            {
                                button2.Enabled = false;
                            }
                            else
                            {
                                button2.Enabled = true;
                            }

                            for (int i = 0; i < wireObject1._connectionstart.Count; i++) 
                            {
                                if ((wireObject1._connectionstart[i] == cyclicSelectedIndex[0] && wireObject1._connectionend[i] == cyclicSelectedIndex[1]) ||
                                   (wireObject1._connectionstart[i] == cyclicSelectedIndex[1] && wireObject1._connectionend[i] == cyclicSelectedIndex[0]) ||
                                    wireObject1._connectionstart[i] == cyclicSelectedIndex[0] || wireObject1._connectionend[i] == cyclicSelectedIndex[1] ||
                                     wireObject1._connectionstart[i] == cyclicSelectedIndex[1] || wireObject1._connectionend[i] == cyclicSelectedIndex[0])
                                {

                                    button3.Enabled = false;
                                }

                                if ((wireObject1._connectionstart[i] == cyclicSelectedIndex[0] && wireObject1._connectionend[i] == cyclicSelectedIndex[1]) ||
                                   (wireObject1._connectionstart[i] == cyclicSelectedIndex[1] && wireObject1._connectionend[i] == cyclicSelectedIndex[0]))
                                {
                                    button2.Enabled = false;  
                                }
                            }

                            label13.Text = selectedIndex.ToString();
                            label13.Visible = true;
                            label14.Visible = true;
                            listView1.Items.Add(selectedIndex.ToString()).EnsureVisible();
                        }
                    }
                }
            }
        }

另一件事是我为设计器Timer1添加了一个定时器。 定时器已启用并设置为30毫秒。

在pictureBox鼠标移动事件中我做了:

refreshFlag = true;

然后在timer1 tick事件中我检查这个标志是否为true然后制作一个pictureBox1.refresh(); 我的想法是当我在移动事件中拖动/移动连接点或未连接点时,每30毫秒制作一次绘制事件。那么问题可能在那里吗?

这是timer1 tick事件:

private void timer1_Tick(object sender, EventArgs e)
        {
            if (refreshFlag == true)
            {
                pictureBox1.Refresh();
                refreshFlag = false;
            }
        }

1 个答案:

答案 0 :(得分:0)

您的pictureBox1_MouseMove被调用的频率高于您的pictureBox1_Paint,因为第二个调用仅每30毫秒调用一次,如果移动了鼠标,则会被调用。

所以增加移动鼠标的moveCounter:

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    ...
    moveCounter++;
    textBox1.Text = moveCounter.ToString();
    ...
}