次の方法で共有


Control.Name プロパティ

コントロールの名前を取得または設定します。

Public Property Name As String
[C#]
public string Name {get; set;}
[C++]
public: __property String* get_Name();public: __property void set_Name(String*);
[JScript]
public function get Name() : String;public function set Name(String);

プロパティ値

コントロールの名前。既定値は空の文字列 ("") です。

解説

Name プロパティを実行時に使用すると、型やプログラム上の名前ではなく、名前でオブジェクトを評価できます。 Name プロパティは String 型を返すため、case スタイル論理ステートメント (Visual Basic では Select ステートメント、C# および C++ では switch ステートメント) で評価されます。

使用例

 
' 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 handling methods.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' Connect the ControlRemoved and ControlAdded event handlers to the event handling methods.
    ' ControlRemoved and ControlAdded are not available at design time.
    AddHandler Me.ControlRemoved, AddressOf Me.Control_Removed
    AddHandler Me.ControlAdded, AddressOf Me.Control_Added
End Sub 'Form1_Load


Private Sub Control_Added(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs)
    MessageBox.Show(("The control named " + e.Control.Name + " has been added to the form."))
End Sub


Private Sub Control_Removed(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs)
    MessageBox.Show(("The control named " + e.Control.Name + " has been removed from the form."))
End Sub


' Click event handler for a Button control. Adds a TextBox to the form.
Private Sub addControl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
    ' Create a new TextBox control and add it to the form.
    Dim textBox1 As New TextBox()
    textBox1.Size = New Size(100, 10)
    textBox1.Location = New 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.
    Me.Controls.Add(textBox1)
End Sub


' Click event handler for a Button control.
' Removes the previously added TextBox from the form.
Private Sub removeControl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button2.Click
    ' Loop through all controls in the form's control collection.
    Dim tempCtrl As Control
    For Each tempCtrl In Me.Controls
        ' Determine whether the control is textBox1,
        ' and if it is, remove it.
        If tempCtrl.Name = "textBox1" Then
            Me.Controls.Remove(tempCtrl)
        End If
    Next tempCtrl
End Sub

[C#] 
// 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 handling methods.
private void Form1_Load(object sender, System.EventArgs e)
{
    // Connect the ControlRemoved and ControlAdded event handlers
    // to the event handling methods.
    // ControlRemoved and ControlAdded are not available at design time.
    this.ControlRemoved += new System.Windows.Forms.ControlEventHandler(this.Control_Removed);
    this.ControlAdded += new System.Windows.Forms.ControlEventHandler(this.Control_Added);
}

private void Control_Added(object sender, System.Windows.Forms.ControlEventArgs e)
{
    MessageBox.Show("The control named " + e.Control.Name + " has been added to the form.");
}

private void Control_Removed(object sender, System.Windows.Forms.ControlEventArgs e)
{
    MessageBox.Show("The control named " + e.Control.Name + " has been removed from the form.");
}

// Click event handler for a Button control. Adds a TextBox to the form.
private void addControl_Click(object sender, System.EventArgs e)
{
    // Create a new TextBox control and add it to the form.
    TextBox textBox1 = new TextBox();
    textBox1.Size = new Size(100,10);
    textBox1.Location = new 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.
private void removeControl_Click(object sender, System.EventArgs e)
{
    // Loop through all controls in the form's control collection.
    foreach (Control tempCtrl in this.Controls)
    {
        // Determine whether the control is textBox1,
        // and if it is, remove it.
        if (tempCtrl.Name == "textBox1")
        {
            this.Controls.Remove(tempCtrl);
        }
    }
}

[C++] 
// 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 handling methods.
private:
void Form1_Load(Object* /*sender*/, System::EventArgs* /*e*/)
{
   // Connect the ControlRemoved and ControlAdded event handlers
   // to the event handling methods.
   // ControlRemoved and ControlAdded are not available at design time.
   this->ControlRemoved += new System::Windows::Forms::ControlEventHandler(this, &Form1::Control_Removed);
   this->ControlAdded += new System::Windows::Forms::ControlEventHandler(this, &Form1::Control_Added);
}

void Control_Added(Object* /*sender*/, System::Windows::Forms::ControlEventArgs* e)
{
   MessageBox::Show(String::Format( S"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( S"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 = new 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 = S"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 = __try_cast<Control*>(myEnum->Current);
      // Determine whether the control is textBox1,
      // and if it is, remove it.
      if (tempCtrl->Name->Equals(S"textBox1"))
      {
         this->Controls->Remove(tempCtrl);
      }
   }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

Control クラス | Control メンバ | System.Windows.Forms 名前空間 | ISite.Name