Expand Minimize

Control::Name Property

Gets or sets the name of the control.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[BrowsableAttribute(false)]
public:
property String^ Name {
	String^ get ();
	void set (String^ value);
}

Property Value

Type: System::String
The name of the control. The default is an empty string ("").

The Name property can be used at run time to evaluate the object by name rather than type and programmatic name. Because the Name property returns a String type, it can be evaluated in case-style logic statements (Select statement in Visual Basic, switch statement in Visual C# and Visual C++).

The following code example displays the Name of a control in a MessageBox when the control is added or removed from a form.

   // This example demonstrates the use of the ControlAdded and 
   // ControlRemoved events. This example assumes that two Button controls 
   // are added to the form and connected to the addControl_Click and 
   // removeControl_Click event-handler methods. 
private:
   void Form1_Load( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Connect the ControlRemoved and ControlAdded event handlers 
      // to the event-handler methods. 
      // ControlRemoved and ControlAdded are not available at design time. 
      this->ControlRemoved += gcnew System::Windows::Forms::ControlEventHandler( this, &Form1::Control_Removed );
      this->ControlAdded += gcnew System::Windows::Forms::ControlEventHandler( this, &Form1::Control_Added );
   }

   void Control_Added( Object^ /*sender*/, System::Windows::Forms::ControlEventArgs^ e )
   {
      MessageBox::Show( String::Format( "The control named {0} has been added to the form.", e->Control->Name ) );
   }

   void Control_Removed( Object^ /*sender*/, System::Windows::Forms::ControlEventArgs^ e )
   {
      MessageBox::Show( String::Format( "The control named {0} has been removed from the form.", e->Control->Name ) );
   }

   // Click event handler for a Button control. Adds a TextBox to the form. 
   void addControl_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Create a new TextBox control and add it to the form.
      TextBox^ textBox1 = gcnew TextBox;
      textBox1->Size = System::Drawing::Size( 100, 10 );
      textBox1->Location = Point(10,10);

      // Name the control in order to remove it later. The name must be specified 
      // if a control is added at run time.
      textBox1->Name = "textBox1";

      // Add the control to the form's control collection. 
      this->Controls->Add( textBox1 );
   }

   // Click event handler for a Button control. 
   // Removes the previously added TextBox from the form. 
   void removeControl_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Loop through all controls in the form's control collection.
      IEnumerator^ myEnum = this->Controls->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         Control^ tempCtrl = safe_cast<Control^>(myEnum->Current);

         // Determine whether the control is textBox1, 
         // and if it is, remove it. 
         if ( tempCtrl->Name->Equals( "textBox1" ) )
         {
            this->Controls->Remove( tempCtrl );
         }
      }
   }

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft