I have two Lists of type int where I store point coordinates called Point_X and Point_Y.
This is the delete method in the new class:
public void DeletePoint(int x, int y)
{
for (int i = 0; i < Point_X.Count; i++)
{
if ((i == x) && (i == y) || y == -1 || x == -1)
{
Point_X.RemoveAt(i);
Point_Y.RemoveAt(i);
}
else
{
}
}
}
In x and y, I’m getting the index of the point I clicked. If I add two points and click on one of them so I’m getting two indexes of the point, e.g. x will be 0 and y will be -1.
Now I’m running through the Point_X list and I need to check for all situations and compare the indexes I’m getting with the indexes in the lists Point_X and Point_Y and then it should remove the point I clicked on.
This if doesn’t seem to be working: if ((i == x) && (i == y) || y == -1 || x == -1).
If I have two points in pictureBox1 and I click on point number 2 and try to delete it, it doesn’t get removed the first time around. It does get removed the second time I click though.
This is the code of the button click in Form1 where I click to delete the point:
private void button3_Click(object sender, EventArgs e)
{
wireObject1.DeletePoint(cyclicSelectedIndex[0],cyclicSelectedIndex[1]);
button3.Enabled = false;
}
This is the code where I click on the point I want to select in pictureBox1‘s MouseDown event:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
label1.Visible = true;
label4.Visible = true;
// find the index that is closest to the current mouse location
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]))
{
button2.Enabled = false;
}
}
label13.Text = selectedIndex.ToString();
label13.Visible = true;
label14.Visible = true;
listView1.Items.Add(selectedIndex.ToString()).EnsureVisible();
}
}
}
}
}
The problem is that the DeletePoint method in the new class doesn’t delete the point I clicked on unless I click on it twice.
And it’s logical if ((i == x) && (i == y)) since in some cases x is 1 and y is 0 so the variable (i) will never be also 0 and also 1 so I’m stuck here.
I don’t how to check for all situations – e.g. x is 1 and y is 0. There may be other scenarios to cover but I think the problem is somewhere in this if statement.
x = 1 , y = 1
Point_X >>> [0] = 331.0 , [1] = 212.0
Point_Y >>> [0] = 213.0 , [1] = 212.0
i = 0
This is the case when i have two points and i clicked on the second point.
It didnt do anything in the function in the new class it didnt remove the point.
Same for the first point it didnt remove it it didnt enter to the remove area.
This is when im using Alexei Levenkov idea sample for the If checking.
Now in Form1 when im clicking on a point in the pictureBox1 so in the mouse down event i have a variable(t) wich i calculate the index of the point in the new class:
public float GetIndexByXY( int x , int y , float tol)
{
for (idx = 0; idx < Point_X.Count; ++idx)
{
float dx = Point_X[idx] - x;
float dy = Point_Y[idx] - y;
float dist = (float)Math.Sqrt(dx * dx + dy * dy);
if (dist < tol) return idx;
}
return -1;
}
For example if i have one point and i click on it then variable(t) = 0
Then the List cyclicSelectedIndex have two cells/places in [0] i have 0 and in [1] i have -1 and currentCyclicIndex is now 1 and selectedIndex is 0 thats in Form1 in mouse down event. The mouse down event is just to mark wich point i want to delete.
In Form1 button click where i click to the delete the point:
private void button3_Click(object sender, EventArgs e)
{
wireObject1.DeletePoint(cyclicSelectedIndex[0],cyclicSelectedIndex[1]);
button3.Enabled = false;
pictureBox1.Invalidate();
}
cyclicSelectedIndex[0] = 0 and cyclicSelectedIndex[1] = -1
So in the new class in the DeletePoint function:
public void DeletePoint(int x, int y)
{
for (int i = 0; i < Point_X.Count; i++)
{
if ((Point_X[i] == x) && (Point_Y[i] == y) || y == -1 || x == -1)
{
Point_X.RemoveAt(i);
Point_Y.RemoveAt(i);
}
else
{
}
}
}
x = 0 and y = -1
Now i need to remove the index 0 and -1 from the Lists: Point_x and Point_Y and each index in this Lists contain a coordinate of hte point i want to delete.
Most likely you want to compare values of elements to x/y, not indexes:
Note it may be better to use class (struct if you know/have good reason) with X/Y properties and store them in one list: