2009-09-30 4 views
1

VB.NET을 사용하여 Visual Studio 2008을 사용하고 있습니다.vb.net에서 프로그래밍 방식으로 레이블 이름 바꾸기

프로그래밍 방식으로 이름을 바꾸려는 레이블이 여러 개 있습니다.

레이블의 현재 이름은 label100, label101의 형태를 취할 label102 등

그것은 너무 간단 소리를하지만 나를 이길 수있어이 가능합니까?

+0

레이블의 이름을 바꾸려고하십니까? –

+0

아, 미안하다. 이름은 player100 player101 player102 등의 형태를 취할 것입니다. – simon

+0

정규 표현식을 대체 할 것을 제안하지만 두 가지 문제가 있습니다. –

답변

1

label.Name 속성을 사용하여 레이블 이름을 변경할 수 있습니다.

변수의 선언의 이름이 이 아니므로 찾기/바꾸기를 사용해야합니다. MSDN 도움말에서

예 :

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-handler 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 
+0

label.Name 속성에 익숙하지 않습니다. 나는 당신이 어떻게 내가 그것을 사용할 것인지에 대한 예를 들기에 충분할만큼 친절한 가라고 생각합니다. 고마워요 – simon

+0

나는 그가 런타임에 코드에서 변경된 이름을 원한다고 생각합니다. –

+0

그렇습니다. 정확히 말하자면, 가능하면 코드를 한 번만 실행하여 이름을 변경 한 다음 앱에서 삭제할 수 있습니다. – simon

1

찾기 및 바꾸기 대화 상자가 가장 좋은 것입니다.

관련 문제