We help IT Professionals succeed at work.

swapping inputted character

cmladios asked
Last Modified: 2013-11-26
hi guys,
im using vb.net 2K5 and im in quite a pickle here. hope someone can help me out.

what i want to do is when the user inputs the letter "g" my program will check if the preceding index is an "n" and if it is both the letters "n" and "g" would be replaced with the symbol "]" as i type

provided below is the code i curently have.

but my problem with this code is when you input "g" it will swap out "n" & "g" for "]" and it will keep doing this everytimei "g" is pressed and while  there is an "n". and as a result it will swap out "n" characters that i don't want to be swapped out.

merlot and ping
--> merlot a]d pi]

all i want is whenever i type "g" the prog. will check the preceding index if its an "n" and if yes it will swap "n" and "g" with "]" thats it.

merlot and ping
--> merlot and pi]
merlot and pi] galileo
Private Sub txt1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtbw1.KeyPress
        Dim asc As Short
        Dim jub As String
        Dim grad As Char
        Dim i As Integer
        Dim TextLength As Integer
        jub = Trim(txtbw1.Text)
        TextLength = jub.Length
        asc = Microsoft.VisualBasic.AscW(e.KeyChar)
        asc = IIf(asc >= 65 And asc <= 91 Or asc >= 97 And asc <= 122 Or asc = 32 Or asc = 1 Or asc = 93 Or asc = 97 Or asc = 8, asc, 0)
        If asc = 97 Then
            txtbw2.Text = ""
        End If
        If asc = 103 Then
            For i = 0 To TextLength - 1
                grad = jub.Chars(i)
                If grad = "n" Then
                    asc = 93
                    txtbw1.Text = jub.Remove(i, 1)
                    txtbw1.SelectionStart = i
                ElseIf grad = "N" Then
                    asc = 91
                    txtbw1.Text = jub.Remove(i, 1)
                    txtbw1.SelectionStart = i
                End If
        End If
        e.KeyChar = Microsoft.VisualBasic.ChrW(asc)
        If asc = IIf(asc < 65 And asc > 91 Or asc < 98 And asc > 122, asc, 0) Then
            MsgBox("That character has no baybayin equivalent", MsgBoxStyle.Exclamation, "JDN-CKS Baybayin Writer")
        End If
    End Sub

Open in new window

Watch Question

Applications and Integrations Consultan
This one is on us!
(Get your first solution completely free - no credit card required)


thanks tim,

i tried on the code and it works like a charm. however i forgot a few details and once again i need your help.

see heres my new dilema:

i need to replace capital "N" with "[" when i type small "g"


so what my program should be doing when ever i type "g"  it will check if its preceded by an "n" or "N"

if "g" is preceded by "n" it will be swapped out for  "]"
if "g" is preceded by "N" it will be swapped out for  "["

plus i noticed when i type in "g" and it is not preceded by an "n" or "N" the insertion point does not position itself after letter "g"

i tried playing around with your code and im stumped... this is so frustrating... Thanks again for your time and patience
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.