Click to See Complete Forum and Search --> : Textboxes
February 16th, 2000, 06:48 PM
I have problems concerning textboxes. Thanks to anyone who can give me some tips on these:
1) I have 2 textboxes, Text1 & Text2, for instance. I wrote a code that after inputting something in Text1 and pressing the Enter key, Text1 will be disabled so that no changes can be made to it, and Text2 will have the focus. But when I try using the Tab key, Text2 has the next focus, but Text1 is still enabled. How can I make the 2 keys perform similarly? I tried using SendKeys but it's still the same.
2) Is there a way of displaying numeric values in textboxes right-aligned?
The Matrix
February 16th, 2000, 08:57 PM
'hope this helps, put this in the forms declarations
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
'put this in timer1's code
Private Sub Timer1_Timer()
If GetAsyncKeyState(vbKeyReturn) <> 0 Then
Text1.Enabled = False
Text2.SetFocus
End If
End Sub
February 17th, 2000, 01:54 AM
Mr. Matrix,
I don't know if I still lack something, but I copied your code and pasted it in a sample form w/ 2 textboxes, and I still don't achieve my desired result. What I did was Text1 will change its background color (to set a more obvious indication that the code works), and set the focus to Text2 once Enter/Tab is pressed. Enter key works, Tab key sets the focus to Text2 but Text1's background color doesn't change.
Chris Eastwood
February 17th, 2000, 02:44 AM
Here you go :
place the following code in Form1 for your TextBoxes - I'm assuming here that they are called Text1 and Text2 by default (you'll need to change this if they're called anything else)
'
private Sub Text1_KeyUp(KeyCode as Integer, Shift as Integer)
'
' If Enter is pressed, then disable text1 and move to text2
'
If KeyCode = Asc(vbCr) then
EnableText1 false
End If
End Sub
'
private Sub Text1_LostFocus()
'
' Catch the tab / click away
'
EnableText1 false
End Sub
'
private Sub EnableText1(byval bEnable as Boolean)
Text1.Enabled = bEnable
'
If bEnable then
on error resume next ' just in case
Text2.SetFocus
Err.Clear
End If
End Sub
The 'EnableText1' procedure allows you to disable and re-enable it by passing false/true.
As for the right aligned textbox for numbers, this is possible, but only at design time - set the Alignment property of the text box to '1 - Right Justify'.
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
The Matrix
February 17th, 2000, 09:04 AM
whoops, set that timer interval to 1. thats the problem right there
Chris Eastwood
February 17th, 2000, 09:20 AM
That code will catch the 'Return' key for any event on the form and system wide, (eg. editing in notepad) not very useful for checking in Text1.
Checkout my earlier answer for a better / correct method.
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.