Is there any way to change all of the text inside a label to another character? It would still be the same length, but all of the text within the label would be the new character.

Recommended Answers

All 7 Replies

If you mean like changing "My Label" to "xxxxxxxx" then you could try

lblLabel.Text = Space(lblLabel.Text.Length).Replace(" ", "x")

Space(lblLabel.Text.Length) generates a string of blanks the same length as the label text and Replace(" ","x") replaces all of the blanks with the desired character.

You could use a readonly textbox then set the password character, by removing the border it looks just like a label.

I need to be able to change the text within the labels to a desired character. I can not use the password character, because the character is not consistant. It must also be able to ignore the spaces withing the label.

You could use regular expressions

Dim rex As New System.Text.RegularExpressions.Regex("[^ ]")
lblMylabel.Text = rex.Replace(lblMylabel.Text, "x")

The expression "[^ ]" matches any non-blank character. Rex.Replace scans the given string and replaces any matching string with the second string so basically it says replace any non-blank character with an x.

Or if you want each word to have a different letter mask You could use the split function, change each word to the character you want and use the join function to put it back together.

Or if you want a simple substitution cipher the Caesar cipher is easy to implement and will change each character to an offset character.

Here's examples of both:

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim OrigStr As String = "Test1 Test2 Test3"

        'Using Split

        Dim MaskStr As String = "xdfeotjdwps"
        Dim OrigSplit() As String = OrigStr.Split()
        For I = 0 To OrigSplit.Length - 1
            OrigSplit(I) = Space(OrigSplit(I).Length).Replace(" ", MaskStr(I))
        Next
        Label2.Text = Join(OrigSplit)

        'Using Caesar Cipher

        Label1.Text = CaesarCipher(OrigStr, "Test123")
    End Sub
    Private Function CaesarCipher(Input As String, KeyStr As String) As String
        Dim Key As Integer = 0
        For Each c As Char In KeyStr
            Key += Asc(c)
        Next
        Dim Result As String = ""
        If Key > 93 Then Math.DivRem(Key, 93, Key)
        For Each c As Char In Input
            If c <> " " Then
                Dim offset As Integer = Key + Asc(c)
                If offset > 125 Then offset = (offset - 125) + 32
                Result += Chr(offset)
            Else
                Result += c
            End If
        Next
        Return Result
    End Function

Thanks for all of the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.