How to change label characters

0

I'm doing the hangman and every time the player hits a letter he has to change the letter by the line but I can not find a way to do it with a label, could it only be done in a textbox?

Thanks in advance!

 For i = 0 To palabraAAcertar.Length - 1
      If tbEscribirLetra.Text = palabraAAcertar(i) Then
        'reemplazar caracter
      End If
  Next
    
asked by Lluís 07.01.2018 в 12:11
source

1 answer

0

You could do it using the Substring method, as you say:

For i = 0 To _palabraAAcertar.Length - 1
    If tbEscribirLetra.Text = _palabraAAcertar(i) Then
        labelResultado.Text =
            labelResultado.Text.Substring(0, i) &
            _palabraAAcertar(i) &
            labelResultado.Text.Substring(i + 1)
    End If
Next

The result is the concatenation of the text of the i characters first, the new character that will replace the line and the following characters to the index i .

You could also use a StringBuilder object:

Dim sb As New StringBuilder(labelResultado.Text)
For i = 0 To _palabraAAcertar.Length - 1
    If tbEscribirLetra.Text = _palabraAAcertar(i) Then
        sb(i) = tbEscribirLetra.Text
    End If
Next
labelResultado.Text = sb.ToString()

You create a StringBuilder object with the initial text, you replace the characters of the indices indicated and you generate the resulting string with the ToString method.

    
answered by 07.01.2018 / 12:51
source