Results 1 to 6 of 6

Thread: Text Replace (If Not statement)

  1. #1

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013

    Text Replace (If Not statement)

    Hey, just thought I'd post this quick question here, I'm making a hang man game but these If Not statements don't do much... Here's the code:

    vb Code:
    1. Public Class Form1
    3.     Dim GuessedLetters As List(Of Char) = New List(Of Char)
    4.     Dim HiddenWord As String
    6.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    8.         HiddenWord = "ABCabc"
    10.         GuessedLetters.Add("A")
    11.         GuessedLetters.Add("a")
    12.         GuessedLetters.Add("B")
    13.         GuessedLetters.Add("b")
    15.         Label1.Text = GuessedLetters.ToArray
    17.         WriteLetters()
    19.     End Sub
    21.     Private Sub WriteLetters()
    23.         Label2.Text = HiddenWord
    25.         If Not GuessedLetters.Contains("A") Then Label2.Text.Replace("A", "_")
    26.         If Not GuessedLetters.Contains("B") Then Label2.Text.Replace("B", "_")
    27.         If Not GuessedLetters.Contains("C") Then Label2.Text.Replace("C", "_")
    28.         If Not GuessedLetters.Contains("D") Then Label2.Text.Replace("D", "_")
    29.         If Not GuessedLetters.Contains("E") Then Label2.Text.Replace("E", "_")
    30.         If Not GuessedLetters.Contains("F") Then Label2.Text.Replace("F", "_")
    31.         If Not GuessedLetters.Contains("G") Then Label2.Text.Replace("G", "_")
    32.         If Not GuessedLetters.Contains("H") Then Label2.Text.Replace("H", "_")
    33.         If Not GuessedLetters.Contains("I") Then Label2.Text.Replace("I", "_")
    34.         If Not GuessedLetters.Contains("J") Then Label2.Text.Replace("J", "_")
    35.         If Not GuessedLetters.Contains("K") Then Label2.Text.Replace("K", "_")
    36.         If Not GuessedLetters.Contains("L") Then Label2.Text.Replace("L", "_")
    37.         If Not GuessedLetters.Contains("M") Then Label2.Text.Replace("M", "_")
    38.         If Not GuessedLetters.Contains("N") Then Label2.Text.Replace("N", "_")
    39.         If Not GuessedLetters.Contains("O") Then Label2.Text.Replace("O", "_")
    40.         If Not GuessedLetters.Contains("P") Then Label2.Text.Replace("P", "_")
    41.         If Not GuessedLetters.Contains("Q") Then Label2.Text.Replace("Q", "_")
    42.         If Not GuessedLetters.Contains("R") Then Label2.Text.Replace("R", "_")
    43.         If Not GuessedLetters.Contains("S") Then Label2.Text.Replace("S", "_")
    44.         If Not GuessedLetters.Contains("T") Then Label2.Text.Replace("T", "_")
    45.         If Not GuessedLetters.Contains("U") Then Label2.Text.Replace("U", "_")
    46.         If Not GuessedLetters.Contains("V") Then Label2.Text.Replace("V", "_")
    47.         If Not GuessedLetters.Contains("W") Then Label2.Text.Replace("W", "_")
    48.         If Not GuessedLetters.Contains("X") Then Label2.Text.Replace("X", "_")
    49.         If Not GuessedLetters.Contains("Y") Then Label2.Text.Replace("Y", "_")
    50.         If Not GuessedLetters.Contains("Z") Then Label2.Text.Replace("Z", "_")
    51.         If Not GuessedLetters.Contains("a") Then Label2.Text.Replace("a", "_")
    52.         If Not GuessedLetters.Contains("b") Then Label2.Text.Replace("b", "_")
    53.         If Not GuessedLetters.Contains("c") Then Label2.Text.Replace("c", "_")
    54.         If Not GuessedLetters.Contains("d") Then Label2.Text.Replace("d", "_")
    55.         If Not GuessedLetters.Contains("e") Then Label2.Text.Replace("e", "_")
    56.         If Not GuessedLetters.Contains("f") Then Label2.Text.Replace("f", "_")
    57.         If Not GuessedLetters.Contains("g") Then Label2.Text.Replace("g", "_")
    58.         If Not GuessedLetters.Contains("h") Then Label2.Text.Replace("h", "_")
    59.         'and do on... (In my actual code I have the full alphabet twice.)
    61.     End Sub
    63. End Class

    Thanks in advance. P.S. I"m not using any tutorials so I'm pretty sure this is not the most efficient way. Thanks for your patience.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Chelmsford UK

    Re: Text Replace (If Not statement)

    If Not GuessedLetters.Contains("A") Then Label2.Text = Label2.Text.Replace("A", "_")

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Chelmsford UK

    Re: Text Replace (If Not statement)

    hve a look at my hangman in my signature

  4. #4
    Lively Member ShadowTzu's Avatar
    Join Date
    Oct 2014

    Re: Text Replace (If Not statement)

    Dim Alphabet As String = "abcdefghijklmnopqrstuvwxyz"
    For i As Integer = 0 To Alphabet.Length - 1
        If Not GuessedLetters.Contains(Char.ToUpper(Alphabet(i))) Then Label2.Text = Label2.Text.Replace(Char.ToUpper(Alphabet(i)), "_")
        If Not GuessedLetters.Contains(Char.ToLower(Alphabet(i))) Then Label2.Text = Label2.Text.Replace(Char.ToLower(Alphabet(i)), "_")

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002

    Re: Text Replace (If Not statement)

    Wait... hold on one second... your code doesn't make sense....

    If Not GuessedLetters.Contains("A") Then Label2.Text.Replace("A", "_")

    If the GuessedLetters list doesn't contain "A" ... then show "A" in the label? Wait... no... that's reversing it...

    Ah, there's a FAR easier way to deal with this:
    Private Sub WriteLetters()
        For each C as char in GuessedLetters
    End Sub
    You don't need to check for each letter in the list... just loop through the list, replacing the character found with the underscore.

    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Hyperactive Member
    Join Date
    Mar 2012

    Re: Text Replace (If Not statement)

    @tg, I too was confused, but I think the logic is if a letter is not one of the letters that was guessed, change any occurrence of that letter in the hidden word to an underscore (to show that that letter hasn't been guessed correctly yet). Thus the only letters that would appear would be those in the hidden word that the person has made a guess on. As such, I think that ShadowTzu has the best code.

    @NinjaNic, If I understood what I told tg correctly, then use ShadowTzu's code with a little bit of a change (see my final comment). Also you may think about just using capitalized letters only to save yourself the headache of checking for both cases (and just change the user's guess to Uppercase). Finally, you are doing a lot of String manipulation on a control. It is much better to do your data manipulation on variables first and then display the final piece of data to a control. When you make a change to a Label's Text property, you keep redrawing that control to show the change which incurs a lot of overhead. But you don't care about the drawing of the Label except for the data that it contains. Therefore you should change that data first, and then post it to the control. So slightly changing ShadowTzu's solution, try:
    Dim Alphabet As String = "abcdefghijklmnopqrstuvwxyz"
    Dim DisplayedAnswer As String = HiddenWord ' Make a new String so that the original doesn't get corrupted
    For i As Integer = 0 To Alphabet.Length - 1
        If Not GuessedLetters.Contains(Char.ToUpper(Alphabet(i))) Then DisplayedAnswer = DisplayedAnswer.Replace(Char.ToUpper(Alphabet(i)), "_")
        If Not GuessedLetters.Contains(Char.ToLower(Alphabet(i))) Then DisplayedAnswer = DisplayedAnswer.Replace(Char.ToLower(Alphabet(i)), "_")
    ' After the loop that changes the data has completed, assign the Text of Label2 to the processed String
    Label2.Text = DisplayedAnswer
    Doing it this way will also prevent the answer to be briefly flashed on the screen as it is processing the data.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width