Send keys in VB6 not working on textbox - vb6

I have a webbrowser in VB6 that displays a web site from a third party company. I´m trying to paste text inside this website's textboxes with the code:
mainFrm.wbr(1).SetFocus
SendKeys "10"
It is working on most textboxes, except for one that has some keys disabled by the javascript funcion:
function only_numbers()
{
var key=window.event.keycode;
if (key < 48 || key > 57)
{
if(key != 44 || key != 8 || key != 46 || key != 96)
{
window.event.keycode=0;
}
}
}
Is there a way to bypass this script? Or maybe another way of setting the text of the textbox?
Thanks in advance.

I use VB6 Desktop application where I work. Always avoid using "SendKeys" but the use, use as follows:
SendKeys "{DOWN}"
In other words, when using "{" and "}"
We also have a function called "SoNumeros" where it validates what the final User are typing and allows only numbers.
we always in action "KeyPress" component:
Function SoNumeros(KeyAscii, Optional Nao_Aceita_Virgula_ou_Ponto As Integer, Optional ByVal aceitaValoresNegativos As Boolean = True, Optional ByVal aceitaBarra As Boolean = False)
If KeyAscii = Asc("-") And Not aceitaValoresNegativos Then KeyAscii = 0
If Nao_Aceita_Virgula_ou_Ponto = 0 Then
' If Not (IsNumeric(Chr(KeyAscii))) And KeyAscii 8 And KeyAscii Asc(",") And KeyAscii Asc(".") And KeyAscii Asc("-") Then
If Not (IsNumeric(Chr(KeyAscii))) And KeyAscii 8 And KeyAscii Asc(",") And KeyAscii Asc("-") Then
If aceitaBarra = False Then
KeyAscii = 0
Else
If KeyAscii Asc("/") Then
KeyAscii = 0
End If
End If
End If
Else
If Not (IsNumeric(Chr(KeyAscii))) And KeyAscii 8 And KeyAscii Asc("-") Then
If aceitaBarra = False Then
KeyAscii = 0
Else
If KeyAscii Asc("/") Then
KeyAscii = 0
End If
End If
End If
End If
End Function
I hope it helped you

Related

Mismatch problem with textbox and label! vb6

I have tried to change the code but it didnt works.
Here is my code:
Private Sub CTcash_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
bill = Val(CTcash.Text - CLtotaloutput.Caption)
If Val(bill) > 0 Then
Change.CLchange.Caption = Val(bill)
Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
Change.Show
End If
If Val(bill) < 0 Then
MsgBox "Not Enough Money", vbOKOnly, "Invalid"
End If
End If
End Sub
The highlighted code
bill = Val(CTcash.Text - CLtotaloutput.Caption)
Both CTcash.Text and CLTotaloutput.Caption are text strings so you can't subtract them directly. Try:
bill = Val(CTcash.text) - Val(CLtotaloutput.Caption)
You really should ensure that both strings are numerical first though!
#John Eason is correct. You also do not have to use Val on bill since it is already an integer. And you are not doing anything if bill is 0 but this could be fine based on your requirements.
Private Sub CTcash_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
bill = Val(CTcash.Text) - Val(CLtotaloutput.Caption)
If bill > 0 Then ' Note that when bill is 0 nothing happens but that might be fine
'Change.CLchange.Caption = bill 'Not sure why this is needed but it may be
Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
Change.Show
End If
If bill < 0 Then
MsgBox "Not Enough Money", vbOKOnly, "Invalid"
End If
End If
End Sub
I think a better approach would be to use a Select Case statement.
Private Sub CTcash_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
bill = Val(CTcash.Text) - Val(CLtotaloutput.Caption)
Select Case True
Case bill > 0
Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
Change.Show
Case bill < 0
MsgBox "Not Enough Money", vbOKOnly, "Invalid"
Case Else
' Do nothing - this documents that this is done on purpose
End Select
End If
End Sub
You can use IsNumeric function to check if the inputs are numeric and parse accordingly. Use the Right function to remove any non-numbers from the left side. For example, Right("$123", Len("$123") - 1) will remove the leading $.
To strip all non-numerical values from a string:
Dim ResultString As String
myString = "aaa34BB12,000xcv9.9zz"
Dim i As Integer
For i = 1 To Len(myString)
myChar = Mid(myString, i, 1)
If IsNumeric(myChar) = True Then
ResultString = ResultString + myChar
End If
Next
MsgBox ResultString
Note that this may cause an issue with decimal amounts since 1.23 will before 123. Not something you want. Try using the CCur function. I have never used it myself but it might be exactly what you want.
Source: https://www.vbforums.com/showthread.php?437526-RESOLVED-Removing-non-numeric-chars-from-a-string&p=2686411&viewfull=1#post2686411
At first I miss the const, i put it in the other sub:
Private Sub CTcash_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
bill = Val(CTcash.Text) - totalsum
Select Case True
Case bill > 0
Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
Change.Show
Case bill < 0
MsgBox "Not Enough Money", vbOKOnly, "Invalid"
Case Else
End Select
End If
End Sub
Private Sub CTbarcode_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case Is = vbKeyF6
totaldiscount = 200
totalsum = Val(CLsuboutput.Caption) - totaldiscount
CLtotaloutput.Caption = Format(totalsum, "Rp\. ###,###,###. ,-")
CLdiscoutput.Caption = Format(totaldiscount, "Rp\. ###,###,###. ,-")
CTcash.Enabled = True
CTcash.SetFocus
End Select
End Sub
Here is it after i edit it:
Private Sub CTcash_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
totaldiscount = 200
totalsum = Val(CLsuboutput.Caption) - totaldiscount
bill = Val(CTcash.Text) - totalsum
Select Case True
Case bill > 0
Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
Change.Show
Case bill < 0
MsgBox "Not Enough Money", vbOKOnly, "Invalid"
Case Else
End Select
End If
End Sub

