Replace & with 1 when entering in the textbox

Solved
Louloude74 Posted messages 62 Status Member -  
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   -
Hello,

I don't know about you, but it often happens to me, since I don’t have a numeric keypad, that I input numbers like this &é" instead of 123.
To avoid this, I would like to find out how to replace the characters &é"'(-è_çà with 1234567890 as I type; and at the same time, force uppercase letters.

For the uppercase letters, I use:
 Private Sub TextBox1_Change() Me.TextBox1.Text = UCase(Me.TextBox1.Text) End Sub 


Have a nice Sunday and thank you in advance.

Ludwig

4 answers

  1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   Ambassadeur 1 588
     
    Hello,
    maybe:
    Private Sub TextBox1_Change() Me.TextBox1.Text = UCase(Me.TextBox1.Text) Me.TextBox1.Text = replace(Me.TextBox1.Text,"&&","1") ' ... Me.TextBox1.Text = replace(Me.TextBox1.Text,"à","0") End Sub
    0
  2. Anonymous user
     
    Hello

    if you are working in VB.Net, there is a more optimized way (in terms of execution time)

    You need to derive the textbox and rewrite the ProcessCmdKey method
    Here is an example in C# (the base language of .Net, VB.Net is "just" a skin VB)
    https://codes-sources.commentcamarche.net/forum/affich-1526742-keydown-keypress#5

    And something to convert from C# to VB.Net
    https://lite.qwant.com/?q=c%23+to+vb&client=opensearch
    --
    When I was little, the Dead Sea was only sick.
    George Burns
    0
  3. Anonymous user
     
    I didn't explain why it's not optimized to do Replaces in the Change event (and this applies regardless of the VB used, but I only have a good solution in .Net)

    Let's say your textBox contains AZERTYUIOP and you've just typed à.

    We will apply the 10 Replaces one after the other.
    Each Replace will scan each character to see if it is the one it needs to replace:
    • AZERTYUIOPà to find &,
    • AZERTYUIOPà to find é
    • etc...doing nothing until
    • AZERTYUIOPà to find à, and there it finds à which it replaces with 0 so the textBox becomes AZERTYUIOP0


    So the Changed event is triggered again and we start scanning AZERTYUIOP0 10 times for nothing.

    In the end, 220 Replaces are executed and only one bears fruit.

    Of course, it's done in the blink of an eye, but since I’m a bit twisted, I'll copy and paste
    &é"'(-è_çà
    into my textBox
    There is a change, so
    &é"'(-è_çà
    becomes
    1é"'(-è_çà
    which will trigger a new change event, one of them "Change" currently executing (I don’t know which will take priority over the other...) will change the text back to
    12"'(-è_çà
    which will trigger a 3rd execution of the event, etc...etc...

    At a glance, 10 Changed events will each perform 10 Replaces, hoping that all the parallel executions do not conflict.

    That's when it will start to show, and I won't tell you if you press a key in the meantime....

    --
    When I was little, the Dead Sea was just sick.
    George Burns
    0
  4. Louloude74 Posted messages 62 Status Member 1
     
    Hello and thank you for your responses,

    I tested the first solution, the one with replace().
    It’s fast but doesn’t work for the characters é, è, ç, and à.

    I checked your link and made a conversion, but some lines of code are in red.

    It’s a shame there isn't a function like ucase().
    I will keep looking.

    Thanks again for your responses and have a good Sunday.

    Ludwig
    0
    1. Anonymous user
       
      I went to check your link and made a conversion, but some lines of code are in red.

      Well, are you coding in VB.Net or not?
      If it's another VB, it definitely won't work....

      If it's indeed VB.Net, did you derive the textbox?
      Which lines are in red?
      0
    2. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
       
      Did you add the 8 missing lines for replacements 2 to 8?
      0
      1. yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588 > yg_be Posted messages 23437 Registration date   Status Contributor Last intervention  
         
        this will be a bit faster. it seems to me quite unimportant, the computer just works a little bit for each character entered:
        Private Sub TextBox1_Change() dim t as string static aa as boolean if not aa then aa = true t=Me.TextBox1.Text t = UCase(t) t = replace(t,"&&","1") ' ... add the 8 lines from 2 to 8 t = replace(t,"à","0") if t <> Me.TextBox1.Text then Me.TextBox1.Text = t end if aa = false end if End Sub
        0