473,581 Members | 2,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Keypress??

Fia
Hi
In Visual Basic 6 I could call keypress with an integer of a choosen key.
For example I could call a textbox's keypress with the number 13 (for the
enter key). But now in Visual Basic .Net I don't know how I shall do to do
the same thing. I have tried to send Keys.Numpad8, but then it complains
about it cannot convert from Keys to keyEventArgs I have also tried to send
the number 8, but then it complains it cannot convert from Integer to
keyEventArgs
Can anyone help me, please

Thanks
Fia

Nov 21 '05 #1
3 6849
Private Sub TextBox1_KeyDow n(ByVal sender As Object, ByVal e As
System.Windows. Forms.KeyEventA rgs) Handles TextBox1.KeyDow n
If e.KeyCode = Keys.Enter Then
MessageBox.Show ("Enter Key Was Pressed")
End If
End Sub
Nov 21 '05 #2
"Fia" <fi*****@telia. com> schrieb:
In Visual Basic 6 I could call keypress with an integer of a choosen key.
For example I could call a textbox's keypress with the number 13 (for the
enter key). But now in Visual Basic .Net I don't know how I shall do to do
the same thing. I have tried to send Keys.Numpad8, but then it complains
about it cannot convert from Keys to keyEventArgs I have also tried to
send
the number 8, but then it complains it cannot convert from Integer to
keyEventArgs


\\\
Dim CharacterCode As Integer = 13
FooTextBox_KeyP ress( _
Me.FooTextBox, _
New KeyPressEventAr gs(ChrW(Charact erCode)) _
)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3
Fia wrote:
I have tried to send Keys.Numpad8, but
then it complains about it cannot convert from Keys to keyEventArgs I
have also tried to send the number 8, but then it complains it cannot
convert from Integer to keyEventArgs
Can anyone help me, please


The parameters that are passed to the events of control have changed a lot
between VB6 and VB.NET.

In VB6, you would get a list of parameters that changed from one event type
to the next, containing all the relevant information (keycodes, mouse
positions, etc.). In VB.NET, you will always get two objects passed as your
event parameters: sender (which will point to the control that caused the
event to fire) and a second parameter usually called "e" or "eventArgs"
which contains all the relevant data for the event.

The object type of "e" will change from one event to another. An event such
as a button click that doesn't provide any extra information will provide an
object of type "System.EventAr gs". The object type passed to a KeyPress
event is of type "System.Windows .Forms.KeyPress EventArgs".

If you look at this object within your keypress event, you'll see if has all
the information you need to respond to the keypress (specifically the
KeyChar, which tells you which key was pressed, and the Handled property
which if set to True tells VB to ignore the keypress because you've already
dealt with whatever it was supposed to do).

In order to call the Keypress event procedure, you will need to provide both
of these objects (sender and e). For sender, just pass the control that the
user interacted with (or pass Nothing if there's nothing relevant, it's up
to your code to deal with this object however you need, or ignore it totally
if you want). For e, you will need to create a new KeyPressEventAr gs object.
The constructor of this object allows you to specify what keycode you want
it to contain.

I created a simple form with a button (Button1) and a textbox (Textbox1).
The textbox displays whatever key you press when it has the focus. The
button calls its KeyPress event and tells it that "A" was pressed.

\\\
Private Sub Button1_Click(B yVal sender As System.Object, _
ByVal e As System.EventArg s) _
Handles Button1.Click
TextBox1_KeyPre ss(Button1, New
System.Windows. Forms.KeyPressE ventArgs("A"c))
End Sub

Private Sub TextBox1_KeyPre ss(ByVal sender As Object, _
ByVal e As System.Windows. Forms.KeyPressE ventArgs) _
Handles TextBox1.KeyPre ss
Me.TextBox1.Tex t = "Key: " & e.KeyChar
e.Handled = True
End Sub
///

As you can see, clicking the button makes the textbox think that "A" was
pressed. (The letter "c" after the "A" in the Button1_Click procedure tells
VB that "A" is a character, not a string, as the KeyPressEventAr gs procedure
excepts a single character to be provided).

However, having said all of that, my recommendation would be that you change
your code so that you don't need to do this.

It's much better IMO to create a separate function to handle the pressed
key, and call it from both the KeyPress and the Click events. This was you
know that if the KeyPress event fires it is always because a key was
pressed. It can make the code much easier to read and maintain in the
future, particularly if someone else is likely to work on the same code.

Hope that helps,

--

(O) e n o n e
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
14845
by: Alex007 | last post by:
Hi, I've been working on this C assignement for a CS course.... the assignement is going pretty well and all my code works well on both Windows and Linux. The only thing that doesn't work is a stupid function that only has to wait for a keypress from the user (it's a CLI program). Here's what I have :
3
7353
by: Darryn Ross | last post by:
Hi, I am trying to catch the KeyPress event on my datagrid but it isn't working... i have also tried registering the handler with the event like this... dgGLBatch.KeyPress += new KeyPressEventHandler(dgGLBatch_KeyPress); but nothing changed. I was wondering wether a custom table and column style might have an effect on how this works???
2
5395
by: rege | last post by:
Hi, I have two applications developed in C# say A and B. When user presses key "L" with applicaition A active , it causes a keypress event which causes application B to run. Application A then waits for application B to exit. On exit from Application B the focus shifts back to application A. The problem is if user presses L more than...
1
6160
by: Rene | last post by:
Hi, I am running is some problems with the KeyPreview and KeyPress events. The KeyPress event is only triggered when there this an focusable control on the form. When all controls are disabled the Form.KeyPress event does not trigger anymore. A way to 'solve' this is to set the focus to the form, but during testing it happens ofter the...
4
4521
by: Tom | last post by:
I have a VB.NET user control that I wrote - this control has three or four other controls on it (textbox, combobox, datetime picker, etc). Now, whenever the control is on a form and the user enters anything into the textbox (for instance) I trap the keypress event to handle some stuff (i.e. if it is an enter key, etc). Now, once I am done with...
15
4027
by: Adam J. Schaff | last post by:
I have noticed that if a user closes a form via pressing return (either while the OK button has focus or if AcceptButton is set to OK for the form) then the "ENTER" keypress event fires ON THE CALLING FORM! This is very bad for me, because in my application, Form1 responds to an ENTER keypress by calling Form2. If the user closes Form2 via an...
5
3069
by: Henry Jones | last post by:
I am new to C# and wanted to capture the KeyPress for a textbox. I created some code as follows: private void textBox3_KeyPress(object sender, System.EventArgs e) { this.textBox2.Text = sender.ToString();
2
19284
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class ctlLabelTextbox : UserControl { .... } The class that I have created for this purpose is derived from class UserControl.
0
7804
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7910
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3832
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.