none
Delete selected controls in Winforms C# RRS feed

  • Question

  • I need to click on a button, "select" that button, and by clicking on an erase button, that control will be deleted. How could I do that ?.
    **Up da te**

    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.


    • Edited by SebGM2018 Friday, May 25, 2018 6:19 PM
    Friday, May 25, 2018 4:01 PM

All replies

  • You wouldn't want the user to click the button, because that would trigger the normal "click" action for that button.  You couldn't tell the difference between "do the action" and "I'm preparing to delete".

    You are probably going to need to have a "mode" for this.  What I mean is, click one button to go into "select" mode, then use a rubber-band rectangle (available on the web) to select which control or controls to affect.  Then after controls are selected, pick "Delete".

    Alternative, you could have them click "Delete" first, then have them choose the control to delete.  That would require a little extra code in your app so that the Click handler has something like

        if (we are in 'delete' mode )
        {
            go delete this control;
        }
        else
        {
            take the regular click action;
        }


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Friday, May 25, 2018 5:19 PM
  • Are all the following different or is any of these the same? It is not clear.

    • button you click
    • an erase button
    • the control to delete


    Sam Hobbs
    SimpleSamples.Info

    Friday, May 25, 2018 5:33 PM
  • Yes, 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.
    Friday, May 25, 2018 6:17 PM
  • please, check the update
    Friday, May 25, 2018 6:18 PM
  •   Control CC = new Control();
    
            private void button1_Click(object sender, EventArgs e)
            {
                CC = (Control)sender;
            }
    
            private void buttonHide_Click(object sender, EventArgs e)
            {
                CC.Visible = false;
            }
    Project : https://drive.google.com/open?id=1pjy7zJRPUsW3vPO11X1BKI7bjKeF5FEp
    • Edited by Ahmed_Soft3 Saturday, May 26, 2018 12:10 AM
    • Proposed as answer by Ahmed_Soft3 Monday, May 28, 2018 10:55 PM
    Saturday, May 26, 2018 12:09 AM
  • One way to do it is that when you are in selection mode you can show a totally different window; one that looks the same but actually is different. You can have two user controls; one with all the controls but another that is nothing but an image. You can disable and hide one user control and enable and show another, based on the mode. It is probably possible to do it in such a way as to give the impression that it is all one window. You can create the image at design time or during execution; I think during execution is better. You can get an image (screen shot) of the form to create the image. You can also get the location and sizes of the relevant controls and when the image is clicked on you can determine if the click is for a control that is relevant.

    That sounds like a lot of programming and it might be but I think it could work quite well. I have not done much of that so I can't help with the details. It would be better to create new questions for specific pieces, the parts you need help with.



    Sam Hobbs
    SimpleSamples.Info

    Saturday, May 26, 2018 12:36 AM
  • It's not that hard.  You'll need something like a List<Control> to hold the list of controls.  Then, in all of the Click handlers, use code like I had above:

        if( delete_mode )
             controls_to_delete.Add( this );
        else {
             do regular click action;

    Then, when they click the Delete button, you just run through the controls_to_delete list and hide them.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Saturday, May 26, 2018 6:35 AM
  • Hi SebGM2018,

    Thank you for posting here.

    For your question, if you want to select the button which you click on, you need to define a click event to get the sender parameter to know the control name.

    When I click Delete button, the selected control will be deleted.

      private void btnDelete_Click(object sender, EventArgs e)
            {
                this.Controls.Remove(ctr);
            }
    
            Control ctr;
            public void MyClick(object sender)
            {
                ctr = (Control)sender;
            }
    
            private void textBox1_Click(object sender, EventArgs e)
            {
                MyClick(sender);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                MyClick(sender);
            }
    
            private void checkBox1_Click(object sender, EventArgs e)
            {
                MyClick(sender);
            }
    
            private void listBox1_Click(object sender, EventArgs e)
            {
                MyClick(sender);
            }
    
            private void label1_Click(object sender, EventArgs e)
            {
                MyClick(sender);
            }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 28, 2018 9:28 AM
    Moderator