|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
turning a simple keypress sub into a reusable function
Private Sub Text7_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 8 Then KeyAscii = 0 End If End Sub I found this snipped of code below on abstractvb.com I'm messing around trying to turn it into a function so I can use it on several text boxes. I cant seem to make it into a function and have it work I have something like: Private Sub Text7_KeyPress(KeyAscii As Integer) returnOnlyNumeric (KeyAscii) End Sub where the function returnOnlyNumeric is a very slightly modified version of the code above. but its letting me enter letters still. can anyone help? thanks. |
|
#2
|
|||
|
|||
|
Code:
Public Sub ReturnOnlyNumeric(ByRef KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
does it look like that?
__________________
Programmer's Corner |
|
#3
|
|||
|
|||
|
Code:
Private Function returnOnlyNumeric(KeyAscii As Integer) As Integer
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 8 Then
returnOnlyNumeric = 0
Else:
returnOnlyNumeric = KeyAscii
End If
End Function
' The above function doesnt work if called in a keypress sub
Private Sub Text7_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
' The above sub works fine
|
|
#4
|
|||
|
|||
|
Hi drew010!U must do it as:
Private Function returnOnlyNumeric(Byref KeyAscii As Integer) As Integer If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 8 Then returnOnlyNumeric = 0 Else: returnOnlyNumeric = KeyAscii End If End Function ' The above function doesnt work if called in a keypress sub Private Sub Text7_KeyPress(KeyAscii As Integer) If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 8 Then KeyAscii = 0 End If End Sub ' The above sub works fine Add ByRef... |
|
#5
|
|||
|
|||
|
thanks a ton! i greatly appreciate the help
|
|
#6
|
||||
|
||||
|
not to steal someone elses post, but can i ask what ByRef means/does
|
|
#7
|
|||
|
|||
|
ByRef means pass by Reference. so when you send the value of the variable to the function, instead of copying it and creating a new variable for the function, it just sends the memory address of the existing variable. saves on cpu time and memory.
|
|
#8
|
||||
|
||||
|
even more than that, though... ByRef has profound influences on program execution. Passing values by reference passes the actual physical memory address of the variable. Thus, any functions operating on that variable will modify the original value through the memory address. Passing values ByVal passes the value contained in the variable's memory address - thus, each function receiving a variable by value receives only a copy of the original, and subsequent operations do not influence the value of the original value. This has huge impact when you are working with classes or assembling dll's
__________________
Fisherman "Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - A.Einstein |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > turning a simple keypress sub into a reusable function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|