Good day! Please tell me how to check the existence of a component on a form (c # language). It seems like a simple task, but it does not work.
Thank you in advance!

    3 answers 3

    I would recommend looking for the control recursively, otherwise as soon as you put the control in the nested container, your scheme will break.

    bool Find(Control resContainer, ControlCollection controls) { if (controls.Contains(resContainer)) return true; foreach (var control in controls) { if (this.Find(resContainer, control.Controls)) return true; } return false; } 
       if (this.Controls.Contains( someBtn )) { // TO DO } // или так if (this.Controls.ContainsKey( "someBtn" )) { // TO DO: } 

      Details here

      • one
        Do not have time)) - Mihey

      Already bounced himself.

       this.Controls.Contains(resContainer) 
      Register inside the form or specify it. resContainer - the item itself. Returns true if the element exists and false if it is not initialized.