-
Jul 22nd, 2021, 09:05 PM
#1
Thread Starter
Addicted Member
[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.
-
Jul 22nd, 2021, 09:35 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 22nd, 2021, 09:54 PM
#3
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:
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = "A"c Then
e.KeyChar = "B"c
End If
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.
-
Jul 22nd, 2021, 09:56 PM
#4
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.
-
Jul 22nd, 2021, 09:58 PM
#5
Thread Starter
Addicted Member
Re: vb.ner remove first char from text
Originally Posted by .paul.
Try…
Code:
if e.keycode = keys.A then
e.Handled = True
My.Computer.Keyboard.SendKeys("B")
end if
nothing happens remains the same error.
-
Jul 22nd, 2021, 10:01 PM
#6
Thread Starter
Addicted Member
Re: vb.ner remove first char from text
Originally Posted by jmcilhinney
Don't use KeyDown in the first place. Use KeyPress and you can just replace the character:
vb.net Code:
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = "A"c Then
e.KeyChar = "B"c
End If
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.
-
Jul 22nd, 2021, 10:02 PM
#7
Thread Starter
Addicted Member
Re: vb.ner remove first char from text
Originally Posted by jmcilhinney
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.
-
Jul 22nd, 2021, 10:06 PM
#8
Re: vb.ner remove first char from text
Originally Posted by LiwisJames
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.
-
Jul 22nd, 2021, 10:12 PM
#9
Thread Starter
Addicted Member
Re: vb.ner remove first char from text
Originally Posted by jmcilhinney
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.
-
Jul 22nd, 2021, 10:17 PM
#10
Re: vb.ner remove first char from text
Originally Posted by LiwisJames
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:
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
Console.WriteLine($"KeyDown: {e.KeyCode} ({e.KeyData})")
End Sub
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
Console.WriteLine($"KeyPress: {e.KeyChar}")
End Sub
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
Console.WriteLine($"KeyUp: {e.KeyCode} ({e.KeyData})")
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.
-
Jul 22nd, 2021, 10:25 PM
#11
Re: vb.ner remove first char from text
Originally Posted by LiwisJames
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".
-
Jul 22nd, 2021, 11:42 PM
#12
Thread Starter
Addicted Member
Re: vb.ner remove first char from text
Originally Posted by jmcilhinney
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:
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
Console.WriteLine($"KeyDown: {e.KeyCode} ({e.KeyData})")
End Sub
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
Console.WriteLine($"KeyPress: {e.KeyChar}")
End Sub
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
Console.WriteLine($"KeyUp: {e.KeyCode} ({e.KeyData})")
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)
-
Jul 23rd, 2021, 12:19 AM
#13
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.
-
Jul 23rd, 2021, 12:29 AM
#14
Thread Starter
Addicted Member
Re: vb.ner remove first char from text
Originally Posted by jmcilhinney
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.
-
Jul 23rd, 2021, 02:00 PM
#15
Thread Starter
Addicted Member
Re: vb.ner remove first char from text
Originally Posted by jmcilhinney
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|