Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi

I was using this in VB6:

   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 !

  • *Experts*
Posted

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:

           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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

thank you

I managed to convert this code:

   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:

   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 :)

  • Leaders
Posted
(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.
[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...