There seems a bit a problem with approach. You need to treat all the things as objects, but once You draw them, the information (of their location) is lost.
What to do?
Create class structure describing Your Actor
, UseCase
and Relations
between them. It depends on Your concept where some time should be spent to create it. Each of this objects will have parameters like Location
, Name
and some more info (e.g. for drawing).
On program start You will create a collection, where You will hold all this objects. Once user select the object and click to delete it, You will remove it from the collection.
On any change in collection You will just refresh the location of drawing, which will redraw all objects from the collection.
Note: I will put this separated into files (first line) so You get a general idea how to structure it. Namespaces/usings omitted for brevity.
Actor.cs:
public class Actor
{
public int X { get; set; }
public int Y { get; set; }
public string Name { get; set; }
public List<UseCase> UseCases { get; set; } //keeping UseCases as reference - relation
public Actor() { UseCases = new List<UseCase>(); }
}
UseCase.cs:
public class UseCase
{
public int X { get; set; }
public int Y { get; set; }
public string Name { get; set; }
}
Form1.cs (code-file):
public class Form1 : Form
{
public List<Actor> Actors = new List<Actor>();
public void Add()
{
//creating new actor based on input + adding to collection
var lAct = new Actor() { X = 10, Y = 20, Name = "Actor1" };
Actors.Add(lAct);
NotifyRedraw();
}
public void Remove(string aName)
{
var lAct = Actors.FirstOrDefault( a => a.Name == aName);
if (lAct != null)
Actors.Remove(lAct);
NotifyRedraw();
}
public void NotifyRedraw()
{
this.panel1.Refresh();
}
/* call methods above from clicking the buttons*/
public void Draw(Graphics g)
{
g.Clear(); //clear the graphics to make fresh drawing
foreach (Actor a in Actors)
{
g.DrawCircle( /* implementation of drawing Actor */);
foreach(UseCase u in a)
{
g.DrawRectangle( /* implementation of drawing UseCase */ );
g.DrawLine(Pens.Red, a.X, a.Y, u.X, u.Y); //implementation of drawing Relation
}
}
}
}
If You want to follow more deeply the OOP approach, You should actually not call g.DrawSth
from the "General" method, but from each object (so each object know how to draw itself).
public class Actor
{
// .... implementation of above properties
private const int cSIZE = 10;
public void Draw(Graphics g)
{
//implementation of drawing for object
g.DrawCircle(Brushes.Blue, this.X, this.Y, cSIZE, cSIZE);
}
}
From Form1
class and Draw()
method, You will call:
public void Draw(Graphics g)
{
foreach(Actor a in Actors)
a.Draw(g);
}