473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 6822
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
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_KeyPress( _
Me.FooTextBox, _
New KeyPressEventArgs(ChrW(CharacterCode)) _
)
///

--
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.EventArgs". The object type passed to a KeyPress
event is of type "System.Windows.Forms.KeyPressEventArgs".

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 KeyPressEventArgs 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(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
TextBox1_KeyPress(Button1, New
System.Windows.Forms.KeyPressEventArgs("A"c))
End Sub

Private Sub TextBox1_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles TextBox1.KeyPress
Me.TextBox1.Text = "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 KeyPressEventArgs 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
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...
3
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...
2
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...
1
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...
4
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...
15
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...
5
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 =...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.