Results 1 to 5 of 5

Thread: HELP! reference control by string

Hybrid View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2002
    Location
    Florida
    Posts
    24

    HELP! reference control by string

    is there a way to reference a control (button or checkbox) by using a string for its name instead of an integer?

    I want to loop through a recordset with names of controls and add or remove them based on values in the recordset.

    I am converting from vba where this worked:
    CHKNAME = "chk" & rc![Report]
    Set ctl = FRM.Controls(CHKNAME)

    thanks

  2. #2
    Junior Member
    Join Date
    Sep 2002
    Posts
    23
    Try
    Code:
    Dim aryCtl() As Control
    aryCtl = GetFormControls(me)
    For I = 0 to Ubound(aryCtl)
        if aryCtl(I).Name .....
    Next
    
    
    Public Function GetFormControls(frm as Form) As Control()
            Dim aryCtl() As Control
            Dim intIdx As Integer = -1
            ReDim aryCtl(200)
            GetFormControls2(frm.Controls, aryCtl, intIdx)
            ReDim Preserve aryCtl(intIdx) ' Resize the array
            Return aryCtl
        End Function
        Private Sub GetFormControls2(ByVal ctlControls As Control.ControlCollection, _
                                    ByVal aryCtl As Control(), ByRef intIdx As Integer)
            Dim ctlW As Control
            Dim intBound As Integer
            intBound = aryCtl.GetUpperBound(0)
            For Each ctlW In ctlControls
                intIdx = intIdx + 1
                If intIdx > intBound Then
                    ReDim Preserve aryCtl(intIdx * 2)
                    intBound = aryCtl.GetUpperBound(0)
                End If
                aryCtl(intIdx) = ctlW
                If Not (ctlW.Controls Is Nothing) AndAlso ctlW.Controls.Count > 0 Then
                    GetFormControls2(ctlW.Controls, aryCtl, intIdx)
                End If
            Next
        End Sub

  3. #3
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    correct me if im wrong but u could use the callbyname() function in vb6 namespace

  4. #4
    Junior Member
    Join Date
    Sep 2002
    Posts
    23
    You are mistaken.
    CallByname "Executes a method on an object, or sets or returns a property on an object". The name of the method or property is contained in a string, not the name of the control.

    Code:
    Public Function CallByName( _
       ByVal Object As System.Object, _
       ByVal ProcName As String, _
       ByVal UseCallType As CallType, _
       ByVal ParamArrayArgs() As Object _
    ) As Object
    Object
    Required. Object. A pointer to the object exposing the property or method.
    ProcName
    Required. String. A string expression containing the name of the property or method on the object.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2002
    Location
    Florida
    Posts
    24

    CALLBYNAME WORKS, THANKS

    IT WORKS:

    Public WithEvents cmd_inorg_clp_11and12 As New Button()
    Dim ctl As Object

    ctl = CallByName(Me, "cmd_inorg_clp_11and12", CallType.Get)

    Dim chkx As Integer
    Dim cmdx As Integer
    Dim chkandcmdy As Integer

    chkx = 30
    cmdx = 50
    chkandcmdy = 50

    With ctl
    .Location = New Point(50, 50)
    .Text = "INORG CLP 11 AND 12"
    .Width = 180
    .Height = 20
    End With
    Me.TabControl1.TabPages.Item(1).Controls.Add(ctl)

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