这是 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._connectionend
aList<int>
它们包含两点之间每条连接/线的起点和终点。
cyclicSelectedIndex
是List<int>
。[0] 包含第一个单击/选择的点和 [1] 第二个选择/单击的点。
当我点击一个点时我做了什么:wireObject1.GetIndexByXY 它找到了我点击的点的最近中心,所以我知道我选择了我想要的这个点,而不是它附近的一个。
最后,我需要检查何时单击/选择两个点是否与它们之间的线(连接)连接button4.enabled = true
ELSE button4.enabled = false
。
(我知道代码和描述长期以来一直试图使其最短,我很抱歉)