VB6 KeyDown | KeyUp VS VS.NET

ADO DOT NET

Centurion
Joined
Dec 20, 2006
Messages
160
Hi
I was using this in VB6:
Visual Basic:
    Private Sub SubjectTextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles SubjectTextBox.KeyDown
        On Error Resume Next
        If (KeyCode = vbKeyZ) And (Shift = vbCtrlMask) Then
            SendKeys("^Z")
        End If
        If (KeyCode = vbKeyX) And (Shift = vbCtrlMask) Then
            Clipboard.Clear()
            Clipboard.SetText(Subject.SelText)
            Subject.SelText = ""
        End If
        If (KeyCode = vbKeyC) And (Shift = vbCtrlMask) Then
            Clipboard.Clear()
            Clipboard.SetText(Subject.SelText)
        End If
        If (KeyCode = vbKeyV) And (Shift = vbCtrlMask) Then
            Subject.SelText = Clipboard.GetText
        End If
        If (KeyCode = vbKeyA) And (Shift = vbCtrlMask) Then
            Subject.SelStart = 0
            Subject.SelLength = Len(Subject.Text)
        End If
    End Sub
    Private Sub SubjectTextBox_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles SubjectTextBox.KeyUp
        On Error Resume Next
        If KeyCode = vbKeyDelete Then
            If Subject.SelLength = 0 Then Subject.SelLength = 1
            Subject.SelText = ""
        End If
    End Sub
I just copied it from my old code!
Most of features are not supported!
I just want to know that now how can determine the pressing of the following keys in a text box:)

Ctrl + Z
Ctrl + X
Ctrl + A
Ctrl + V
Ctrl + A
Delete !
 
The biggest change is in using the parameter "e" passed to the event. It has the KeyCode and boolean values for Shift. For control and Alt, you'll need something like this:
C#:
            bool CtrlPressed = ((Control.ModifierKeys & Keys.Control) != 0);
            bool AltPressed = ((Control.ModifierKeys & Keys.Alt) != 0);

You could also make this more generic by casing "sender" to TextBox. That way you wouldn't have to hard-code the name "Subject". It paves the way to you making your own custom control that just inherits from TextBox.

-ner
 
thank you sir for your help:)
but i cannot understand c#!
i just know vb
i want in my key press/up/down event check if the mentions keys are pressed?
 
thank you
I managed to convert this code:
Visual Basic:
    Private Sub SubjectTextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles SubjectTextBox.KeyDown
        If (KeyCode = vbKeyZ) And (Shift = vbCtrlMask) Then
            SendKeys("^Z")
        End If
    End Sub
to this code:
Visual Basic:
    Private Sub SubjectTextBox_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles SubjectTextBox.KeyUp
        If e.Control = True And e.KeyCode = Keys.Z Then
            MsgBox("a")
        End If
    End Sub
But I have 2 questions:

1. Why it does not work well when I press Ctrl + Z ?!
It works but not normally I have to press both buttons too tight!?

2. How should I send keys in vb.net like "SendKeys("^Z")" in VB?

Thanks :)
 
(Addressing question 1) The code worked fine for me. What do you mean you have to press both buttons too tight? Maybe it would seem more responsive if you used the KeyDown event instead of KeyUp.
 
Back
Top