Textbox skills
When using the Visual Basic development application, the TextBox control is the most common (especially for database programs), the following is some techniques when developing applications, I hope to help friends with VB.
1. Limit Textbox only to enter numbers.
We only need to judge whether Keyascii can achieve the goality between 48-57, but we will use the retracted key, so you must add Keyascii <> 8.
Private sub text1_keypress (Keyascii As INTEGER) if keyascii <> 8 and keyascii <48 or keyascii> 57 Then Beep keyascii = 0 end iFend Sub
2. Automatically turn the input in English to lowercase / uppercase.
If you use ucase $ and LCase $ in the keypress event, then when you enter ABC, the result in TextBox will be CBA (specific reason is not much here), we have to judge its keyascii, just between uppercut and lowercase 32, so ... look at the procedure below.
'Capital turn lowercase private sub text1_keypress (keyascii as integer) if keyascii> = 65 and keyascii <= 90 Then Keyascii = keyascii 32 End IFEND SUB
'Small write bigwide private sub text2_keypress (Keyascii as integer) if keyascii> = 97 and keyascii <= 122 Then Keyascii = Keyascii - 32 End IFEND SUB
3, the method of synchronizing two TextBox.
How to explain this method? The second is equal to the first one :-).
Private sub text1_keypress (Keyascii as integer) text2 = text1nd Sub
4. After pressing the ENTER key, the cursor is automatically moved to the next control.
When Keyascii is 13 (Enter), the Send gives it a Tab button.
Private sub text1_keypress (keyascii as integer) if keyascii = 13 Then SendKeys "{tab}" Keyascii = 0 End IFEND SUB
5. Automatically select the entire string of text when the cursor moves to TextBox.
When the object gets the focus, select (SELSTART = 0) from the foremost, the length is selected as the length of the text (Len (Text1)).
Private sub text1_gotfocus () text1.selstart = 0 text1.sellength = len (text1) End Sub
6. Change its text color when the cursor moves to TextBox.
When TextBox gets the focus (Gotfocus) Change the text color, if the focus is lost (Lostfocus) will restore the text color, and ForeColor is the foreground color of the text.
'When the cursor moves to TextBox, the text is set to red private sub text1_gotfocus () text1.foretruor = vbrednd sub' When the cursor is moving the textBox text Set to Black Private Sub Text1_lostfocus () Text1.ForeColor = VBLACKEND SUB
The above code can change the background color of the TextBox as long as Text1.ForeColor is changed to Text1.backColor.