Results 1 to 10 of 10

Thread: How do I pass a control array to a sub???!?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2000
    Posts
    103
    Trying to pass a control array of text boxes to a sub where I can do work on it.

    The form has a text1 textbox control array on it

    Public Sub form_load()

    Dim i As Integer

    For i = 1 To 5
    Load frmMain.Text1(i)
    frmMain.Text1(i).Top = frmMain.Text1(i - 1).Top + 50
    frmMain.Text1(i).Visible = True
    Next i

    InsertValues(Text1) '????

    end sub




    Public Sub InsertValues(theControl as ??)


    dim i as integer

    for i = 0 to 5
    theControl(i).text = 100
    next i


    end sub

    I keep getting arguement not optional no matter what I try. Help????


  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'you can't contain the name of a control
    'in a variable and access it..just doesn't work
    'you can accomplich the feat using a public variable
    'and a function...but it's not generic...
    
    Public newValue As String
    
    Public Sub form_load()
    
    Dim i As Integer
    
    For i = 1 To 5
    Load frmMain.Text1(i)
    frmMain.Text1(i).Top = frmMain.Text1(i - 1).Top + 800
    frmMain.Text1(i).Visible = True
    Next i
    
    newValue = 200
    InsertValue
    
    
    End Sub
    
    
    Public Function InsertValue()
    
    Dim i As Integer
    
    For i = 0 To Text1.Count - 1
    Text1(i).Text = newValue
    Next i
    
    
    End Function
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    There's probably a better/simpler way, but this works
    Code:
    Option Explicit
    
    Private Sub Form_Load()
    Dim i As Integer
    
    For i = 1 To 5
    Load frmMain.Text1(i)
    frmMain.Text1(i).Top = frmMain.Text1(i - 1).Top + 50
    frmMain.Text1(i).Visible = True
    Next i
    
    InsertValues (Text1(0).Name)
    
    End Sub
    
    
    Public Sub InsertValues(theControl As String)
    
    
    Dim ctl As Control
    
    For Each ctl In Controls
        If ctl.Name = theControl Then
            ctl.Text = 100
        End If
    Next
        
    End Sub

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2000
    Posts
    103
    nope... that doesn't help at all. I want to pass a control array to a sub. I can pass a control to a sub, why can't I pass a control array?

  5. #5
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    the control array is not accessed by name. if you type text1.name you can't get it...it is accessed by index
    text1(0)

    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    nope... that doesn't help at all. I want to pass a control array to a sub. I can pass a control to a sub, why can't I pass a control array?
    Because in VB as in life, you can't always do what you want to do.

  7. #7
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    And, as in life, there are multiple ways to do things. Declare the control array "global" or "public"

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I would have to disagree with Martin here, you can pass the control array as an object. But you would have to use the item property, since vb can't determine it's default.
    Code:
    Public Sub InsertValues(theControl As Object)
    Dim ctl As Control
    
      For Each ctl In thecontrol
        ctl.Text = 100
      Next
        
    End Sub
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    Originally posted by kedaman
    I would have to disagree with Martin here, you can pass the control array as an object. But you would have to use the item property, since vb can't determine it's default.
    Code:
    Public Sub InsertValues(theControl As Object)
    Dim ctl As Control
    
      For Each ctl In thecontrol
        ctl.Text = 100
      Next
        
    End Sub
    Heh. Coming to theaters soon:

    "Clash of the Guru's"


  10. #10
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    You can expand the Procedure to work for either a single Control or a Control Array, also you don't need to qualify the property you want to set, once the Control is enumerated in the For .. Each statement VB knows what the Default property is, i.e.
    Code:
    Sub SetControlValue(ByRef oControl As Object, ByVal NewValue As Variant)
        Dim oSubControl As Control
    
        If Not TypeOf oControl Is Control Then
            For Each oSubControl In oControl
                oSubControl = NewValue
            Next
        Else
            oControl = NewValue
        End If
    End Sub
    Example:
    Code:
    Private Sub Command1_Click()
        SetControlValue txtArray, "Hello"
        SetControlValue Text1, "Goodbye"
    End Sub

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