Allow computer keypad form be controlled from keyboard

I have created a dialer in VB6 to dial a phone number. It has the following buttons: the digits 0-9, backspace, "Call", and "Disconnect". Now I want enter numbers in the form using the keypad part of the keyboard. In the text box, any key can be typed in using the keypad. But I only want the number pad to operate.
For that I checked ASCII values using the KeyPress event - seeing if the pressed key value lies in between 47 and 58. But along with the key values its ASCII values are also getting displayed in the text box.
One more thing - I have one MS Flex Grid on the form along with the dialer. So when the cursor is on the form, or if the mouse is clicked anywhere other than the text box, values don't display in the text box. So how do I always keep focus on the text box?
Dim val As Integer
Private Sub append(val As Integer)
Text1.Text = Text1.Text & val
End Sub
Private Sub Backspace_Click()
With Text1
'FOCUS TO THE TEXTBOX
.SetFocus
'PUT THE CURSOR AT THE END OF THE TEXT
.SelStart = Len(.Text)
'SEND THE KEY
SendKeys ("{BACKSPACE}")
'AND THATS IT :D
End With
End Sub
Private Sub key_0_Click()
val = 0
append val
End Sub
Private Sub key_1_Click()
val = 1
append val
End Sub
Private Sub key_2_Click()
val = 2
append val
End Sub
Private Sub key_3_Click()
val = 3
append val
End Sub
Private Sub key_4_Click()
val = 4
append val
End Sub
Private Sub key_5_Click()
val = 5
append val
End Sub
Private Sub key_6_Click()
val = 6
append val
End Sub
Private Sub key_7_Click()
val = 7
append val
End Sub
Private Sub key_8_Click()
val = 8
append val
End Sub
Private Sub key_9_Click()
val = 9
append val
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii > 47 And KeyAscii < 58) Then
val = KeyAscii
append val
Else
KeyAscii = 0
' Text1.Text = KeyAscii
End If
End Sub
'Private Sub Text1_LostFocus()
' Text1.SetFocus
'End Sub
You could set the KeyPreview property on the form to true.
When you have done that, put your code in the KeyPress of the form that was in the KeyPress of the Textbox, and set the text value of the textbox, or better still move the code into a function and set it from there, that way you won't need to duplicate your code if you need it in other places.
Have a look at this and see if it helps it is the KeyPreview from MSDN
Hi,
Sorry for the delay. To use the KeyPreview for the form give this a try:
Double click on the form in the project to open it
In the properties for the form find the KeyPreview Property and set it to true
Double click on the form to bring up the code window
Select the keypress event for the form, and add the following code, or something similar.
Private Sub Form_KeyPress(KeyAscii As Integer)
Text1.Text = Text1.Text & Chr$(NrOnly(KeyAscii))
End Sub
Use the function NrOnly that Hqrls has posted as that will allow only numbers which could help you with the validation. You won't need any code in the Text1_KeyPress, the form will now handle that for you.
Give it a try and let me know if you need any other information and I'll see what I can do.
[EDIT 29/05/2014]
Hi,
I've had a bit of a play around with the code and added some bits in which I think might help. Have a look at this and see if it make sense. If you copy it into your code then make sure you back up your original code just in case you need to get back to it.
This is the code if you keep your textbox
Option Explicit
Dim val As String
Dim m_blnTextHasFocus As Boolean 'Added this, so it knows wether the the textbox
'has the focus or not
'it is so the sendkeys doesn't get stuck in a
'loop with the key presses
Private Sub append(strIn As String)
'Changed the parameter from an int to a string, so we can use it in the key press events
Text1.Text = Text1.Text & strIn
End Sub
Private Sub Backspace_Click()
'Moved the code into it's own sub
DeleteAChar
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
'Handle a key press if the textbox doesn't have the focus but ignore the keypress if it does
If Not m_blnTextHasFocus Then
If IsNumeric(Chr$(KeyAscii)) Then
append Chr$(KeyAscii)
ElseIf KeyAscii = vbKeyBack Then
DeleteAChar
End If
End If
End Sub
Private Sub key_0_Click()
val = "0"
append val
End Sub
Private Sub key_1_Click()
val = "1"
append val
End Sub
Private Sub key_2_Click()
val = "2"
append val
End Sub
Private Sub key_3_Click()
val = "3"
append val
End Sub
Private Sub key_4_Click()
val = "4"
append val
End Sub
Private Sub key_5_Click()
val = "5"
append val
End Sub
Private Sub key_6_Click()
val = "6"
append val
End Sub
Private Sub key_7_Click()
val = "7"
append val
End Sub
Private Sub key_8_Click()
val = "8"
append val
End Sub
Private Sub key_9_Click()
val = "9"
append val
End Sub
Private Sub Text1_GotFocus()
m_blnTextHasFocus = True
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
'Check the keyascii value and ignore it if it isn't numeric or backspace
If Not IsNumeric(Chr$(KeyAscii)) And KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End Sub
Private Sub DeleteAChar()
With Text1
'FOCUS TO THE TEXTBOX
.SetFocus
'PUT THE CURSOR AT THE END OF THE TEXT
.SelStart = Len(.Text)
'SEND THE KEY
SendKeys ("{BACKSPACE}")
'AND THATS IT :D
End With
End Sub
Private Sub Text1_LostFocus()
m_blnTextHasFocus = False
End Sub
If you don't need to keep the textbox then you could replace it with a label and have a try with that. It would remove the need to keep track of if the textbox has got the focus or not. Have a go with this and see if it helps any. The only other thing is to make sure KeyPreview is turned on for the form or the keypress code will only work for the form if it has the focus.
Is the textbox the only control you want to have the focus ?
In that case you can use the following code:
Private Sub Text1_LostFocus()
Text1.SetFocus
End Sub
If there are any other controls you want to be able to have the focus as well, then you can use Text1.SetFocus in the _GotFocus() event of the controls which you do not want to have the focus
Another possible solution would be to use the _KeyPress() event of the other controls as well, and convert your current Text1_KeyPress() event to a general function which can be called by the _KeyPress() event of the other controls .. make sure though that you send the output of this general function to the correct textbox
If you post the relevant parts of your code we might be able to give a more specific answer
[EDIT]
for the answer to your original question, go with the answer of lardymonkey using keypreview on the form
an example function for allowed only numeric keys and the backspace:
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = NrOnly(KeyAscii)
End Sub
Private Function NrOnly(intAscii As Integer) As Integer
Dim intReturn As Integer
intReturn = intAscii
Select Case intAscii
Case vbKeyBack
Case vbKey0 To vbKey9
Case Else
intReturn = 0
End Select
NrOnly = intReturn
End Function

