Results 1 to 10 of 10

Thread: generic function containing a textbox

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Resolved 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:
    1. footext1_change()
    2.  
    3.       uppervalue = 100
    4.        lowervalue = footext2.text
    5.        if ((footext1.text<uppervalue ) and (footext1.text>lowervalue)) then
    6.               error_flag=true
    7.        else
    8.                footext1 = 30
    9.                 error_flag = false
    10.        end if
    11.  
    12. end sub

    and for text2 it is

    VB Code:
    1. footext2_change()
    2.  
    3.       uppervalue = 100
    4. lowerrvalue = footext3.text
    5.        if ((footext2.text<uppervalue ) and (footext2.text>lowervalue)) then
    6.               error_flag=true
    7.        else
    8.                footext2 = 30
    9.                 error_flag = false
    10.        end if
    11.  
    12. 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:
    1. private function call_common_routine(uppervalue,lowervalue,currenttextbox)
    2.        if ((currenttextbox<uppervalue) and (currenttextbox>lowervalue)) then
    3.               error_flag=true
    4.        else
    5.                currenttextbox = 30
    6.                 error_flag = false
    7.        end if
    8. 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.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: generic function containing a textbox

    or create a sub

    VB Code:
    1. Private Sub SetErrorFlag(lwrtxt As TextBox, txt As TextBox)
    2.     uppervalue = 100
    3.        lowervalue = lwrtxt.Text
    4.        If ((txt.Text < uppervalue) And (txt.Text > lowervalue)) Then
    5.               error_flag = True
    6.        Else
    7.                txt = 30
    8.                 error_flag = False
    9.        End If
    10. 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"

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: generic function containing a textbox

    If yoou make them a control array, this is how it would work:
    VB Code:
    1. Private Sub footext1_Change(Index As Integer)
    2.       uppervalue = 100
    3.        lowervalue = footext2.Text
    4.        If ((footext1(Index).Text < uppervalue) And (footext1(Index).Text > lowervalue)) Then
    5.               error_flag = True
    6.        Else
    7.                footext1(Index) = 30
    8.                 error_flag = False
    9.        End If
    10.  
    11. End Sub

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    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:
    1. Private Sub SetErrorFlag(lwrtxt As TextBox, txt As TextBox)
    2.     uppervalue = 100
    3.        lowervalue = lwrtxt.Text
    4.        If ((txt.Text < uppervalue) And (txt.Text > lowervalue)) Then
    5.               error_flag = True
    6.        Else
    7.                txt = 30
    8.                 error_flag = False
    9.        End If
    10. End Sub

    i guess then i can call the function like so
    VB Code:
    1. 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:
    1. Private function SetErrorFlag(lwrtxt As TextBox, txt As TextBox)
    2.     uppervalue = 100
    3.        lowervalue = lwrtxt.Text
    4.        If ((txt.Text < uppervalue) And (txt.Text > lowervalue)) Then
    5.               error_flag = True
    6.        Else
    7.                txt = 30
    8.                 error_flag = False
    9.        End If
    10. End function

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: generic function containing a textbox

    When a function is not returning anything it is equivalent to a Sub.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    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:
    1. Private function SetErrorFlag(lwrtxt As TextBox, txt As TextBox)

    or

    VB Code:
    1. Private sub SetErrorFlag(lwrtxt As TextBox, txt As TextBox)

  8. #8
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    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"

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    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
  •  



Click Here to Expand Forum to Full Width