Welcome to Dream.In.Code
Become a VB Expert!

Join 149,537 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,526 people online right now. Registration is fast and FREE... Join Now!




Using the replace method

 
Reply to this topicStart new topic

Using the replace method

shortcut144
29 Nov, 2007 - 10:45 AM
Post #1

New D.I.C Head
*

Joined: 29 Nov, 2007
Posts: 1


My Contributions
Is there a way to use the Replace method in VB to replace a whole word?

CODE

word = InputBox("Enter a word:", "Hangman Game") 'User inputs word

' display dashes in the wordLabel
        ' and clear the incorrectLabel
        If word.Length = 3 Then
            wordLabel.Text = "---"
        ElseIf word.Length = 4 Then
            wordLabel.Text = "----"
        ElseIf word.Length = 5 Then
            wordLabel.Text = "-----"
        ElseIf word.Length = 6 Then
            wordLabel.Text = "------"
        ElseIf word.Length = 7 Then
            wordLabel.Text = "-------"
        End If


I want to be able to put a certain amount of dashes in the wordlabel box according to how many characters are put into the word inputbox instead of making If... Then statements like the ones shown. I have only found a way to use the replace method to replace a single letter.

Mod Edit: Please use code blocks, makes it easier to read smile.gif ==> code.gif

This post has been edited by PsychoCoder: 29 Nov, 2007 - 12:14 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Using The Replace Method
29 Nov, 2007 - 12:22 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
If this is .NET you could use

CODE

wordLabel.text = new String("-",word.length)


This will set the label to the number of dashes that is the same length of the word.

Enjoy!

"At DIC we be dash making code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Using The Replace Method
29 Nov, 2007 - 12:50 PM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,481



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
The best, and most efficient way, I can think of would be to turn the text in the TextBox into a char array, then loop through the array replacing each char with a "-", something like this:

CODE

Private Sub ReplaceText()
     'Create a Char Array to hold the items in the text box
      Dim items() As Char = TextBox1.Text.ToCharArray()
     'Empty the textbox
      TextBox1.Text = String.Empty
      'Loop through each char in the array
      'and replace it with a "-"
       For Each ch As Char In items
           TextBox1.Text += "-"
       Next
End Sub


Something like this should help you. You could also use the PasswordChar property of the TextBox control to have it do this for you. So the choice is really up to you.

Hope this helps smile.gif
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 09:14PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month