Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > How to change e.keyChar value?
 

How to change e.keyChar value?

Hi

I am working on a uni-code text editor (for urdu language)
in VB6 it was easy to override input value of a KeyPress event like

If KeyAscii=65 Then KeyAscii=98 'I can override the key value

So I can replace input values by my unicode characters values

but in .Net the e.KeyChar value is read-only, how can I over-ride this value


(If I use following procedure, it will work but will not accurate

If e.KeyChar="a" Then
e.Handled=True
Text1.Text &= ChrW(1575)
End If

because if user changes cursor somewhere middle in the text
the event will append the character on the last of text)

Thanks

Lahore - Pakistan
Umer Saleem  Saturday, November 28, 2009 8:56 AM
You can use e.Handled = True then raise a new KeyPress event with the adjusted e.KeyChar value in the argument.
Acamar  Saturday, November 28, 2009 10:14 AM


In VB 2008 and VB 2010, you can do:

Private Sub TextBox1_KeyPress(ByVal sender As Object, _

ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

If e.KeyChar = "a" Then

e.KeyChar = "e"

End If

End Sub

Crazypennie  Saturday, November 28, 2009 1:48 PM
Hi,

In VS 2005, you can change the key char in key press event.

Private Sub TextBox1_KeyPress(ByVal sender As Object, _

ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
' To change the char entered into upper case characters

If Char.IsLetter(e.KeyChar) Then
e.KeyChar = Char.ToUpper(e.KeyChar)
End If
End Sub

Diya R  Monday, November 30, 2009 6:17 AM

Is it compact framework?

Does following work?

 If e.KeyChar = "a" Then
            e.Handled = True
            Dim currentPos = TextBox1.SelectionStart
            TextBox1.Text = TextBox1.Text.Insert(currentPos, ChrW(1575))
            TextBox1.SelectionStart = currentPos + 1
        End If


Arjun Paudel
Arjun Paudel  Monday, November 30, 2009 10:47 AM

You ask it so nice even if the OP gives the idea that something can be done in VB6 what is not even in VB6.

:-)

I think that your solutions is the most real.

Even removing an replacing a key from the keyboard will not do like the OP is asking.

:-)


Success
Cor
Cor Ligthert  Monday, November 30, 2009 11:57 AM
Sometimes, you have to pick your fights with Windows more carefully. (And, at times like this, I am not "above" outright cheating. Here's one way to do that, but...I am certain that you can make it more efficient.)

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, _
                                     ByVal e As System.EventArgs) _
                                     Handles TextBox1.TextChanged
        Dim currentPos As Integer = TextBox1.SelectionStart

        TextBox1.Text = TextBox1.Text.Replace(ChrW(65), ChrW(1575))
        TextBox1.SelectionStart = currentPos
    End Sub


jinzai  Monday, November 30, 2009 10:39 PM

You can use google to search for other answers

Custom Search

More Threads

• Using My.Computer.FileSystem to Display a Web Directory
• Sorted list
• How to close Byte Array with 0 Byte?
• Returning a value
• Comparing frames from webcam
• I am getting "Cannot Create ActiveX component" when I try to Release DHCP lease using "GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")"
• Scrollable Display of Extremely Large Image on a Form
• Learning TAPI 3.0
• Checkbox problem..help needed immediately please
• Assembly always not found