How can i check that if i clicked/selected two points which are connected with a line between...

  • Thread starter Thread starter Chocolade1972
  • Start date Start date
C

Chocolade1972

Guest
I have this code of pictureBox mouse down evet.

I added/drawed 6 points on the pictureBox.

Then clicked and connected with a line each two points. Now i want to do that when i click with the mouse on a point and then click on another point which is connected to the first clicked one button4 will be enabled true. ( button4 allow me to delete a connection between two points ).


The problem is to check when i clicked on a point and then on a second point which are connected between them.


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))
// To check why when clicking on connected point with al ine its blinking enable true the button 2 for milisecond !!!!!
// Still connecting points dosent work good yet sometimes need to click twice in a row to delete a line and to check all possible ways and bugs.
{
button2.Enabled = false;
}
else
{
button2.Enabled = true;
}

for (int i = 0; i < wireObject1._connectionstart.Count; i++) // To check why wireObject1._connectionstart.Count List is empty ! where do i add something to list if at all ?! A bug !!!!!
{
if ((wireObject1._connectionstart == cyclicSelectedIndex[0] && wireObject1._connectionend == cyclicSelectedIndex[1]) ||
(wireObject1._connectionstart == cyclicSelectedIndex[1] && wireObject1._connectionend == cyclicSelectedIndex[0]) ||
wireObject1._connectionstart == cyclicSelectedIndex[0] || wireObject1._connectionend == cyclicSelectedIndex[1] ||
wireObject1._connectionstart == cyclicSelectedIndex[1] || wireObject1._connectionend == cyclicSelectedIndex[0])
{

button3.Enabled = false;
}

if ((wireObject1._connectionstart == cyclicSelectedIndex[0] && wireObject1._connectionend == cyclicSelectedIndex[1]) ||
(wireObject1._connectionstart == cyclicSelectedIndex[1] && wireObject1._connectionend == cyclicSelectedIndex[0]))
{
button2.Enabled = false; /// Dosent delete a connection when for example 3 point are connected with 3 lines like a tringale.
/// Also to make that when there are connections between points more then 2 like 3 points connected if i click on two connected points to turn on button4.
}

if ((wireObject1._connectionstart == cyclicSelectedIndex[0]))
{
label8.Text = "First point clicked/selected";

}

if (wireObject1._connectionend == cyclicSelectedIndex[1])
{
label20.Text = "Second point clicked/selected";
}
}

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


// to make that when clicking/selecting two connected points button4 will be enabled so i can delete this connection between the two selected points.
// today i can delete only connection between two connected points only after clicked on connect points button.
// when clicking the connect points button its enabling the button4 but i need to make also if i selected/clicked on two connected points and two connected points
// it will enable button4 too ! to check how to do it.
}
}
}
}
}
}


wireObject1 is just a variable of the class WireObject.

wireObject1.GetIndexByXY find the closest index of the point center. So only if click close enough to the center the point will be selected so it will make sure i clicked/selected the point i wanted !


public float GetIndexByXY( int x , int y , float tol)
{
for (idx = 0; idx < woc.Point_X.Count; idx++)//++idx)
{
float dx = woc.Point_X[idx] - x;
float dy = woc.Point_Y[idx] - y;
float dist = (float)Math.Sqrt(dx * dx + dy * dy);

if (dist < tol) return idx;

}
return -1;
}


cyclicSelectedIndex is List<int>


currentCyclicIndex is int


_connectionstart and _connectionsend each is List<int>


This is the button click event which add a line connection between two points :


private void button2_Click(object sender, EventArgs e)
{
wireObject1.addConnection(cyclicSelectedIndex[0], cyclicSelectedIndex[1]);
button4.Enabled = true;
for (int i = 0; i < wireObject1._connectionstart.Count; i++)
{
if ((wireObject1._connectionstart == cyclicSelectedIndex[0] && wireObject1._connectionend == cyclicSelectedIndex[1]) ||
(wireObject1._connectionstart == cyclicSelectedIndex[1] && wireObject1._connectionend == cyclicSelectedIndex[0]) ||
wireObject1._connectionstart == cyclicSelectedIndex[0] || wireObject1._connectionend == cyclicSelectedIndex[1] ||
wireObject1._connectionstart == cyclicSelectedIndex[1] || wireObject1._connectionend == cyclicSelectedIndex[0])
{
button3.Enabled = false;
button2.Enabled = false;
}
}
addFrame = true;
pictureBox1.Refresh();

}


This is the addConnection in WireObject:


public void addConnection( int i, int j)
{
connectionStart.Add(i);
connectionEnd.Add(j);
}


This is button4 click event that should remove a line that connecting two points:


private void button4_Click(object sender, EventArgs e)
{

// to check when clicking on two points any two connected points the delete connection is off cant click on it.
// to check why its off and make sure and check that if i clicked on any two connected point the delete connection button will be on
// whne clicked on Lock button and also on Start New Animation button the delete/add connection buttons will be on and work as regular.


int t = wireObject1._connectionstart.Count;
wireObject1.DeleteConnection(cyclicSelectedIndex[0], cyclicSelectedIndex[1]); // for some reason i need to click twice or three times to remove a line.
// could be something wrong in the new class side to check it out.
button4.Enabled = false;
/*if ((t - 1) != wireObject1._connectionend.Count)
{
throw new Exception("Another Bug !");
}*/

if (wireObject1._connectionstart.Count != wireObject1._connectionend.Count)
{
throw new Exception("Bug !");
}
addFrame = true;
pictureBox1.Invalidate(); /// To check that when clicking on third point delete connection still on and whne cliking on delete connection it does nothing.
/// // If clicking on two points connected withl ine then cliking on one point third point to enable false the delete connection since the third selected point
/// // is not connected to anything ! To check that only if a point is connected to something to active the button delete connection.
/// // To check how i did it with the button connect points !
}


public void DeleteConnection(int i, int j)
{
for (int f = 0; f < connectionStart.Count(); f++)
{
if ((connectionStart[f] == i && connectionEnd[f] == j) || (connectionStart[f] == j && connectionEnd[f] == i))
{

connectionStart.RemoveAt(f);
connectionEnd.RemoveAt(f);
return;
}
else
{
}
}


}


In General when i connect two points with a line ( draw line between the two points ) so i click first on two points select two points then click on button2 and a line is drawed between the two points i clicked/selected .

And right after i clicked on button2 then button4 is enabled true and i can remove this connection/line between the two points.

The problem is when i have for example 8 points each two are connected if i click/select two points that i didnt connect last so button4 is not enabled true.

Button4 enabled true only when i clicked on button2.

The problem is that i want that button4 will be enabled true also when i click/select any two connected points !

Continue reading...
 
Back
Top