473,661 Members | 2,506 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(keyAsc ii as integer) like this

Private Sub txtBox_keypress (KeyAscii As Integer)
txtBox.SelStart = 0
txtBox.SelLengt h = 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 2796
albertw
267 Contributor
Hi

I am using Keypress(keyAsc ii as integer) like this

Private Sub txtBox_keypress (KeyAscii As Integer)
txtBox.SelStart = 0
txtBox.SelLengt h = 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(keyAsc ii as integer) like this

Private Sub txtBox_keypress (KeyAscii As Integer)
txtBox.SelStart = 0
txtBox.SelLengt h = 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
24173
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 pressed and pass it to a standard subroutine that identifies what action should be taken. Unfortunately, I have a web-browser contol on the form and the webbrowser does not include a KeyPress event. I need some way to intercept keypress and...
1
3880
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 keypress does not work properly. Why would this be and how can it be rectified? seeker53
3
7365
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
5397
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 once, say thrice, then three keypress events are generated. Therefore application A starts
1
6170
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 keypress event is not getting triggered on keys like
10
27849
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? Tim
4
4524
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 handling the keypress event for the textbox, I then need to pass that keypress event BACK to the...
15
4036
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 ENTER, then Form1 just reopens it again, kind of trapping the user in Form2 (they can still close...
3
6852
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 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...
0
8343
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8856
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8762
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8633
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7365
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
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
2
1992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.