Question Deleting drawn figures

Joined
May 25, 2020
Messages
8
Location
Sofia, Bulgaria
Programming Experience
Beginner
@jmcilhinney , @Skydiver , @Sheepings

Can I ask a question for which I have a code, but I don't like how it works аnd I would like to improve it. It is about deleting the already drawn figures. Because every time I have to press the "Remove" button before selecting the shape I want to delete. Is there a way to mark several shapes instead of individually as I did?

C#:
private List<Shapes> _shapes = new List<Shapes>();
        private Shapes _s = null;
        private bool optionMove = false;
        private bool optionRemove = false;
        private bool optionCalculate = false;

///

C#:
private void removeBtn_Click(object sender, EventArgs e)
        {
            label1.Text = "Click on shape to remove it.";
            optionMove = false;
            optionRemove = true;
            optionCalculate = false;
            Form2.triangle = false;
            Form2.rectangle = false;
            Form2.circle = false;

///

C#:
if (e.Button == MouseButtons.Left)
            {
                _s = _shapes.Where(shape => shape.ConteinsCordinate(e.Location)).FirstOrDefault();
                if (optionRemove == true)
                {
                    _shapes.RemoveAll(removeshape => removeshape.ConteinsCordinate(e.Location));
                    Invalidate();
                }
                optionRemove = false;
            }
 
It is about deleting the already drawn figures
But the title of this thread is:
Change the color of the drawn figures
That's two relatively unrelated topics, which means that this new question belongs in a new thread with a relevant title and just the information relevant to the new issue. On thread per topic and one topic per thread.
 
I've moved the new question to a new thread.
 
Can we assume that the code that draws the shapes uses the _shapes list?

Later as you advance through your programming skills, you will encounter the Command pattern which is the generally accepted way to deal with situations like this. But design patterns are kind of esoteric until you've got enough experience under your belt.

So to the issue at hand: you seem to already have a basic notion of hit testing -- you are determining which shape the user clicked on by checking to see if the click point is within the confines of the shape. Good! So what you need to do is have a list of selected items. For the sake of discussion let's name it _selectedShapes. I think that you already have _s which was meant to to signify the current selected item. Perhaps change _s to be a list of shapes rather than a single shape (and give it a better name like _selectedShapes). You populate this list by adding a new item each time the user clicks on a shape. Then when the user clicks on your "Remove" button, you can simply iterate over this list and remove each item found from the _shapes list. Then invalidate the control/form and only the surviving shapes will be drawn.

If you don't want to iterate over the selected items, and you are a big believer in LINQ (which to be that case with your various uses of LINQ extension methods), then you can likely use the Except() extension method to simplify getting a new shapes list without the selected shapes.
 
Back
Top Bottom