Results 1 to 15 of 15

Thread: [RESOLVED] vb.net remove first char from text

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2021
    Posts
    166

    Resolved [RESOLVED] vb.net remove first char from text

    i'm make a textbox print another char with keydown event using this:


    on keydown event:

    HTML Code:
    if e.keycode = keys.A then
     My.Computer.Keyboard.SendKeys("B")
    end if
    i need know how i can remove the key that i press of end result see textbox show:

    aBaBaB

    but i only need B result on textbox
    Last edited by LiwisJames; Jul 23rd, 2021 at 12:29 AM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: vb.ner remove first char from text

    Try…

    Code:
    if e.keycode = keys.A then
        e.Handled = True
        My.Computer.Keyboard.SendKeys("B")
    end if

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: vb.ner remove first char from text

    Don't use KeyDown in the first place. Use KeyPress and you can just replace the character:
    vb.net Code:
    1. Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    2.     If e.KeyChar = "A"c Then
    3.         e.KeyChar = "B"c
    4.     End If
    5. End Sub
    Note that KeyPressEventArgs.KeyChar was originally read-only but was made read/write some time ago. Unless you're targeting a really old .NET version, you should be fine.

    Just note that, because this is working with characters and not keys, you will need to take case into account. You should have with keys too, i.e. you should have taken modifiers into account, but it's still different with KeyPress.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: vb.ner remove first char from text

    I just checked and it was .NET Framework 2.0 in which that KeyChar property was made read/write so you definitely should not have an issue with that.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2021
    Posts
    166

    Re: vb.ner remove first char from text

    Quote Originally Posted by .paul. View Post
    Try…

    Code:
    if e.keycode = keys.A then
        e.Handled = True
        My.Computer.Keyboard.SendKeys("B")
    end if
    nothing happens remains the same error.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2021
    Posts
    166

    Re: vb.ner remove first char from text

    Quote Originally Posted by jmcilhinney View Post
    Don't use KeyDown in the first place. Use KeyPress and you can just replace the character:
    vb.net Code:
    1. Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    2.     If e.KeyChar = "A"c Then
    3.         e.KeyChar = "B"c
    4.     End If
    5. End Sub
    Note that KeyPressEventArgs.KeyChar was originally read-only but was made read/write some time ago. Unless you're targeting a really old .NET version, you should be fine.

    Just note that, because this is working with characters and not keys, you will need to take case into account. You should have with keys too, i.e. you should have taken modifiers into account, but it's still different with KeyPress.

    I chose keydown because it works when the key keeps pressing and sending input, how much keypress only sends the result when pressed several times, as in a game that asks to stay clicking a button several times to accomplish something.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2021
    Posts
    166

    Re: vb.ner remove first char from text

    Quote Originally Posted by jmcilhinney View Post
    I just checked and it was .NET Framework 2.0 in which that KeyChar property was made read/write so you definitely should not have an issue with that.
    framework 2 is very cool it is a hybrid between vb6 and vb.net many chose it.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: vb.ner remove first char from text

    Quote Originally Posted by LiwisJames View Post
    framework 2 [...] is a hybrid between vb6 and vb.net
    No it isn't. I don't know whether you're referring to the fact that default form instances were added to VB 2005 but that's a language thing, not a Framework thing.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2021
    Posts
    166

    Re: vb.ner remove first char from text

    Quote Originally Posted by jmcilhinney View Post
    No it isn't. I don't know whether you're referring to the fact that default form instances were added to VB 2005 but that's a language thing, not a Framework thing.
    yes I mean the design and likeness of code.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: vb.ner remove first char from text

    Quote Originally Posted by LiwisJames View Post
    I chose keydown because it works when the key keeps pressing and sending input, how much keypress only sends the result when pressed several times, as in a game that asks to stay clicking a button several times to accomplish something.
    That's complete rubbish. If you press and hold a key then you get multiple KeyDown and KeyPress events. You can see it for yourself if you bother to test. Try this code:
    vb.net Code:
    1. Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
    2.     Console.WriteLine($"KeyDown: {e.KeyCode} ({e.KeyData})")
    3. End Sub
    4.  
    5. Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    6.     Console.WriteLine($"KeyPress: {e.KeyChar}")
    7. End Sub
    8.  
    9. Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
    10.     Console.WriteLine($"KeyUp:  {e.KeyCode} ({e.KeyData})")
    11. End Sub
    Run that and watch the Output window in VS as you press, hold and release keys. Try using modifier keys as well and see the difference between KeyCode and KeyData.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: vb.ner remove first char from text

    Quote Originally Posted by LiwisJames View Post
    yes I mean the design and likeness of code.
    The only thing about VB 2005 that is more like VB6 than VB.NET 2003 is the fact that default form instances allow you to use a form without explicitly creating an object and passing a reference to it around. Not sure that that really qualifies as "design and likeness of code".

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Apr 2021
    Posts
    166

    Re: vb.ner remove first char from text

    Quote Originally Posted by jmcilhinney View Post
    That's complete rubbish. If you press and hold a key then you get multiple KeyDown and KeyPress events. You can see it for yourself if you bother to test. Try this code:
    vb.net Code:
    1. Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
    2.     Console.WriteLine($"KeyDown: {e.KeyCode} ({e.KeyData})")
    3. End Sub
    4.  
    5. Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    6.     Console.WriteLine($"KeyPress: {e.KeyChar}")
    7. End Sub
    8.  
    9. Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
    10.     Console.WriteLine($"KeyUp:  {e.KeyCode} ({e.KeyData})")
    11. End Sub
    Run that and watch the Output window in VS as you press, hold and release keys. Try using modifier keys as well and see the difference between KeyCode and KeyData.
    Where from this shift ?

    KeyPress: a
    KeyUp: A (A)
    KeyDown: ShiftKey (ShiftKey, Shift)
    KeyDown: B (B, Shift)
    KeyPress: B
    KeyUp: B (B, Shift)
    KeyUp: ShiftKey (ShiftKey)

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: vb.ner remove first char from text

    Like I said, KeyDown and KeyUp are about keys on the keyboard while KeyPress is about characters. If you want an upper-case B character then you you need to depress and release both the Shift key and the B key, so to get a single KeyPress event you need two KeyDown events and two KeyUp events. Maybe make an effort on your own behalf and read the documentation for those events instead of asking someone else to explain it to you. That documentation exists for a reason.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Apr 2021
    Posts
    166

    Re: vb.ner remove first char from text

    Quote Originally Posted by jmcilhinney View Post
    Like I said, KeyDown and KeyUp are about keys on the keyboard while KeyPress is about characters. If you want an upper-case B character then you you need to depress and release both the Shift key and the B key, so to get a single KeyPress event you need two KeyDown events and two KeyUp events. Maybe make an effort on your own behalf and read the documentation for those events instead of asking someone else to explain it to you. That documentation exists for a reason.
    well I research about I have the concept of each, but I like to summarize the understanding of reading to be faster and learning and creation. but I read the Microsoft articles in 2016

    thank u by u solution.

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Apr 2021
    Posts
    166

    Re: vb.ner remove first char from text

    Quote Originally Posted by jmcilhinney View Post
    Like I said, KeyDown and KeyUp are about keys on the keyboard while KeyPress is about characters. If you want an upper-case B character then you you need to depress and release both the Shift key and the B key, so to get a single KeyPress event you need two KeyDown events and two KeyUp events. Maybe make an effort on your own behalf and read the documentation for those events instead of asking someone else to explain it to you. That documentation exists for a reason.
    worked friend worked and even more it does faster and better the concepts are widespread and keypress presents more positive result.

Tags for this Thread

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