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.
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()
}
}