private void node_Down(object sender, MouseEventArgs e)
 {
     if (canDraw)
     {
         node1 = (RoundButton)sender;
     }
 }
        private void panel1_Click(object sender, EventArgs e)
        {
            if (canDraw)
            {
                nrNodes++;
                Point  newLocation = new Point(clickX - sizeOfNode / 2, clickY - sizeOfNode / 2);
                string nameNode    = string.Format("node{0}", nrNodes);

                nodes[nrNodes] = new RoundButton
                {
                    Location  = newLocation,
                    Name      = string.Format(nameNode),
                    Text      = nrNodes.ToString(),
                    Size      = new Size(sizeOfNode, sizeOfNode),
                    FlatStyle = FlatStyle.Flat,
                    BackColor = Color.FromName("Aqua")
                };
                nodes[nrNodes].MouseDown += new MouseEventHandler(node_Down);
                nodes[nrNodes].MouseUp   += new MouseEventHandler(node_Up);

                panel1.Controls.Add(nodes[nrNodes]);

                Console.WriteLine("Node Location: " + newLocation);
            }
        }
 public edge(RoundButton node1, RoundButton node2, int newWeight) : this()
 {
     this.firstNode  = node1;
     this.secondNode = node2;
     this.weight     = newWeight;
 }
        private void node_Up(object sender, MouseEventArgs e)
        {
            if (canDraw)
            {
                Point upLocation = e.Location;
                upLocation.X += node1.Location.X;
                upLocation.Y += node1.Location.Y;

                //check if you released mouseBtn inside an existing node
                for (int i = 1; i <= nrNodes; ++i)
                {
                    int       trX      = nodes[i].Location.X;
                    int       trY      = nodes[i].Location.Y;
                    Rectangle currNode = new Rectangle(trX, trY, sizeOfNode, sizeOfNode);
                    if (currNode.Contains(upLocation))
                    {
                        node2 = nodes[i];
                        break;
                    }
                }

                //if you released mouseBtn inside an existing node2, different from node1
                if (node2 != null && node2 != node1)
                {
                    //check if an edge between node1 and node2 already exists
                    bool gtg = true;
                    for (int i = 0; i < edges.Count; i++)
                    {
                        if ((edges[i].firstNode == node1 && edges[i].secondNode == node2) ||
                            (edges[i].firstNode == node2 && edges[i].secondNode == node1))
                        {
                            gtg = false;
                        }
                    }
                    //if an edge between node1 and node2 doesn't already exist
                    if (gtg)
                    {
                        //generate an edge with a random weight
                        Random rnd       = new Random();
                        int    newWeight = rnd.Next(1, 100);
                        edge   newEdge   = new edge(node1, node2, newWeight);
                        //memorize the edge
                        edges.Add(newEdge);
                        //draw an edge between node1 and node2
                        DrawEdge(newEdge, Color.Black);

                        //update Form Edges with the newEdge
                        frmEdges.AddRow(newEdge);
                    }
                }
                //else, if you released mouseBtn in an empty space of the Drawing Area
                else if (panel1.Bounds.Contains(upLocation.X + panel1.Bounds.X, upLocation.Y + panel1.Bounds.Y))
                {
                    //move node1 to the new location
                    node1.Location = new Point(upLocation.X - sizeOfNode / 2, upLocation.Y - sizeOfNode / 2);
                    //and redraw the edges
                    redrawEdges(Color.WhiteSmoke);
                }

                //reset node1 and node2 to null
                node1 = node2 = null;
            }
        }