0

这是 pictureBox1 鼠标按下事件的 Form1 中的代码:

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                label1.Visible = true;
                label4.Visible = true;
                if (wireObject1 == null)
                {
                }
                else
                {
                    float t = wireObject1.GetIndexByXY(e.X, e.Y, 5);

                    if (t == -1)
                    {
                        button3.Enabled = false;
                    }
                    else
                    {
                        if (pointDeletion == false)
                        {
                            button3.Enabled = true;
                        }
                        else
                        {
                            button3.Enabled = false;
                        }
                        {
                            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;
                                    }

                                    if ((wireObject1._connectionstart[i] == cyclicSelectedIndex[0]) && (wireObject1._connectionend[i] == cyclicSelectedIndex[1]) ||
                                        (wireObject1._connectionstart[i] == cyclicSelectedIndex[1]) && (wireObject1._connectionend[i] == cyclicSelectedIndex[1]) ||
                                        (wireObject1._connectionstart[i] == cyclicSelectedIndex[0]) && (wireObject1._connectionend[i] == cyclicSelectedIndex[0]) ||
                                        (wireObject1._connectionstart[i] == cyclicSelectedIndex[1]) && (wireObject1._connectionend[i] == cyclicSelectedIndex[0]))
                                    {
                                        button4.Enabled = true;

                                    }
                                    else
                                    {
                                        button4.Enabled = false;
                                    }  
                                }
                                label13.Text = selectedIndex.ToString();
                                label13.Visible = true;
                                label14.Visible = true;
                                listView1.Items.Add(selectedIndex.ToString()).EnsureVisible();
                            }
                        }
                    }
                }
            }
        }

我有一个按钮单击事件,当我单击它时,它会在pictureBox1. 所以我添加/单击 6 次并添加 6 点。现在我可以将它们拖到该pictureBox1区域周围。然后我单击另一个按钮在两个选定点之间画一条线。当我在pictureBox鼠标按下事件中单击一个点时,我选择一个点,然后选择第二个点并单击以将它们与一条线连接。

最后,我有 6 个点和 3 条线,每两个点用画线连接它们。

现在我想在pictureBox鼠标按下事件中做的是,当我选择两个点时,它们之间的线连接起来button4.enabled = true。如果我单击任何未连接的点转button4.enabled = false

所以我试着这样做:

if ((wireObject1._connectionstart[i] == cyclicSelectedIndex[0]) && (wireObject1._connectionend[i] == cyclicSelectedIndex[1]) ||
                                        (wireObject1._connectionstart[i] == cyclicSelectedIndex[1]) && (wireObject1._connectionend[i] == cyclicSelectedIndex[1]) ||
                                        (wireObject1._connectionstart[i] == cyclicSelectedIndex[0]) && (wireObject1._connectionend[i] == cyclicSelectedIndex[0]) ||
                                        (wireObject1._connectionstart[i] == cyclicSelectedIndex[1]) && (wireObject1._connectionend[i] == cyclicSelectedIndex[0]))
                                    {
                                        button4.Enabled = true;

                                    }
                                    else
                                    {
                                        button4.Enabled = false;
                                    }  

但它并非在所有情况下都有效。

现在:

wireObject1._connectionstart是 aList<int>并且也是wireObject1._connectionendaList<int>它们包含两点之间每条连接/线的起点和终点。

cyclicSelectedIndexList<int>。[0] 包含第一个单击/选择的点和 [1] 第二个选择/单击的点。

当我点击一个点时我做了什么:wireObject1.GetIndexByXY 它找到了我点击的点的最近中心,所以我知道我选择了我想要的这个点,而不是它附近的一个。


最后,我需要检查何时单击/选择两个点是否与它们之间的线(连接)连接button4.enabled = trueELSE button4.enabled = false

(我知道代码和描述长期以来一直试图使其最短,我很抱歉)

4

2 回答 2

1

你需要调试,看看出了什么问题。如果你一步一步地做,它应该会出现在你的眼前。看起来,你正在做的wireObject1.GetIndexByXY(e.X, e.Y, 5);女巫可能没有返回正确的值,或者你的 if 语句做了一些有线的东西,比如检查一个点是否连接到自身:

wireObject1._connectionstart[i] == cyclicSelectedIndex[1]) && (wireObject1._connectionend[i] == cyclicSelectedIndex[1]

单个索引如何成为一个点的开始和结束。长话短说,逐行运行该方法,查看索引是否正确插入到数据结构中,并检查 if 语句中的变量。

于 2013-07-18T06:03:00.597 回答
0

我有正确的 IF allready 。刚刚添加了 button4.Enabled true 或 false 和 break;

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

从我到目前为止所做的测试来看,一切似乎都运行良好。我希望我是对的。

谢谢。

于 2013-07-18T16:37:54.133 回答