Dynamic image change on keypress VB6

So I'm trying to make this form in VB6 with just an image, and whenever you press say the "Q" key that image changes, and if you press "E" it changes to back to the previous one. Simple stuff.
Here's what I have:
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 81 Then Image.Picture = LoadPicture("E:\Imagenes\Avatars\4.jpg")
If KeyAscii = 69 Then Image.Picture = LoadPicture("E:\Imagenes\Avatars\3.gif")
End Sub
Well, that doesn't work. What could I do to make it work? Thanks!
In reading your comment, you say that you are using the Default Image Control, there is not a Default Image Control you need to add an instance of it to your Form or just use the Forms Picture Property.
These examples work for me:
Private Sub Form_KeyPress(KeyAscii As Integer)
If (Chr(KeyAscii) = "Q" Or Chr(KeyAscii) = "q") Then Form1.Picture = LoadPicture("E:\Imagenes\Avatars\4.jpg")
If (Chr(KeyAscii) = "E" Or Chr(KeyAscii) = "e") Then Form1.Picture = LoadPicture("E:\Imagenes\Avatars\3.gif")
End Sub
and
Private Sub Form_KeyPress(KeyAscii As Integer)
If (Chr(KeyAscii) = "Q" Or Chr(KeyAscii) = "q") Then Image1.Picture = LoadPicture("E:\Imagenes\Avatars\4.jpg")
If (Chr(KeyAscii) = "E" Or Chr(KeyAscii) = "e") Then Image1.Picture = LoadPicture("E:\Imagenes\Avatars\3.gif")
End Sub

Flex grid non editable column

