|
-
Sep 22nd, 2005, 02:49 PM
#1
Thread Starter
Frenzied Member
generic function containing a textbox
Hello
I have got about 14 textboxes which use similar routines, the routines read and write to the textboxes the same way, the only difference is these routines is the name of the textbox.
is it possible for me to create a generic routine which will manipulate the textboxes in the same way.
e.g i have 5 textboxes,
footext1, footext2, footext3, footext4 & footext5
now the routine is say
VB Code:
footext1_change()
uppervalue = 100
lowervalue = footext2.text
if ((footext1.text<uppervalue ) and (footext1.text>lowervalue)) then
error_flag=true
else
footext1 = 30
error_flag = false
end if
end sub
and for text2 it is
VB Code:
footext2_change()
uppervalue = 100
lowerrvalue = footext3.text
if ((footext2.text<uppervalue ) and (footext2.text>lowervalue)) then
error_flag=true
else
footext2 = 30
error_flag = false
end if
end sub
with 15 textboxes i would have to repeat the code 15 times
now if i had some sort of textbox i could have one routine and call it from every textboxes change method
something like
VB Code:
private function call_common_routine(uppervalue,lowervalue,currenttextbox)
if ((currenttextbox<uppervalue) and (currenttextbox>lowervalue)) then
error_flag=true
else
currenttextbox = 30
error_flag = false
end if
end function
footext2_change()
uppervalue = 100
lowerrvalue = footext3.text
currenttextbox = footext2.text
call_common_routine(uppervalue,lowervalue,currenttextbox)
end sub
[/Highlight]
is such a routine possible, passing textboxes, the way i do it it just copies the textbox contents to variable and passes it, i wanted to pass the current textbox "handle(?)" to the common routine.
Last edited by vb_student; Sep 24th, 2005 at 11:56 AM.
-
Sep 22nd, 2005, 02:53 PM
#2
Re: generic function containing a textbox
It's better you make them a control array so that only one functio will work for all of them.
Pradeep
-
Sep 22nd, 2005, 03:00 PM
#3
Re: generic function containing a textbox
or create a sub
VB Code:
Private Sub SetErrorFlag(lwrtxt As TextBox, txt As TextBox)
uppervalue = 100
lowervalue = lwrtxt.Text
If ((txt.Text < uppervalue) And (txt.Text > lowervalue)) Then
error_flag = True
Else
txt = 30
error_flag = False
End If
End Sub
then in footext1_change
SetErrorFlag footext2,footext1
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Sep 22nd, 2005, 03:03 PM
#4
Re: generic function containing a textbox
If yoou make them a control array, this is how it would work:
VB Code:
Private Sub footext1_Change(Index As Integer)
uppervalue = 100
lowervalue = footext2.Text
If ((footext1(Index).Text < uppervalue) And (footext1(Index).Text > lowervalue)) Then
error_flag = True
Else
footext1(Index) = 30
error_flag = False
End If
End Sub
Pradeep
-
Sep 22nd, 2005, 03:12 PM
#5
Thread Starter
Frenzied Member
Re: generic function containing a textbox
thanks for the replies, i should have used the control arrays
i have got lots of code and i think i need to stick with the non control array
i liked statics method.
so i can pass textboxes like so
VB Code:
Private Sub SetErrorFlag(lwrtxt As TextBox, txt As TextBox)
uppervalue = 100
lowervalue = lwrtxt.Text
If ((txt.Text < uppervalue) And (txt.Text > lowervalue)) Then
error_flag = True
Else
txt = 30
error_flag = False
End If
End Sub
i guess then i can call the function like so
VB Code:
seterrorflag(footext1.text,footext2.text)
why do i need to create a sub, would not a function do the same thing?
i mean what is wrong with
[Highlight=VB]
VB Code:
Private function SetErrorFlag(lwrtxt As TextBox, txt As TextBox)
uppervalue = 100
lowervalue = lwrtxt.Text
If ((txt.Text < uppervalue) And (txt.Text > lowervalue)) Then
error_flag = True
Else
txt = 30
error_flag = False
End If
End function
-
Sep 22nd, 2005, 03:14 PM
#6
Re: generic function containing a textbox
When a function is not returning anything it is equivalent to a Sub.
-
Sep 22nd, 2005, 04:50 PM
#7
Thread Starter
Frenzied Member
Re: generic function containing a textbox
so the only difference between a function and a sub is that a function can return a value whereas a sub cannot.
and i could do
VB Code:
Private function SetErrorFlag(lwrtxt As TextBox, txt As TextBox)
or
VB Code:
Private sub SetErrorFlag(lwrtxt As TextBox, txt As TextBox)
-
Sep 22nd, 2005, 08:55 PM
#8
Re: generic function containing a textbox
correct both will work.. but if you need to return a value, you would use function
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Sep 23rd, 2005, 12:13 AM
#9
Re: generic function containing a textbox
vb_student, didn't you start a couple of threads a while back on the difference between subs and functions?
Anyway, a sub IS a function, but the result is handled internally by VB.
-
Sep 24th, 2005, 11:55 AM
#10
Thread Starter
Frenzied Member
Re: generic function containing a textbox
thanks for the replies penagate
i think i did, i had forgotten the difference for a while
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|