ADO DOT NET Posted January 12, 2007 Posted January 12, 2007 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 ! Quote
*Experts* Nerseus Posted January 12, 2007 *Experts* Posted January 12, 2007 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 Quote "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
ADO DOT NET Posted January 12, 2007 Author Posted January 12, 2007 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? Quote
mskeel Posted January 12, 2007 Posted January 12, 2007 Going from C# to VB isn't hard. Try it and you might surprise yourself. Quote
ADO DOT NET Posted January 12, 2007 Author Posted January 12, 2007 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 :) Quote
Leaders snarfblam Posted January 12, 2007 Leaders Posted January 12, 2007 (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. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.