-4

i need that when you click a garbage button, the "Selection mode" turn on (A bool variable became true) and when you click that control (let's say a button) the application "selects it" and with another button, (Ok button) the application should knew Wich controls you clicked, and then, delete them, the problem i Have is in the recognize witch controls you selected update For "selection" I mean that the app knows which control you clicked

SebGM2018
  • 3
  • 5

2 Answers2

0

You can implement the required functionality as follows. The idea is simple, to keep track of our last selection we have a "previousSelection" variable of type "Control" in our class. We can even use a list of type "Control" to keep track of multiple selections.

There's a toggle button to enable / disable "Garbage Mode" which maps to "IsGarbageModeEnabled" field of type "bool". Then we have an "InitControlsRecursiveMethod(ControlCollection collection)" which takes in a collection of controls, to which we would attach an event handler, which is "GenericClickHandler(Control c)" in our case. This handler simply updates the "previousSelection" on each button click.

And lastly we have our button "ConfirmDeletionBtn", upon clicking which, we would check whether "GarbageMode" is enabled or not, if it is, we would have some basic validation to check whether the control being deleted isn't our "Delete" or "GarbageModeToggle" button itself (this could cause trouble if user double clicks the 'Delete' button). Afterwards, it would remove / dispose the control that is to be deleted.

public partial class FormName : Form
{
    //To keep track of the previously selected control (i.e. to be deleted)
    private Control previousSelection { get; set; }

    //To keep track of whether "Garbage Mode" is enabled or disabled
    private bool IsGarbageModeEnabled { get; set; }

    //Constructor
    public FormName()
    {
        InitializeComponent();
        IsGarbageModeEnabled = false;
        previousSelection = new Control();

        //Attach a generic click handling event to each control to 
        //update "previousSelection" with each click.
        //Similar logic can be used for other events as well 
        //(e.g. GotFocus, which might even accomodate control selection via keyboard).
        InitControlsRecursive(this.Controls);
    }

    //This attaches the GenericClickHandler(Control c) to each control on the form.
    private void InitControlsRecursive(Control.ControlCollection collection)
    {
        foreach (Control c in collection)
        {
            c.MouseClick += (sender, e) => { GenericClickHandler(c); };
            InitControlsRecursive(c.Controls);
        }
    }

    //The generic click handling event we're using to update "previousSelection".
    private void GenericClickHandler(Control c)
    {
        previousSelection = c;
    }

    //By clicking the confirm deletion / OK button, we would delete the last selected control.
    private void ConfirmDeletionBtn_Click(object sender, EventArgs e)
    {
        if(IsGarbageModeEnabled == true)
        {
            if(previousSelection != ConfirmDeletionBtn || previousSelection != ToggleGarbageModeBtn)
            {
                this.Controls.Remove(previousSelection);
                previousSelection.Dispose();
            }
        }
    }

    //This is used to enable/disable Garbage Mode. Controls can be deleted only once it is enabled.
    private void ToggleGarbageModeBtn_Click(object sender, EventArgs e)
    {
        IsGarbageModeEnabled = !IsGarbageModeEnabled;
    }
}

Further Reading:

Mikaal Anwar
  • 1,720
  • 10
  • 21
  • *the application should knew Wich controls you clicked....For "selection" I mean that the app knows which control you clicked* So the OP wants to know which button***S*** were clicked, not which one is currently focused. – John Wu May 26 '18 at 01:13
  • @JohnWu The OP mentioned in his question that “The app knows about which ‘control’ was clicked” NOT buttonS, so that’s your own addition. Secondly, through simple logic, one can easily keep track of the previously selected “Control”, if the focus event is used, like I demonstrated. – Mikaal Anwar May 26 '18 at 02:46
  • @JohnWu Regardless, I have updated my answer, so you can remove your downvote now. – Mikaal Anwar May 26 '18 at 02:46
-1

Firstly you need somewhere to keep track of the controls that were "selected" (clicked). So add this to the form's codebehind:

List<Control> _itemsToDelete = new List<Control>();

And you need a flag to indicate whether the user has activated garbage mode:

bool _garbageMode = false;

To activate garbage mode:

async void GarbageMode_Click(object sender, EventArgs e)
{
    _garbageMode = true;
}

Now when they "select" a control you add it to the list:

async void Control_Click(object sender, EventArgs e)
{
    if (_garbageMode)
    {
        _itemsToDelete.Add((Control)sender);
    }
}

Then to delete

foreach (var control in _itemsToDelete)
{
    this.Controls.Remove(control);
    control.Dispose();
}
_itemsToDelete.Clear();
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • So by this logic, if there are 20 controls on a form, in order to make them ‘deleteable’, we have to modify the click event for each of them? – Mikaal Anwar May 26 '18 at 02:47
  • No, you could have a common click handler, which you could add on top of existing handlers, programmatically if you want. A control can have multiple event handlers, no problem. Even though there is only one handler, you can tell which control caused the event by casting the `sender`, as in the example. – John Wu May 26 '18 at 06:16
  • Thanks really much! But what if I want to deselect a control with another click? (Like if you already selected one, but you do a mistake and want to deselect that control with other click) – SebGM2018 May 27 '18 at 06:12
  • You'd just need to do something like `if (_itemsToDelete.Contains(sender)) _itemsToDelete.Remove(sender) else _itemsToDelete.Add(sender)` – John Wu May 27 '18 at 07:53