Sample Code

netnewb2002

Member
Joined
Sep 17, 2002
Messages
13
Location
Wisconsin
Im trying to create a guessing game. Is there any sample code / Links for subsituting asteriks in for the word in .NET?

It would be the same word but hidden by the asteriks, and how would I subsitute a letter in those asteriks?

I started out trying to read a random word from file to the text box:

[VB]
Dim sWords() As String hold words from file

Private Sub frmguess_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
read in the file
Dim fs As FileStream
Dim reader As StreamReader
fs = New FileStream("words.txt", FileMode.Open) word to bin directory
reader = New StreamReader(fs)
Read in the words
While reader.Peek <> -1
If sWords Is Nothing Then
ReDim Preserve sWords(0)
Else
ReDim Preserve sWords(sWords.GetUpperBound(0) + 1)
End If
sWords(sWords.GetUpperBound(0)) = reader.ReadLine read in the word
txtwords.text=swords /// ?
End While

[/VB]

Cant seem to get the word from the file to display in the textbox. Any Ideas?


Thanks.
 
1. If you mean putting asterisks in place of all characters in a text box(?), then go to your text box, scroll down through the properties to Behaviour, and in there is a property for a password character, put * or whatever you want in there. Or to do it with code, put:
Code:
[I]textboxname[/I].PasswordChar = "*"

2.
To do this, create a list box and set its visible property to false
Code:
            Dim Free As Integer = FreeFile()
            Dim strTemp As String

             Add all of the words form words.txt to [i]Listname[/i]
            FileOpen(Free, [I]Filename[/I], OpenMode.Input)
            Do Until EOF(Free)  EOF = End Of File
                Input(Free, strTemp)
                [I]Listname[/I].Items.Add(strTemp & vbCrLf)
            Loop
FileClose(Free)

Then, to get rid of all the vbCrLf thingies, put:
Code:
 For I = 0 To [I]Listname[/I].Items.Count - 1
            [i]Listname[/i].Items.Item(I) = Replace$([I]Listname[/I]. _ Items.Item(I), vbCrLf, "")
        Next

Then, finally, if you want one randomly stuck into the textbox, put:
Code:
Randomize()
Dim Rand As Integer = Int(Rnd() * _
  ([I]Listname[/I].Items.Count - 1)

[I]Textbox[/I].Text = [I]Listname[/I].Items.Item(Rand)

Hope that helps:)
 
Last edited by a moderator:
Another question, :/

How would I subsitute a letter in for the asterik for the text box? So, if i had a word like "meat" which shows up like "****", and i wanted to subsitute an e for the asterik, how would I try to do that?

**** <--Meat

*e** <--Meat
 
In the text box containing the word (named txtWord for this example), disable it and create another text box (named txtGuess), this will be where you guess. Put the following code where its needed:

Code:
Public CurWord As String = txtWord.Text
txtWord.PasswordChar = ""
For I = 0 To txtWord.Text.Length
txtWord.Text = Replace$(txtWord.Text, txtWord.Text.Char(I), "*")
Next
txtGuess.MaxLength = txtWord.Text.Length

Then, in the txtGuess text changed event, put:
Code:
For I = 0 To txtWord.Text.Length
If txtGuess.Text.Char(I) = CurWord.Char(I) Then
txtWord.Text.Char(I) = txtGuess.Text.Char(I)
End If
Next

That should work, havent tested it yet.
 
Or, if that doesnt work, replace the line:
Code:
txtWord.Text.Char(I) = txtGuess.Text.Char(I)
With
Code:
txtWord.Text = Replace$(txtWord.Text, txtWord.Text.Char(I), txtGuess.Text.Char(I))

Havent tested it.
 
Sorry, bit confused on where to put this:

Code:
Public CurWord As String = txtWord.Text
txtWord.PasswordChar = ""
For I = 0 To txtWord.Text.Length
txtWord.Text = Replace$(txtWord.Text, txtWord.Text.Char(I), "*")
Next
txtGuess.MaxLength = txtWord.Text.Length

What do I declare "I" as? I tried string, integer, but, either one I try I get an blue line ...build error.
 
Declare I as an integer, put it at the top in the general declarations, get rid of the line:
Code:
Public CurWord As String
and put:
Code:
Dim CurWord As String
in the general declarations
and remove:
Code:
Dim Rand As Integer = Int(Rnd() * (Listname.Items.Count - 1)
and stick that up into the general declarations.

Create a command button, name it cmdNew, set its text to New word or whatever you want, and in the cmdNew_Click event, put this code:

Code:
Randomize()

[i]Textbox[/i].Text = [i]Listname[/i].Items.Item(Rand)  This sticks a new random word from the list into the textbox
txtWord.PasswordChar = ""  Set the password char. to nothing
For I = 0 To txtWord.Text.Length
txtWord.Text = Replace$(txtWord.Text, txtWord.Text.Char(I), "*")
Next
txtGuess.MaxLength = txtWord.Text.Length

If you have anymore problems, Ill attatch a sample project if you want.
 
Last edited by a moderator:
Im sorry, when you have time could you please provide an example program? Im in no hurry, Im trying to figure this code out.

Some day I might actually know what Im doing in Visual Basic.net.

Thanks for the help and quick responses. :)

later,
 
Nice, still looking through your code to see how everything works.

I was working on it awhile also..still didnt figure it out though lol.

Thanks for making a program to show me.

:( = :confused: = :-\ = :D
 
Back
Top