Avatar of cmladios
 asked on

swapping inputted character

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

.NET Programming

Avatar of undefined
Last Comment

8/22/2022 - Mon

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.

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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes