[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
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
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.
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.
Re: vb.ner remove first char from text
Quote:
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.
Re: vb.ner remove first char from text
Quote:
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.
Re: vb.ner remove first char from text
Quote:
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.
Re: vb.ner remove first char from text
Quote:
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.
Re: vb.ner remove first char from text
Quote:
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.
Re: vb.ner remove first char from text
Quote:
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.
Re: vb.ner remove first char from text
Quote:
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".
Re: vb.ner remove first char from text
Quote:
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)
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.
Re: vb.ner remove first char from text
Quote:
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.
Re: vb.ner remove first char from text
Quote:
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.