I don't want to edit some of the column in flex gird.
Flex Grid
column1, column2, .... column35
i want to edit from column1... column10 only, remaining columns i don't want to edit or type.
How to do in vb6.
I believe the MS Flex Grid was designed for displaying data and not editing. If you need to edit cell data you can accomplish it using the Flex Grid using an approach of superimposing a textbox at runtime to capture user data entry and set the "Text" property of the cell in code. Otherwise you can choose to use a different control.
Here are some examples of the aforementioned approach:
http://support.microsoft.com/kb/241355
http://www.vb-helper.com/howto_edit_flexgrid_control.html
I've made a special user control in VB6 to an editable grid. If you want I can send you a copy.
The code I use to enable to edit a cell is the follow:
Private Sub fg_KeyDown(KeyCode As Integer, Shift As Integer)
Dim Cancel As Boolean
Dim Idc As Long
Dim x
If KeyCode = vbKeyEscape And Shift = 0 Then
If Not fgLocked Then
If fgRowChanged Then
RaiseEvent BeforeRestoreBuffer
For Idc = 1 To UBound(fgBuffer)
x = fgBuffer(Idc)
fgValues(Idc, fg.Row) = x
If fgColFormat(Idc) = "*" And fgBuffer(Idc) <> "" Then
fg.TextMatrix(fg.Row, Idc) = "*******"
ElseIf fgColFormat(Idc) = "RTF" Then
fg.TextMatrix(fg.Row, Idc) = Format(fgBuffer(Idc), "")
Else
fg.TextMatrix(fg.Row, Idc) = Format(fgBuffer(Idc), fgColFormat(Idc))
End If
Next
fgRowChanged = False
RaiseEvent RestoreBuffer
End If
End If
ElseIf KeyCode = vbKeyReturn And Shift = 0 Then
NextCell
ElseIf KeyCode = vbKeyF2 And Shift = 0 Then
If Not fgLocked Then
If fgColFormat(fg.Col) = "RTF" Then
CellEditBig fgValues(fg.Col, fg.Row)
Else
CellEdit fgValues(fg.Col, fg.Row)
End If
End If
ElseIf KeyCode = vbKeyF2 And Shift = vbShiftMask Then
If Not fgLocked Then
CellEditBig fgValues(fg.Col, fg.Row)
End If
ElseIf KeyCode = vbKeyDelete And Shift = 0 Then
If Not fgLocked Then
RaiseEvent BeforeDelete(Cancel)
If Not Cancel Then
If fg.Rows = fg.FixedRows + 1 Then
fg.AddItem ""
If fgRowNumber Then fg.TextMatrix(fg.Rows - 1, 0) = fg.Rows - 1
fgValues_AddItem ""
End If
fg.RemoveItem fg.Row
If fgRowNumber Then Renumera
fgValues_RemoveItem fg.Row
LoadBuffer fg.Row
RaiseEvent AfterDelete
End If
End If
ElseIf KeyCode = vbKeyInsert And Shift = 0 Then
If Not fgLocked Then
RaiseEvent BeforeInsert(Cancel)
If Not Cancel Then
fg.AddItem "", fg.Row
If fgRowNumber Then Renumera
fgValues_AddItem "", fg.Row
RaiseEvent AfterInsert
End If
End If
Else
RaiseEvent KeyDown(KeyCode, Shift)
End If
End Sub

How to focus the next cell while clicking tab key and cell coloring

I am new to vb
Flexgrid
Header 01 .... 31
Values .........
I am entering the values at run time in flexgrid cell, if i click tab button, the focus will move to next cell on the same row.
Code for Ascii
Private Sub flexgrid_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57
flexgrid.Text = flexgrid.Text & Chr(KeyAscii)
Case 46 'Dot
flexgrid.Text = flexgrid.Text & Chr(KeyAscii)
Case 8
If Len(flexgrid.Text) > 0 Then
flexgrid.Text = Left(flexgrid.Text, (Len(flexgrid.Text) - 1))
End If
Case Else
KeyAscii = 0
Beep
End Select
End Sub
How to do this.
And also how to change the particular cell background color.
Code
For i = 1 To flexgrid.Rows - 1
flexgrid.TextMatrix(i, 33) = vbred 'It's giving value like '255'
flexgrid.TextMatrix(i, 33) = .CellBackColor = vbred 'It's giving value 'False'
Next i
Any ideas & suggestion...?
To move the selected column use the KeyDown (or KeyUp if you prefer) event and place your code there.
Private Sub MSFlexGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = Asc(vbTab) Then
If MSFlexGrid1.Col < MSFlexGrid1.Cols - 1 Then
MSFlexGrid1.Col = MSFlexGrid1.Col + 1
End If
End If
End Sub
To change the cell background color first set the cell, then set the CellBackColor.
flexgrid.Row = i
flexgrid.Col = 33
flexgrid.CellBackColor = vbRed

Resources