0

I am new in c# and Visual Studio (2015). I want to create new box but before that i want to check if there is a box with the name of new box of not. I am struggling with what command to use to check if text box exits. I tried TextBox.Exits but there is nothing in directory. Thank you

zain ali
  • 21
  • 4
  • 3
    Possible duplicate of [Get a Windows Forms control by name in C#](http://stackoverflow.com/questions/1536739/get-a-windows-forms-control-by-name-in-c-sharp) – 001 Apr 04 '17 at 13:04

1 Answers1

0

You can search for specific controls that exist in your current form using the Controls.Find method (see docs). This will either return the control(s) that have been found in a Control[] array and in case nothing has been found, it will return and empty Control[] array.

In you case you can check on the array. If it's empty you know there's no controls yet with the name you want to use!

var controlFound = this.Controls.Find(NameYouWantToUseAndCheck, true);

if(controlFound.Length == 0)
{
    // No existing control has been found with this name, you can safely make your new control
}
else
{
    // An existing control has been found with this name, use another name or do whatever you want!
}
Jurjen
  • 1,376
  • 12
  • 19