Changing letters

Say I have two text boxes I want the first one to have "god" in it. How would I get the program to run so when I click a button the second text box changes the letters to whatever

Comments

  • : Say I have two text boxes I want the first one to have "god" in it. How would I get the program to run so when I click a button the second text box changes the letters to whatever
    :

    What do you mean with: 'the second textbox changes it...'??
    To change the text in a textbox: (Text1 is the name of your textbox)
    [code]
    Text1.Text = "Whatever"
    [/code]

    Greets...
    Richard

  • : : Say I have two text boxes I want the first one to have "god" in it. How would I get the program to run so when I click a button the second text box changes the letters to whatever
    : :
    :
    : What do you mean with: 'the second textbox changes it...'??
    : To change the text in a textbox: (Text1 is the name of your textbox)
    : [code]
    : Text1.Text = "Whatever"
    : [/code]
    :
    : Greets...
    : Richard
    :
    :
    Basically Im trying to encrypt the letters of the first box so if the first box says god im trying to get the second box to show it as something different such as dog so then every time you see a "g" in the first box it shows up as "d" in the second
  • : : : Say I have two text boxes I want the first one to have "god" in it. How would I get the program to run so when I click a button the second text box changes the letters to whatever
    : : :
    : :
    : : What do you mean with: 'the second textbox changes it...'??
    : : To change the text in a textbox: (Text1 is the name of your textbox)
    : : [code]
    : : Text1.Text = "Whatever"
    : : [/code]
    : :
    : : Greets...
    : : Richard
    : :
    : :
    : Basically Im trying to encrypt the letters of the first box so if the first box says god im trying to get the second box to show it as something different such as dog so then every time you see a "g" in the first box it shows up as "d" in the second
    :

    VB has a function for reversing the text (found that out after I made my own function for it :-D): Reverse$
    To replace every g with a d (for example): Replace$

    To do both, just combine them:
    [code]
    Text2.Text = Reverse$(Replace$(Text1.Text, "g", "d", , , vbTextCompare))
    [/code]
    The vbTextCompare tells Replace$ to compare capital insensitive. If you want it to be capital sensitive, use vbBinaryCompare (which is the default valie, so in that case you can leave the entire last part out)

    Greets...
    Richard

  • : : : : Say I have two text boxes I want the first one to have "god" in it. How would I get the program to run so when I click a button the second text box changes the letters to whatever
    : : : :
    : : :
    : : : What do you mean with: 'the second textbox changes it...'??
    : : : To change the text in a textbox: (Text1 is the name of your textbox)
    : : : [code]
    : : : Text1.Text = "Whatever"
    : : : [/code]
    : : :
    : : : Greets...
    : : : Richard
    : : :
    : : :
    : : Basically Im trying to encrypt the letters of the first box so if the first box says god im trying to get the second box to show it as something different such as dog so then every time you see a "g" in the first box it shows up as "d" in the second
    : :
    :
    : VB has a function for reversing the text (found that out after I made my own function for it :-D): Reverse$
    : To replace every g with a d (for example): Replace$
    :
    : To do both, just combine them:
    : [code]
    : Text2.Text = Reverse$(Replace$(Text1.Text, "g", "d", , , vbTextCompare))
    : [/code]
    : The vbTextCompare tells Replace$ to compare capital insensitive. If you want it to be capital sensitive, use vbBinaryCompare (which is the default valie, so in that case you can leave the entire last part out)
    :
    : Greets...
    : Richard
    :
    :
    Thanks for all the help!
  • [b][red]This message was edited by chuckw1 at 2005-1-30 17:8:16[/red][/b][hr]
    [b][red]This message was edited by chuckw1 at 2005-1-30 15:55:38[/red][/b][hr]
    : : : : : Say I have two text boxes I want the first one to have "god" in it. How would I get the program to run so when I click a button the second text box changes the letters to whatever
    : : : : :
    : : : :
    : : : : What do you mean with: 'the second textbox changes it...'??
    : : : : To change the text in a textbox: (Text1 is the name of your textbox)
    : : : : [code]
    : : : : Text1.Text = "Whatever"
    : : : : [/code]
    : : : :
    : : : : Greets...
    : : : : Richard
    : : : :

    : : : Basically Im trying to encrypt the letters of the first box so if the first box says god im trying to get the second box to show it as something different such as dog so then every time you see a "g" in the first box it shows up as "d" in the second
    : : :
    : :
    : : VB has a function for reversing the text (found that out after I made my own function for it :-D): Reverse$
    : : To replace every g with a d (for example): Replace$
    : :
    : : To do both, just combine them:
    : : [code]
    : : Text2.Text = Reverse$(Replace$(Text1.Text, "g", "d", , , vbTextCompare))
    : : [/code]
    : : The vbTextCompare tells Replace$ to compare capital insensitive. If you want it to be capital sensitive, use vbBinaryCompare (which is the default valie, so in that case you can leave the entire last part out)
    : :
    : : Greets...
    : : Richard
    : :
    : :
    : Thanks for all the help!
    :


    I can only get it to do one letter at a time such as it wont let me do "ab" to "cd" it will only do "ab" to "ad"


  • this will take whatever's in Text1 and change the characters to 1 letter over and store it in Text2 (ie: a becomes b, b becomes c, c becomes d, etc). Note that using this as-is for binary data won't work, because if a character is chr(255) itll get bumped too high, but it should work fine for your purposes, or atleast give you an idea of how to get started.

    [code]
    Text2.Text = ""
    For i = 1 To Len(Text1.Text)
    Text2.Text = Text2.Text + Chr(Asc(Mid(Text1.Text, i, 1)) + 1)
    Next
    [/code]
  • I have one more favor to ask could you explain what

    "For i = 1 To Len(Text1.Text)"
    "Chr(Asc(Mid(Text1.Text, i, 1))" do

    I get that this moves it up a letter + 1)

    "Next" and this

  • [b][red]This message was edited by the walrus at 2005-1-31 18:23:15[/red][/b][hr]
    here's the original code with explanation:

    [code]
    Text2.Text = ""
    For i = 1 To Len(Text1.Text)
    Text2.Text = Text2.Text + Chr(Asc(Mid(Text1.Text, i, 1)) + 1)
    Next
    [/code]

    [blue][b]For i = 1 to Len(Text.Text)[/b]
    ...
    [b]Next[/b][/blue]
    This is a For/Next loop. It first sets i equal to 1 then, when it gets to the [b]Next[/b] it tells the processing to go back to the [b]For[/b] part and adds 1 to i. Then it checks to see if i is greater than [italic]Len(Text1.Text)[/italic], and if so, exits the loop. Here's a simple example of a For/Next loop so you can get an idea about what it does:

    [code]
    For X = 1 to 10
    MsgBox X
    Next
    [/code]
    This code will display the numbers 1 through 10 in a messagebox. When it first enters the loop, X will be 1, then when it gets to the [b]Next[/b] it'll go back up to the [b]For[/b] and set X equal to 2, then X = 3, then X = 4, .. , all the way to X = 10. When it gets to [b]Next[/b] on X = 10, it exits the loop because you've told X to only go to 10.

    The [italic]Len(Text1.Text)[/italic] part gets the "Length" of the Text1.Text string. If Text1.Text is "Hello" then Len(Text1.Text) will be 5, because there are 5 letters in "Hello".

    [blue][b]Mid(Text1.Text, i, 1)[/b][/blue]
    This gets the letter in Text1.Text at position i. So since i is in a For/Next loop, each letter will be gotten one at a time. Here's an example:

    [code]
    For X = 1 to Len(Text1.Text)
    MsgBox Mid(Text1.Text, X, 1)
    Next
    [/code]

    If Text1.Text is "Hello" in this example, it'll display each letter (H, then e, then l, etc.)

    [blue][b]Asc(Mid(Text1.Text, i, 1)) + 1[/b][/blue]
    [b]Asc[/b] gets the ASCII value of the character. You may want to Google for "ASCII" and read about it if you don't know what it is. Basically, each letter or number (or any kind of character) is given an ASCII number that represents it. For example, the letter "a" has an ASCII value of 97 and the letter "b" has an ASCII value of 98. So if you were to do:

    [code]
    MsgBox Asc("a")
    MsgBox Asc("b")
    [/code]

    you'll get a messagebox saying 97 and then one saying 98. Therefore, if you add one to that ASCII value, you're moving it up one. [b]Asc("a") + 1[/b] gets the ASCII value of "b", because it's saying [italic]97 + 1[/italic] which equals 98 which is the ASCII value of "b".

    [blue][b]Chr(Asc(Mid(Text1.Text, i, 1)) + 1)[/b][/blue]
    [b]Chr[/b] turns the ASCII value back into a character. For example, [b]MsgBox Chr(97)[/b] will show a messagebox that says "a". So when you Chr and Asc + 1 together you can move something up a letter..

    [code]
    MsgBox Chr(Asc("a") + 1)
    [/code]

    That will show a messagebox of the letter "b" because it gets the ASCII value of "a" (97) adds 1 to it (98) and then turns it back into a character, Chr(98), which equals "b".

    By putting all that in a For/Next loop and using Mid to get each value of Text1.Text, you can move every letter in the string up one.

    Hope this helps.


  • Note: You can press F8 instead of F5 on all these examples to watch them step by step. If you press F8, VB will process one line of code, then you can press F8 again and watch it process the next line and so on.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories