473,508 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use Keypress Properly?

22 New Member
Hi

I am using Keypress(keyAscii as integer) like this

Private Sub txtBox_keypress(KeyAscii As Integer)
txtBox.SelStart = 0
txtBox.SelLength = Len(txtBox.Text)
If KeyAscii = 13 Then
Call SaveContents

but the problem is I can only use this if the contents of txtbox are 1 character long otherwise it resets itself when it checks if I have pressed the return key.
Also is the an Ascii specific code for the Enter key on the NumberPad.

Frank
Dec 16 '06 #1
3 2792
albertw
267 Contributor
Hi

I am using Keypress(keyAscii as integer) like this

Private Sub txtBox_keypress(KeyAscii As Integer)
txtBox.SelStart = 0
txtBox.SelLength = Len(txtBox.Text)
If KeyAscii = 13 Then
Call SaveContents

but the problem is I can only use this if the contents of txtbox are 1 character long otherwise it resets itself when it checks if I have pressed the return key.
Also is the an Ascii specific code for the Enter key on the NumberPad.

Frank
hi
your selstart / sellength statements make the textbox reset itself.
better use these statements on getfocus
Dec 18 '06 #2
Killer42
8,435 Recognized Expert Expert
Hi

I am using Keypress(keyAscii as integer) like this

Private Sub txtBox_keypress(KeyAscii As Integer)
txtBox.SelStart = 0
txtBox.SelLength = Len(txtBox.Text)
If KeyAscii = 13 Then
Call SaveContents

but the problem is I can only use this if the contents of txtbox are 1 character long otherwise it resets itself when it checks if I have pressed the return key.
Also is the an Ascii specific code for the Enter key on the NumberPad.

Frank
If the SaveContents routine is depending on the text to be selected, then you should do so either in the routine itself, or inside the If...End If structure. For example...
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtBox_keypress(KeyAscii As Integer)
  2.   If KeyAscii = 13 Then
  3.     txtBox.SelStart = 0
  4.     txtBox.SelLength = Len(txtBox.Text)
  5.     Call SaveContents
  6. ...
It might also be nice to put the textbox back the way you found it. This might work...
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtBox_keypress(KeyAscii As Integer)
  2.   Static HoldStart As Long, HoldLength As Long
  3.   If KeyAscii = 13 Then
  4.     HoldStart = txtBox.SelStart
  5.     HoldLength = txtBox.SelLength
  6.     txtBox.SelStart = 0
  7.     txtBox.SelLength = Len(txtBox.Text)
  8.     Call SaveContents
  9.     txtBox.SelStart = HoldStart
  10.     txtBox.SelLength = HoldLength
  11. ...
Dec 19 '06 #3
frankleggett
22 New Member
Thanks for your replies

Frank
Dec 19 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
24150
by: Trevor Fairchild | last post by:
I'm writing a program that is designed for quick navigation using specific buttons on the keyboard. In doing this, I have added a KeyPress event for every control on the form to intercept the key...
1
3876
by: seeker52 | last post by:
if keyascii = 32 then text0 = text0 & " " text0.selstart = len(text0) end if This does exactly what it needs to do when a breakpoint is added inside the if. Without the breakpoint the...
3
7326
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
5389
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
6150
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...
10
27779
by: Tim Frawley | last post by:
I am attempting to detect a Shift+Tab in the KeyPress event for back navigation on a control that doesn't support this method. Does anyone have any ideas how to compare e.KeyChar to a ShiftTab? ...
4
4518
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
4011
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...
3
6837
by: Fia | last post by:
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...
0
7226
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
7125
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...
0
7388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7049
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...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5055
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...
0
4709
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...
0
3199
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...
0
1561
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 ...

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.