如何检查pictureBox上是否连接了两个点,然后启用true button4?

时间:2013-07-18 04:45:43

标签: c# winforms

这是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._connectionstartList<int>wireObject1._connectionendList<int>,它们包含两点之间每条连接/线的起点和终点。

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

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


最后我需要检查点击/选择两点如果它们与它们之间的线路(连接)连接button4.enabled = true ELSE button4.enabled = false

(我知道代码和描述很长时间都是为了让它最短,因为我很抱歉)

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

我已经拥有了正确的IF。 刚刚添加了button4.Enabled true或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;
                                        button4.Enabled = true;
                                        break;
                                    }
                                    else
                                    {
                                        button4.Enabled = false;
                                    }

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

感谢。