Results 1 to 18 of 18

Thread: Kinda a toughy, take a shot.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Texas
    Posts
    26

    Cool

    If someone inputs a word in text1, I need text2 to output a new word for every letter entered in text 1.

    So If someone enters:
    "Hi" in text1,

    I need text2 to output:
    "ManHam"

    So that "H"=Man, and "i"=Ham, and so on...
    But I also need "H" to be a different word than "h".

    All I really need is the code and I will fill in the word that each letter means.

    Thanks if you can help.
    -System

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Cool Just a suggestion

    Here is how I'd do it, but may not be easiest way...

    Load values into 2 column array on form load (column 1 being a-z column 2 being the values.)

    If only letters, disable keypress of all other cahracters in textbox1. On keypress in textbox1,do a for loop through array to find the letter entered. return value found in textbox2.

    Does this make sense?
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  3. #3
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    Text2.SelStart = Len(Text2)
    Select Case UCase(Chr(KeyAscii))
    Case "H"
    Text2.SelText = "Man"
    Case "I"
    Text2.SelText = "Ham"
    'etc, etc
    End Select
    End Sub
    try This
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Texas
    Posts
    26

    gwdash

    gwdash,
    That works great, but it is not case sensitive.
    I need "H" and "h" to mean two different words.

    Thanks,
    -System

  5. #5
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    Delete the UCase funtion, that makes it Case Sensitive
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    Text2.SelStart = Len(Text2)
    Select Case Chr(KeyAscii)
    Case "H"
    Text2.SelText = "Man"
    Case "I"
    Text2.SelText = "Ham"
    'etc, etc
    End Select
    End Sub
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Texas
    Posts
    26
    Hey, thanks it works great!

    If someone did a backspace in text1, and I wanted text2 to erase the last 5 letters that it ouput, how would I code that?

    ie:
    If someone did a typo and had to backspace in text1, I would want text2 to erase the last 5 letters in text2, for every 1 letter backspaced in text1.

    Thanks,
    -System

  7. #7
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    'in the keypress of text1

    If keyascii = 8 then text2.text = left(text2.text,5)
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Texas
    Posts
    26

    Cool almost

    That code sorta works...

    I input "Hi", and it output "ManHam", then I backspaced once, and it removed "Ham", but I backspaced again, it would not remove "Man".

    So I started testing more, I input "HiHiHi" and it output "ManHamManHamManHam", then I backspaced once and it removed "HamManHamManHam", so I was left with "Man", when it was only suppost to remove once "Ham" off the end.

    Know whats wrong?
    -System

  9. #9
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    hehehe

    You should consider going with James's suggestion. You do not need to handle any backspaces or anything by building the Text2 string each time Text1 changes.

    The time it takes to figure out what text2 should say after every keystroke in text1 is miniscule and it will save you alot of hassle in working out what keystrokes to handle etc.

    This little piece of code should get you going.

    Code:
    Option Explicit
    Const numWords = 2
    Dim myWords(numWords) As String
    Dim myChars(numWords) As String
    
    Private Sub Form_Load()
      
      myChars(1) = "H"
      myWords(1) = "Man"
      
      myChars(2) = "i"
      myWords(2) = "Ham"
      
      
    End Sub
    
    Private Sub Text1_Change()
      Text2.Text = GetText(Text1.Text)
    End Sub
    
    Private Function GetText(myText As String) As String
      Dim c, index As Integer
      Dim myChar, newText As String
      On Error Resume Next
      For c = 1 To Len(myText)
        myChar = Mid(myText, c, 1)
        index = getIndex(myChar)
        If index = -1 Then
          ' this means that there was no matching word found
          ' you could ignore the letter, or maybe do something else like me
          newText = newText + "Err"
        Else
            newText = newText + myWords(index)
        End If
      Next
      GetText = newText
    End Function
    
    Private Function getIndex(ByVal myChar As String) As Integer
      Dim c As Integer
      For c = 1 To numWords
        If myChars(c) = myChar Then
          getIndex = c
          Exit Function
        End If
      Next
      ' index of -1 is our flag to say not found
      getIndex = -1
      
    End Function
    Regards
    Paul Lewis

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Texas
    Posts
    26
    Thanks, I put in the code that you supplied, and changed all the text1's, text2's, and form name to the right ones, etc... but I keep getting a hightlighted yellow error at *** which I taged below:

    Private Sub frmMain_Load()

    myChars(1) = "H"
    myWords(1) = "Man"

    myChars(2) = "i"
    myWords(2) = "Ham"

    End Sub

    Private Sub text1_Change()
    text2.text = GetText(text1.text)
    End Sub

    ***
    Private Function GetText(myText As String) As String
    ***
    Dim c, index As Integer
    Dim myChar, newText As String
    On Error Resume Next
    For c = 1 To Len(myText)
    myChar = Mid(myText, c, 1)
    index = getIndex(myChar)
    If index = -1 Then
    ' this means that there was no matching word found
    ' you could ignore the letter, or maybe do something else like me
    newText = newText + "Err"
    Else
    newText = newText + myWords(index)
    End If
    Next
    GetText = newText
    End Function

    Private Function getIndex(ByVal myChar As String) As Integer
    Dim c As Integer
    For c = 1 To numWords
    If myChars(c) = myChar Then
    getIndex = c
    Exit Function
    End If
    Next
    ' index of -1 is our flag to say not found
    getIndex = -1

    End Function

    Know whats wrong?

    Thanks,
    -System

  11. #11
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Hmmm

    Nope sorry I don't know whats wrong.

    Normally VB will only highlight the line containing the error when you click to run the project. VB normally then gives you an error message which you use to understand the cause.

    So if VB gave you an error message, how about telling me that as well? I think you will get far better help if you try to solve these things for yourself a little bit first.

    By the way, what version of VB do you use please?

    Regards
    Paul Lewis

  12. #12
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523
    Try this:
    Code:
    '//NOTE: IT WILL NOT WORK IF THE USER WILL USE THE DELETE KEY. LET ME KNOW IF YOU WANT THAT FUTURE
    Option Explicit
    Dim LettToWord() As String
    
    Private Sub Form_Load()
        Dim inCounter As Integer
        Dim I As Integer
        
        '//This is a very boring part of the code in which
        '//I tell the program what it should replace a given letter with
        
        For I = 65 To 90 '//A to Z
            ReDim Preserve LettToWord(I)
            Select Case I
            Case 65
                LettToWord(I) = "STRINGFORA"
            Case 66
                LettToWord(I) = "STRINGFORB"
            Case 67
                LettToWord(I) = "STRINGFORC"
            Case 68
                LettToWord(I) = "STRINGFORD"
            Case 69
                LettToWord(I) = "STRINGFORE"
            End Select
            '//and just go on and on until "Z"
        Next I
        
        For I = 97 To 122 '//a to z
            ReDim Preserve LettToWord(I)
            Select Case I
            Case 97
                LettToWord(I) = "STRINGFORa"
            Case 98
                LettToWord(I) = "STRINGFORb"
            Case 99
                LettToWord(I) = "STRINGFORc"
            Case 100
                LettToWord(I) = "STRINGFORd"
            Case 101
                LettToWord(I) = "STRINGFORe"
            '//and you would have to just go on and on until you've reached "z"
            End Select
        Next I
        
    End Sub
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        Dim LastChar As String
        
        LastChar = Right(Text1.Text, 1)
        
        If KeyAscii = vbKeyBack Then
            Text2.Text = Left(Text2.Text, Len(Text2.Text) - Len(LettToWord(Asc(LastChar))))
        Else
            Text2.Text = Text2.Text & LettToWord(KeyAscii)
        End If
    End Sub
    Let me know if something is wrong

    [Edited by QWERTY on 09-13-2000 at 09:38 PM]

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Texas
    Posts
    26

    :) = :(

    Well, If you couldn't tell, I have the "Learning Edition"
    I am very new at this, and all the stuff that came with the package is not helping my much. I really do apprechieate(spelling?) you guys's help.

    QWERTY:
    I am getting some error, i changed all the Text1s' and Text2s' to the correct names, but I am getting this error when I do a test run:

    Compile Error:
    Sub or Function not defined

    It highlights this in yellow:

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    *note: Text1 was replaced with 'inputtext'

    -System

  14. #14
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523
    Do you have this line in the form's declaration section:
    Code:
    Dim LettToWord() As String
    It won't work without it (to make this code work you have to use ALL of it, it won't work if you will just copy a part of it)

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Texas
    Posts
    26

    Question

    Well, as my General Declarations, I have:
    Dim tString As String, tText
    Dim LettToWord() As String
    Dim LastChar As String
    Dim inCounter As Integer
    Dim I As Integer

    when i run it, i get a
    "Run time error '9'
    subscript out of range"

    and when u click debug, it highlights theline below in yellow:
    two.text = two.text & LettToWord(KeyAscii)

    So you can see what I have so far, I posted it below
    (text1=one, text2=two, and my gen. decs. are above, ):

    Option Explicit
    Const numWords = 2
    Dim myWords(numWords) As String
    Dim myChars(numWords) As String
    Private Sub frmMain_Load()

    '//This is a very boring part of the code in which
    '//I tell the program what it should replace a given letter with

    For I = 65 To 90 '//A to Z
    ReDim Preserve LettToWord(I)
    Select Case I
    Case 65
    LettToWord(I) = "STRINGFORA"
    Case 66
    LettToWord(I) = "STRINGFORB"
    Case 67
    LettToWord(I) = "STRINGFORC"
    Case 68
    LettToWord(I) = "STRINGFORD"
    Case 69
    LettToWord(I) = "STRINGFORE"
    End Select
    '//and just go on and on until "Z"
    Next I

    For I = 97 To 122 '//a to z
    ReDim Preserve LettToWord(I)
    Select Case I
    Case 97
    LettToWord(I) = "STRINGFORa"
    Case 98
    LettToWord(I) = "STRINGFORb"
    Case 99
    LettToWord(I) = "STRINGFORc"
    Case 100
    LettToWord(I) = "STRINGFORd"
    Case 101
    LettToWord(I) = "STRINGFORe"
    '//and you would have to just go on and on until you've reached "z"
    End Select
    Next I

    End Sub

    Private Sub one_KeyPress(KeyAscii As Integer)
    LastChar = Right(one.text, 1)
    If KeyAscii = vbKeyBack Then
    two.text = Left(two.text, Len(two.text) - Len(LettToWord(Asc(LastChar))))
    Else
    two.text = two.text & LettToWord(KeyAscii)
    End If
    End Sub

    also, How would I add in the delete key?

    Thanks,
    -System

  16. #16
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    An Observation

    It's not that I am the sort that always thinks he's right..far from it. However I have to make the observation here that the approach System is following is prone to complexities not necessary.

    There is no need to concern ourselves with the KeyPress event (except as lesson perhaps) if in fact the higer level Change event gives us what we want.

    I would normally not post a message like this but in this case I truly beliese System is being led down the wrong path. I hope you are able to see what I am talking about - or perhaps I have missed something System needs to do that requires KeyPress to be used.

    Regards
    Paul Lewis

  17. #17
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    system:
    you could not have copied Paul's code correctly because I jjust copied it and it works just fine.

    I then copy your code and it goes off to never never land.

    Perhaps you should give it another shot. It works very well.

    Job well done Paul

    my little quirk should have been:

    If KeyAscii = 8 Then Text2 = Left(Text2, (Len(Text2) - 3))

    [Edited by HeSaidJoe on 09-17-2000 at 06:03 PM]
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  18. #18
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523
    System:
    What letters are you typing into the textbox1??? It will work only for letters a-e and A-E. You would have to extend the LettToWord array to make it work for all letters
    If you have only this part of the code:
    Code:
        For I = 65 To 90 '//A to Z
            ReDim Preserve LettToWord(I)
            Select Case I
            Case 65
                LettToWord(I) = "STRINGFORA"
            Case 66
                LettToWord(I) = "STRINGFORB"
            Case 67
                LettToWord(I) = "STRINGFORC"
            Case 68
                LettToWord(I) = "STRINGFORD"
            Case 69
                LettToWord(I) = "STRINGFORE"
            End Select
            '//and just go on and on until "Z"
        Next I
        
        For I = 97 To 122 '//a to z
            ReDim Preserve LettToWord(I)
            Select Case I
            Case 97
                LettToWord(I) = "STRINGFORa"
            Case 98
                LettToWord(I) = "STRINGFORb"
            Case 99
                LettToWord(I) = "STRINGFORc"
            Case 100
                LettToWord(I) = "STRINGFORd"
            Case 101
                LettToWord(I) = "STRINGFORe"
            '//and you would have to just go on and on until you've reached "z"
            End Select
        Next I
    and you try to type "f" then you will get the error, because I didn't tell the program what I want to replace the f with. You have to do it in the above Sub (I'm quite sure that this is the problem, because that code works just perfect on my computer)

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