0

I'm writing dijkstra algorithm,i don't know how can I prevent from remove graphics drew on picturebox,i put a little bit code in here.in addition,i put picture for understand better.but as you see in the picture,part of circle picture is removed that's why i drag windows form.

enter image description here

   private List<Node> listNode = new List<Node>();
   private int counter = 0;
    private void DrawCircles(int nodeId)
    {
        Graphics g = picBoard.CreateGraphics();
        g.SmoothingMode = SmoothingMode.AntiAlias;
        Rectangle rect = new Rectangle(listNode[nodeId].Location.X - 12, listNode[nodeId].Location.Y - 12, 25, 25);
        Pen myPen = new Pen(Brushes.Black, 2);
        g.FillEllipse((listNode[nodeId].Selected ? listNode[nodeId].SelectedColor
            : listNode[nodeId].NormalColor), rect);
        g.DrawEllipse(myPen, rect);
        TextRenderer.DrawText(g, nodeId.ToString(), new Font("Tahoma", 8),
            rect, Color.Black,
            TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter);

        g.Dispose();
    }

    private void picBoard_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            Node node = new Node(counter, e.Location);
            listNode.Add(node);
            DrawCircles(counter);
            counter++;

            InitializeNodeList();
        }
    }

I founded another way

i put this code in DrawCircle() method this code cause no longer remove circle graphics on picturebox but this code has a problem that each time doubleClick remove before node.

       private void DrawCircles(int nodeId)
    {
         Bitmap bmp = new Bitmap(picBoard.Width, picBoard.Height);
        Graphics g = Graphics.FromImage(bmp);
        g.SmoothingMode = SmoothingMode.AntiAlias;
        Rectangle rect = new Rectangle(listNode[nodeId].Location.X - 12, listNode[nodeId].Location.Y - 12, 25, 25);
        Pen myPen = new Pen(Brushes.Black, 2);
        g.FillEllipse((listNode[nodeId].Selected ? listNode[nodeId].SelectedColor
            : listNode[nodeId].NormalColor), rect);
        g.DrawEllipse(myPen, rect);
        TextRenderer.DrawText(g, nodeId.ToString(), new Font("Tahoma", 8),
            rect, Color.Black,
            TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter);
        picBoard.Image = bmp;
    }
   private void picBoard_MouseDoubleClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        Node node = new Node(counter, e.Location);
        listNode.Add(node);
        DrawCircles(counter);
        counter++;

        InitializeNodeList();
    }
}
    private void InitializeNodeList()
    {
        cmbFromNode.Items.Clear();
        CmbToNode.Items.Clear();
        foreach (var node in listNode)
        {
            cmbFromNode.Items.Add(node.Id);
            CmbToNode.Items.Add(node.Id);
            // cmbWeight.Items.Add()
        }
    }
behnam
  • 1,095
  • 2
  • 11
  • 30
  • 1
    You should only draw in the Paint event. This will retain the graphics on resize and refresh. – Nico Schertler Sep 10 '16 at 15:03
  • @NicoSchertler, my question is updated. – behnam Sep 10 '16 at 15:19
  • Your DoubleClick doesn't update the nodeId, does it? – TaW Sep 10 '16 at 15:49
  • @TaW, nodeId is update, but i have a question , when i use this code (Bitmap bmp = new Bitmap(graphBoardPB.Image);) i get error :“Object reference not set to an instance of an object”, but when i use this code ( Bitmap bmp = new Bitmap(picBoard.Width, picBoard.Height);) i dont get error. – behnam Sep 10 '16 at 16:14
  • 1
    @alexshiro because `graphBoardPB.Image` is `null` the first time you access it and `picBoard` is not null, it just uses the dimentions of the control. See the question "[What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it)" – Scott Chamberlain Sep 10 '16 at 16:28
  • @Scott: I don't think that this is his real problem and it surely isn't his question! – TaW Sep 10 '16 at 17:44
  • _nodeId is updated_ Well, where? – TaW Sep 10 '16 at 17:44
  • @TaW, nodeId is updated in picBoard_MouseDoubleClick() event. – behnam Sep 10 '16 at 17:47
  • Well, wasn't the question just closed as a duplicate? – TaW Sep 10 '16 at 17:50
  • _nodeId is updated in picBoard_MouseDoubleClick()_ Either I am blind or you don't show us the real code. Also: `InitializeNodeList();` sounds alot like you are deleting previous nodes? – TaW Sep 10 '16 at 17:51
  • @TaW I thought you were referring to my comment, not my vote to close. This question is a simple duplicate of "I did not draw inside of the Paint Event, now when my window gets covered or minimized my stuff goes away". The one I linked to was the closest one I could find after 1 or 2 minutes of searching for a dupe. – Scott Chamberlain Sep 10 '16 at 17:51
  • @TaW, i added InitializeNodeList() and picBoard_MouseDoubleClick() code. – behnam Sep 10 '16 at 17:53
  • 1
    Ah, I see it now. Here is a way to fix it, I believe: `Bitmap bmp = graphBoardPB.Image == null ? new Bitmap(picBoard.Width, picBoard.Height) : new Bitmap(graphBoardPB.Image);` - This first checks if there already is an image and then Either uses it or creates it. - Btw: if picBoard is a Control with border you should refer not to its size but to its Clientsize! – TaW Sep 10 '16 at 17:58
  • @Scott, you were right, after all; sorry to have bothered you.. – TaW Sep 10 '16 at 18:04
  • @TaW,thanks a lot , it worked really well. – behnam Sep 10 '16 at 18:06

0 Answers0