Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Rename Labels to Text Content Plus lbl

0.00/5 (No votes)
23 Mar 2016CPOL1 min read 6.8K  
Quickly rename thousands of labels in a Designer File without manual intervention

Introduction

Have you ever worked on a large project in VB.NET and found that there were hundreds. if not thousands of Labels that have the name Label#?

Background

I have been working on a large project where a single form may have thousands of labels. Normally, I never really cared about naming labels unless I was going to use them in the code. However, looking through the control list shows that this isn't a very good habit. Faced with the prospect of manually naming thousands of labels, I knew there was a better way.

This is that better way.

Using the Code

Image 1

This code will go through a Designer File and rename Labels based on their Text attribute. The text has spaces and colons removed and then has lbl affixed to the front. It won't rename a label if that label already exists, so having the same text on two labels will only cause the first one to be renamed. After that, it's manual work.

VB.NET
Public Class frmMain
    Private Sub cmdOpenFile_Click(sender As Object, e As EventArgs) Handles cmdOpenFile.Click
        Dim dlgOpenFile As New OpenFileDialog
        dlgOpenFile.Filter = "Designer Files|*.Designer.vb"
        dlgOpenFile.Multiselect = False
        If dlgOpenFile.ShowDialog = DialogResult.OK Then
            txtFileName.Text = dlgOpenFile.FileName
        End If
    End Sub

    Private Sub cmdRenameLabels_Click(sender As Object, e As EventArgs) Handles cmdRenameLabels.Click
        Check_For_DefaultLabels()
    End Sub

    Dim strArray() As String
    Private Sub Check_For_DefaultLabels()
        strArray = IO.File.ReadAllLines(txtFileName.Text)
        For i As Integer = 0 To strArray.Count - 1
            If strArray(i).Contains("Me.Label") And strArray(i).Contains(".Text = ") Then
                Dim strSplit() As String = strArray(i).Split(".")
                Dim LabelName As String = strSplit(1)
                Dim LabelNewName As String = strSplit(2).Replace("""", "").Replace(":",
                                            "").Replace("Text = ", "").Replace(" ", "")
                If Check_For_Duplicates(LabelNewName) = False Then
                    Rename_Label(LabelName, LabelNewName)
                End If
            End If
        Next
        IO.File.WriteAllLines(txtFileName.Text, strArray)
    End Sub

    Private Sub Rename_Label(oldname As String, newname As String)
        For i As Integer = 0 To strArray.Count - 1
            If strArray(i).Contains(String.Format("Me.{0} = New System.Windows.Forms.Label()",
                                                            oldname)) Then
                strArray(i) = strArray(i).Replace(String.Format("Me.{0} = New
                                System.Windows.Forms.Label()", oldname), 
                                String.Format("Me.lbl{0} = 
                                New System.Windows.Forms.Label()", newname))
            End If
            If strArray(i).Contains(String.Format("Me.{0}.Name = ""{0}""", oldname)) Then
                strArray(i) = strArray(i).Replace(String.Format("Me.{0}.Name = ""{0}""", 
                                oldname), String.Format("Me.lbl{0}.Name = 
                                ""lbl{0}""", newname))
            End If
            If strArray(i) = String.Format("        '{0}", oldname) Then
                strArray(i) = String.Format("        'lbl{0}", newname)
            End If
            If strArray(i).Contains(String.Format("Me.{0}.", oldname)) Then
                strArray(i) = strArray(i).Replace(String.Format("Me.{0}.", oldname),
                                String.Format("Me.lbl{0}.", newname))
            End If
            If strArray(i).Contains(String.Format("Me.Controls.Add(Me.{0})", oldname)) Then
                strArray(i) = strArray(i).Replace(String.Format("Me.Controls.Add(Me.{0})",
                            oldname), String.Format("Me.Controls.Add(Me.lbl{0})", newname))
            End If
            If strArray(i).Contains(String.Format("Friend WithEvents {0} As Label", oldname)) Then
                strArray(i) = strArray(i).Replace(String.Format("Friend WithEvents {0} As Label",
                        oldname), String.Format("Friend WithEvents lbl{0} As Label", newname))
            End If
        Next
    End Sub

    Private Function Check_For_Duplicates(strCheck As String) As Boolean
        Dim boolDuplicate As Boolean = False
        For Each Line In strArray
            If Line.Contains(String.Format("Me.{0} = New System.Windows.Forms.Label()", strCheck))
                                                    Then boolDuplicate = True
            If Line.Contains(String.Format("Me.{0}.Name = ""{0}""", strCheck)) 
                                                    Then boolDuplicate = True
            If Line = String.Format("        '{0}", strCheck) Then boolDuplicate = True
            If Line.Contains(String.Format("Me.{0}.", strCheck)) Then boolDuplicate = True
            If Line.Contains(String.Format("Me.Controls.Add(Me.{0})", strCheck)) 
                                                    Then boolDuplicate = True
            If Line.Contains(String.Format("Friend WithEvents {0} As Label", strCheck)) 
                                                    Then boolDuplicate = True
            If boolDuplicate = True Then Exit For
        Next
        Return boolDuplicate
    End Function
End Class

Just create a Form, name it frmMain. Put a Label, TextBox, and 2 Buttons on the form.

Label Name: lblDesignerFile
Label Text: Designer File
TextBox Name: txtFileName
TextBox Text:
Button Name: cmdOpenFile
Button Text: ...
Button Name: cmdRenameLabels
Button Text: Rename Labels

Enjoy not having to manually rename the Labels.

Please note I put this together in maybe an hour, so there is room for improvement.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)