Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 25th, 2003, 12:19 PM
drew010 drew010 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: california usa
Posts: 346 drew010 User rank is Sergeant (500 - 2000 Reputation Level)drew010 User rank is Sergeant (500 - 2000 Reputation Level)drew010 User rank is Sergeant (500 - 2000 Reputation Level)drew010 User rank is Sergeant (500 - 2000 Reputation Level)drew010 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 9 h 41 m
Reputation Power: 12
Send a message via ICQ to drew010 Send a message via AIM to drew010 Send a message via Yahoo to drew010
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.

Reply With Quote
  #2  
Old September 25th, 2003, 02:17 PM
nopoints nopoints is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Windsor ON, Canada
Posts: 459 nopoints User rank is Corporal (100 - 500 Reputation Level)nopoints User rank is Corporal (100 - 500 Reputation Level)nopoints User rank is Corporal (100 - 500 Reputation Level)nopoints User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 13 h 44 m 22 sec
Reputation Power: 8
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

Reply With Quote
  #3  
Old September 25th, 2003, 03:47 PM
drew010 drew010 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: california usa
Posts: 346 drew010 User rank is Sergeant (500 - 2000 Reputation Level)drew010 User rank is Sergeant (500 - 2000 Reputation Level)drew010 User rank is Sergeant (500 - 2000 Reputation Level)drew010 User rank is Sergeant (500 - 2000 Reputation Level)drew010 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 9 h 41 m
Reputation Power: 12
Send a message via ICQ to drew010 Send a message via AIM to drew010 Send a message via Yahoo to drew010
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

Reply With Quote
  #4  
Old September 26th, 2003, 11:00 AM
cleverpig cleverpig is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2003
Posts: 1,152 cleverpig User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via MSN to cleverpig
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...

Reply With Quote
  #5  
Old September 26th, 2003, 11:26 AM
drew010 drew010 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: california usa
Posts: 346 drew010 User rank is Sergeant (500 - 2000 Reputation Level)drew010 User rank is Sergeant (500 - 2000 Reputation Level)drew010 User rank is Sergeant (500 - 2000 Reputation Level)drew010 User rank is Sergeant (500 - 2000 Reputation Level)drew010 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 9 h 41 m
Reputation Power: 12
Send a message via ICQ to drew010 Send a message via AIM to drew010 Send a message via Yahoo to drew010
thanks a ton! i greatly appreciate the help

Reply With Quote
  #6  
Old September 29th, 2003, 08:57 AM
Shocka's Avatar
Shocka Shocka is offline
dont click here
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 409 Shocka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 35 m
Reputation Power: 7
not to steal someone elses post, but can i ask what ByRef means/does
__________________
quick fixs are never solutions
Ritesh's Corner

Reply With Quote
  #7  
Old September 29th, 2003, 11:23 AM
drew010 drew010 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: california usa
Posts: 346 drew010 User rank is Sergeant (500 - 2000 Reputation Level)drew010 User rank is Sergeant (500 - 2000 Reputation Level)drew010 User rank is Sergeant (500 - 2000 Reputation Level)drew010 User rank is Sergeant (500 - 2000 Reputation Level)drew010 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 9 h 41 m
Reputation Power: 12
Send a message via ICQ to drew010 Send a message via AIM to drew010 Send a message via Yahoo to drew010
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.

Reply With Quote
  #8  
Old October 2nd, 2003, 11:20 PM
Fisherman's Avatar
Fisherman Fisherman is offline
Inherits Programmer.Slacker
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2003
Location: Between my Id and your Ego
Posts: 2,172 Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 9 h 9 m 36 sec
Reputation Power: 111
Send a message via ICQ to Fisherman Send a message via AIM to Fisherman
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > turning a simple keypress sub into a reusable function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway