private void RemoveNodeButton_Click(object sender, EventArgs e)
        {
            ResetEventHandler();
            layersPaintedControl.state_PainterForm_MouseDown = LayersPaintedControl.State_PainterForm_MouseDown.Other;
            IPosition nearestPoint = new Position_Connected_Edit(0, 0);
            PositionSetEdit_ImplementByICollectionTemplate positionSetEdit_ImplementByICollectionTemplate = new PositionSetEdit_ImplementByICollectionTemplate();

            positionSetEdit_ImplementByICollectionTemplate.AddPosition(nearestPoint);
            Layer_PositionSet_Square layer = new Layer_PositionSet_Square(positionSetEdit_ImplementByICollectionTemplate);

            MouseMovedRealPosition = delegate(IPosition position)
            {
                positionSetEdit_ImplementByICollectionTemplate.RemovePosition(nearestPoint);
                nearestPoint = m2m_NN.NearestNeighbor(position);
                positionSetEdit_ImplementByICollectionTemplate.AddPosition(nearestPoint);
                layer.SpringLayerRepresentationChangedEvent(layer);
                layersPaintedControl.Invalidate();
            };

            layer.VisibleInOptDlg = false;
            layer.Active          = true;
            layer.Name            = "Edited Figure";
            layer.SquareFrameDrawer.RectangleRadiusWidth = 8;
            layer.SquareFrameDrawer.LineWidth            = 1;
            layer.SquareFrameDrawer.LineColor            = Color.Red;
            layer.CenterPointCoordinate.Visible          = true;
            layer.CenterPointCoordinate.CoordinateFont   = new Font(layer.CenterPointCoordinate.CoordinateFont, FontStyle.Bold);

            if (currentLayer != null)
            {
                layers.Remove(currentLayer);
            }
            currentLayer = layer;
            layers.Add(layer);

            MouseLBottonCheckRealPosition = delegate(IPosition position)
            {
                //((IPositionSetEdit)selectedPositionSetEdit).RemovePosition(nearestPoint);
                if (selectedPositionSetEdit is IPositionSet_ConnectedEdit && nearestPoint is IPosition_Connected)
                {
                    ((IPositionSet_ConnectedEdit)selectedPositionSetEdit).RemovePosition_Connected((IPosition_Connected)nearestPoint);
                }
                else
                if (selectedPositionSetEdit is IPositionSetEdit)
                {
                    ((IPositionSetEdit)selectedPositionSetEdit).RemovePosition(nearestPoint);
                }
                else
                {
                    throw new Exception(error_position);
                }
                selectedPositionSetLayer.SpringLayerRepresentationChangedEvent(selectedPositionSetLayer);
                m2m_NN.Remove(nearestPoint);
                layersPaintedControl.Invalidate();
            };
        }
        private void SelectBotton_Click(object sender, EventArgs e)
        {
            layersPaintedControl.state_PainterForm_MouseDown = LayersPaintedControl.State_PainterForm_MouseDown.onDaggle;
            ResetEventHandler();
            //layersPainterForm.state_PainterForm_MouseDown = LayersPainterForm.State_PainterForm_MouseDown.Other;
            IPosition nearestPoint = new Position_Point(0, 0);
            PositionSetEdit_ImplementByICollectionTemplate positionSetEdit_ImplementByICollectionTemplate = new PositionSetEdit_ImplementByICollectionTemplate();

            positionSetEdit_ImplementByICollectionTemplate.AddPosition(nearestPoint);
            Layer_PositionSet_Square layer = new Layer_PositionSet_Square(positionSetEdit_ImplementByICollectionTemplate);

            MouseMovedRealPosition = delegate(IPosition position)
            {
                positionSetEdit_ImplementByICollectionTemplate.RemovePosition(nearestPoint);
                nearestPoint = m2m_NN.NearestNeighbor(position);
                positionSetEdit_ImplementByICollectionTemplate.AddPosition(nearestPoint);
                layer.SpringLayerRepresentationChangedEvent(layer);
                layersPaintedControl.Invalidate();
            };

            layer.VisibleInOptDlg = false;
            layer.Active          = true;
            layer.Name            = "Edited Figure";
            layer.SquareFrameDrawer.RectangleRadiusWidth = 8;
            layer.SquareFrameDrawer.LineWidth            = 1;
            layer.SquareFrameDrawer.LineColor            = Color.YellowGreen;
            layer.CenterPointCoordinate.Visible          = true;
            layer.CenterPointCoordinate.CoordinateFont   = new Font(layer.CenterPointCoordinate.CoordinateFont, FontStyle.Bold);

            //layer.Point.IsDrawPointBorder = true;
            //layer.Point.PointColor = Color.Red;
            //layer.Point.PointRadius = 3;
            //layer.PointCoordinate.CoordinateFont = new Font(layer.PointCoordinate.CoordinateFont, FontStyle.Bold);
            //layer.PointCoordinate.Visible = true;

            if (currentLayer != null)
            {
                layers.Remove(currentLayer);
            }
            currentLayer = layer;
            layers.Add(layer);
        }
        private void AddDoubleConnectionButton_Click(object sender, EventArgs e)
        {
            selectedNode = null;
            layersPaintedControl.state_PainterForm_MouseDown = LayersPaintedControl.State_PainterForm_MouseDown.onDaggle;
            ResetEventHandler();
            IPosition nearestPoint = new Position_Connected_Edit(0, 0);
            PositionSetEdit_ImplementByICollectionTemplate positionSetEdit_ImplementByICollectionTemplate = new PositionSetEdit_ImplementByICollectionTemplate();

            positionSetEdit_ImplementByICollectionTemplate.AddPosition(nearestPoint);
            Layer_PositionSet_Square layer = new Layer_PositionSet_Square(positionSetEdit_ImplementByICollectionTemplate);

            MouseMovedRealPosition = delegate(IPosition position)
            {
                positionSetEdit_ImplementByICollectionTemplate.RemovePosition(nearestPoint);
                nearestPoint = m2m_NN.NearestNeighbor(position);
                positionSetEdit_ImplementByICollectionTemplate.AddPosition(nearestPoint);
                layer.SpringLayerRepresentationChangedEvent(layer);
                layersPaintedControl.Invalidate();
            };

            layer.VisibleInOptDlg = false;
            layer.Active          = true;
            layer.Name            = "Edited Figure";
            layer.SquareFrameDrawer.RectangleRadiusWidth = 8;
            layer.SquareFrameDrawer.LineWidth            = 1;
            layer.SquareFrameDrawer.LineColor            = Color.YellowGreen;
            layer.CenterPointCoordinate.Visible          = true;
            layer.CenterPointCoordinate.CoordinateFont   = new Font(layer.CenterPointCoordinate.CoordinateFont, FontStyle.Bold);

            if (currentLayer != null)
            {
                layers.Remove(currentLayer);
            }
            currentLayer = layer;
            layers.Add(layer);

            MouseLBottonCheckRealPosition = delegate(IPosition position)
            {
                if (selectedNode == null)
                {
                    selectedNode = nearestPoint;
                    positionSetEdit_ImplementByICollectionTemplate.AddPosition(selectedNode);
                }
                else
                {
                    if (selectedNode != nearestPoint)
                    {
                        float x1 = selectedNode.GetX(), y1 = selectedNode.GetY(), x2 = nearestPoint.GetX(), y2 = nearestPoint.GetY();
                        float distance = (float)Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
                        if (selectedNode is IPosition_Connected_Edit)
                        {
                            ((IPosition_Connected_Edit)selectedNode).GetAdjacencyPositionSetEdit().AddAdjacency((IPosition_Connected)nearestPoint, distance);
                        }
                        else
                        {
                            throw new Exception(error_connection);
                        }
                        if (nearestPoint is IPosition_Connected_Edit)
                        {
                            ((IPosition_Connected_Edit)nearestPoint).GetAdjacencyPositionSetEdit().AddAdjacency((IPosition_Connected)selectedNode, distance);
                        }
                        else
                        {
                            throw new Exception(error_connection);
                        }
                        positionSetEdit_ImplementByICollectionTemplate.RemovePosition(selectedNode);
                        selectedNode = null;
                    }
                }

                selectedPositionSetLayer.SpringLayerRepresentationChangedEvent(selectedPositionSetLayer);
                layersPaintedControl.Invalidate();
            };
        }
        public IPosition GetMouseDoubleChickedNearestPositionInCurrentPositionSet(IPositionSet currentPositionSet)
        {
            M2M_NN m2m_NN = new M2M_NN();

            m2m_NN.PreProcess(currentPositionSet);

            layersPaintedControl.state_PainterForm_MouseDown = LayersPaintedControl.State_PainterForm_MouseDown.onDaggle;
            ResetEventHandler();
            //layersPainterForm.state_PainterForm_MouseDown = LayersPainterForm.State_PainterForm_MouseDown.Other;
            IPosition nearestPoint = new Position_Connected_Edit(0, 0);
            PositionSetEdit_ImplementByICollectionTemplate positionSetEdit_ImplementByICollectionTemplate = new PositionSetEdit_ImplementByICollectionTemplate();

            positionSetEdit_ImplementByICollectionTemplate.AddPosition(nearestPoint);
            Layer_PositionSet_Square layer = new Layer_PositionSet_Square(positionSetEdit_ImplementByICollectionTemplate);

            MouseMovedRealPosition = delegate(IPosition position)
            {
                positionSetEdit_ImplementByICollectionTemplate.RemovePosition(nearestPoint);
                nearestPoint = m2m_NN.NearestNeighbor(position);
                positionSetEdit_ImplementByICollectionTemplate.AddPosition(nearestPoint);
                layer.SpringLayerRepresentationChangedEvent(layer);
                layersPaintedControl.Invalidate();
            };

            layer.VisibleInOptDlg = false;
            layer.Active          = true;
            layer.Name            = "Edited Figure";
            layer.SquareFrameDrawer.RectangleRadiusWidth = 8;
            layer.SquareFrameDrawer.LineWidth            = 1;
            layer.SquareFrameDrawer.LineColor            = Color.Red;
            layer.CenterPointCoordinate.Visible          = true;
            layer.CenterPointCoordinate.CoordinateFont   = new Font(layer.CenterPointCoordinate.CoordinateFont, FontStyle.Bold);


            System.Threading.Thread currentThread            = System.Threading.Thread.CurrentThread;
            Position_Connected_Edit mouseChickedRealPosition = null;
            IPosition nearestPositionInCurrentPositionSet    = null;

            this.BeginInvoke(new dShow(MessageBox.Show), new object[] { "please double chick on the screen." });

            MouseDoubleChickOnLayersPaintedControl += delegate(object sender, MouseEventArgs e)
            {
                mouseChickedRealPosition = new Position_Connected_Edit();
                mouseChickedRealPosition.SetX(layersPaintedControl.ConvertMouseXToRealX(e.X));
                mouseChickedRealPosition.SetY(layersPaintedControl.ConvertMouseYToRealY(e.Y));

                nearestPositionInCurrentPositionSet = m2m_NN.NearestNeighbor(mouseChickedRealPosition);

                if ((currentThread.ThreadState & System.Threading.ThreadState.Suspended) == System.Threading.ThreadState.Suspended)
                {
                    currentThread.Resume();
                }
            };

            if (currentLayer != null)
            {
                layers.Remove(currentLayer);
            }
            currentLayer = layer;
            layers.Add(layer);

            currentThread.Suspend();
            MouseDoubleChickOnLayersPaintedControl = null;
            return(nearestPositionInCurrentPositionSet);
        }