How to detect sended key will write in a textbox?
for example:
i send "W" to a program which is active
how can i detect that "W" is written?
It all depends on how you send the "W" to a program....
If you mean if you actually type, you can use _KeyDown, _KeyUp or _KeyPress events of the textbox..
Private Sub Textbox_KeyPress(KeyAscii As Integer)
If KeyAscii = Asc("W") then Call MsgBox("Yes 'W' pressed")
End Sub
Private Sub TextBox_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyW And ((Shift And vbShiftMask) = vbShiftMask) Then Call MsgBox("Yes 'W' pressed")
End Sub
OR you need to track changes using the _Change event, which ofcourse is a lot more work as you'll need to store the previous text and compare it with the current text, and see if a 'W' has been added. But you have to take into account that people might actually paste more than one letter.
Private Sub TextBox_Change()
If Instr(TextBox.Text, "W")>0 Then
If Instr(TextBox.Tag, "W") = 0 Then
Call MsgBox("Yep 'W' added")
Else
'run down the Text along with the Tag and see if a 'W' is present at the same location
End If
End If
TextBox.Tag = TextBox.Text
End Sub
Related
I have a text box in a form in MS Access. I just want users to enter Letters and spaces. No special characters.
I have implemented the following on KeyPress Event. I am wondering is there any better way to implement the same.
Private Sub txtName_KeyPress(KeyAscii As Integer)
If KeyAscii <> 8 And KeyAscii <> 32 Then 'Not Backspace (important for error correction) and not a Space
If (KeyAscii < 65 Or KeyAscii > 90) And (KeyAscii < 97 Or KeyAscii > 122) Then 'Allowing lower and upper case
Beep 'Let the user know they hit an illegal key
KeyAscii = 0 'Don't let the keystroke through
End If
End If
End Sub
Consider using the BeforeUpdate event of the textbox which is the trigger usually used to validate entry as you have the facility to cancel the update:
Private Sub txtWordsOnly_BeforeUpdate(Cancel As Integer)
If Me.txtWordsOnly Like "*[0-9]*" Or Me.txtWordsOnly Like "*[##$%*^&?()<>/\'""!]*" Then
MsgBox "Invalid entry. Please select only words," _
& " NOT numbers or special characters.", vbInformation
Cancel = -1 ' OR Cancel = True
Me.txtWordsOnly.Undo
End If
End Sub
Use a validation rule.
Allen Browne has a highly useful list of Validation rules here:
http://allenbrowne.com/ValidationRule.html
Letters and spaces only
Is Null Or Not Like "*[!a-z OR "" ""]*"
Punctuation and digits rejected.
I need to create a text box when the user press the key will fill the pattern:
[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}
A valid example is:
63CDB75C-D58A-4100-8C24-9E6433E263B0
The user can only press A..F and 0..9 on the groups 8-4-4-4-12 total digits.
But I don't know how to format it on the vb6 text box. can you help me?
The easy way is to check the KeyPress event and decide if the key is valid or not. It appears you have a well defined format so I added a textbox to a form and set it's maximum length to 36. I added code in the KeyPress event to check for valid keys. I also added code in the Changed event to append the hyphen to the current text instead of making the user enter it. Of course this is very simple so you may need to add to it to work specifically for you. For instance as it is now you can't paste into the textbox with Ctrl + V. Further, the hyphen key isn't currently allowed so if they delete a hyphen they will have to start over to get it back. Also there is no Error handler.
Private Sub Text1_Change()
If Len(Text1.Text) = 8 _
Or Len(Text1.Text) = 13 _
Or Len(Text1.Text) = 18 _
Or Len(Text1.Text) = 23 Then
Text1.Text = Text1.Text & "-"
Text1.SelStart = Len(Text1.Text)
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = Asc(UCase$(Chr$(KeyAscii))) 'force all characters to upper case
Select Case KeyAscii
Case 8, 48 To 57, 65 To 70 '8 is the backspace, 48 to 57 is 0 to 9, 65 to 70 is A to F
Case Else
KeyAscii = 0 'Cancel the key
End Select
End Sub
I am doing a marks updating database system. I need to limit each of my textbox to be in the value of less than 100, when its over than 100 or when its not number, a message box will pop up and the data won't be save until the user change the mistake. How can I do it?
I agree with Hiren Pandya, but I thought I would add my own take as well.
Be aware that converting a string to a numerical value is not trivial, but the Val, CInt, CDBl etc. functions in VB6 can all give you behavior that close to what you want. (some of those links are for VB.Net, but can still be valuable). You want to make sure you are thinking about digit grouping, positive/negative, decimal separators, etc. when you are validating user input on your own. Most of the time, the built-in functions are good enough.
Private Sub Text1_Change()
On Error GoTo Err_Handler
Dim text As String
text = Text1.text
If IsNumeric(text) = True Then
'If you only want integers...
Dim value As Integer
value = Val(text)
If value <= 100 And value > 0 Then
'The value is good so whatever stuff you need to do
'And then leave the procedure
Exit Sub
End If
End If
'Let everything else fall through here...
Err_Handler:
MsgBox "Invalid input."
'Other stuff to prevent saving
End Sub
In the properties of the text box, set MaxLength to 2.
If you want a message, in the text box Change event, you could do...
If Len(txtBox.Text)>2 then msgbox...
then add your message in the messagebox.
I could go into more detail if you need it. Some thing like below...
Private Sub Text1_Change()
If Len(Text1) > 6 Then
Text1 = " "
MsgBox "Not more than six"
Text1.SetFocus
End If
End Sub
I need to enter values in text box and when ever i press enter i have to save the text box values into an array.How can i achieve this functionality?
I would use a separate counter variable to redefine the size of the array, like this:
Option Explicit
Dim myArr() As String '~~~ dynamic array
Dim lngCnt As Long '~~~ a counter variable that keep track of the index
' inital stage..
Private Sub Form_Load()
lngCnt = 0
End Sub
' on KeyPress
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then '~~~ if Enter Key is pressed..
ReDim Preserve myArr(lngCnt) '~~~ reclare the array with the new size, while preserving any elements it may contain
myArr(lngCnt) = Text1.Text '~~~ store the line
Text1.Text = "" '~~~ empty the textbox, so that you could type the next line
lngCnt = lngCnt + 1 '~~~ increment the counter, which we would use as size during the next keypress
End If
End Sub
' to display the elements
Private Sub Command1_Click()
Dim i As Long
For i = LBound(myArr) To UBound(myArr) '~~~ loop through the elements(from Lowerbound to Upperbound)..
Debug.Print myArr(i) '~~~ ..and display the item.
Next
End Sub
If you just want a new entry to be added each time they press the button, then use something like:
Redim Preserve YourArray(LBound(YourArray) To UBound(YourArray) + 1)
YourArray(UBound(YourArray)) = TextBox.Text
Note that this can get very slow and inefficient when the array contains large numbers of items as it's reallocating memory each time.
A better method would involve expanding the size of the array in chunks, while keeping track of the last valid entry.
Just give same name to all the text Boxes
The short answer is to use split to break the string up (you will need to tell the user what char to use to split on).
Long answer don't change the user interface to a repeater and have them use an input per value start here http://blogs.microsoft.co.il/blogs/basil/archive/2008/08/20/javascript-repeater-control-datarepeater-using-jquery-presenter-1-0-8-uicontrols-library.aspx
Else you will be debugging for ever.
How to a raise a keypress event for Special Character for typing extended ASCII Characters ?
Sample code snippet :
keybd_event vbKeyE, 0, KEYEVENTF_KEYDOWN, 0
Is there any alternative so that I can use KeyCode for Special character [ E.g. Chr$(122) ] instead of predefined and limited VbKeyA, VbKeyB, VbKeyC ..... etc ?
Waiting for your reply.
Regards,
Shankar
Here is an example that seems to cover your question:
Private Sub Text1_KeyPress (KeyAscii As Integer)
If KeyAscii = 8 Then MsgBox "You pressed the BACKSPACE key."
End Sub
It was taken from here.
if keyascii=27 then
if (msgbox"do you want to exit?",vbokcancel,"information")=vbok then
end
end if
end if