Click here to Skip to main content
16,014,392 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: i need a help Pin
Smithers-Jones29-May-10 11:57
Smithers-Jones29-May-10 11:57 
AnswerRe: i need a help Pin
Kschuler2-Jun-10 10:52
Kschuler2-Jun-10 10:52 
QuestionTracking NewsTicker Pin
police_0104028-May-10 21:09
police_0104028-May-10 21:09 
AnswerRe: Tracking NewsTicker Pin
LloydA11129-May-10 12:31
LloydA11129-May-10 12:31 
QuestionThanks for prior help- next problem- array/collection or class to store/save data Pin
lemarshall27-May-10 14:26
lemarshall27-May-10 14:26 
AnswerRe: Thanks for prior help- next problem- array/collection or class to store/save data Pin
Andy_L_J28-May-10 8:41
Andy_L_J28-May-10 8:41 
GeneralRe: Thanks for prior help- next problem- array/collection or class to store/save data Pin
lemarshall2-Jun-10 16:36
lemarshall2-Jun-10 16:36 
QuestionCreating label array in vb.net 2007 Pin
lemarshall27-May-10 5:24
lemarshall27-May-10 5:24 
I have a table of 13 items that I need to display in sequence. The sequence can change from day to day based upon the previous days usage. One day the number 1 item may be 3/4" concrete lined rod, the next day that may be 5th and 5/8" SMPC may be the number one item. I pull the material from the table in the sequence order and then I want t fill in the label with the material name and the label tag property with the MaterialID which is a GUID. I found some code in a google search for creating a control array but I'm just not understanding it enough to get it to work in my project. The link for that is as follows:
http://vbnet.codenewbie.com/articles/vbnet/1555/Mimicking_VB6_Control_Arrays_in_VBNET-Page_1.html

My labels are named lblRods_0 to lblRods_12 and the code for loading them is:
    Private Function LoadTabData() As Boolean
        'We need to get the items for the frmLOL from the table Materials and load 
        'them into the text boxes in sequence. If user fills out anything we need to 
        'store it using the GUID stored in the tag property

        'SELECT Materials.MaterialsID, Materials.Type, Materials.Sequence, Materials.Material
        'FROM(Materials)
        'ORDER BY Materials.Type, Materials.Sequence;
        Try
            gblConn = New OleDbConnection(JWIConnStr)
            Dim daMaterial As New OleDbDataAdapter
            Dim dsMaterial As New DataSet
            Dim mySelectQuery As String = "SELECT MaterialsID, Type, Sequence, Material " & _
                "FROM Materials " & _
                "ORDER BY Type, Sequence;"
            Dim myConnection As New OleDbConnection(JWIConnStr)
            Dim myCommand As New OleDbCommand(mySelectQuery, myConnection)
            myConnection.Open()
            daMaterial.SelectCommand = myCommand
            daMaterial.Fill(dsMaterial)
            If Not dsMaterial.Tables(0).Rows.Count > 0 Then
                MsgBox("Unable to locate MATERIALS. Please Cancel and retry later", MsgBoxStyle.OkOnly)
                Exit Function
            Else 'at least one record shows 
                Dim recCount As Integer
                Dim dstbl As New DataTable
                daMaterial.Fill(dstbl)
                recCount = dsMaterial.Tables(0).Rows.Count
                Dim rowNdx As Integer
                Dim lblNdx As Integer
                rowNdx = 0 'Set to zero index value for the dsMaterial tbl 
                lblNdx = 1
                For Each Row In dstbl.Rows
                    If rowNdx = recCount Then
                        Exit For
                    End If
                    'Dim Labels As Label() = ControlArrayUtils.getControlArray(Me, "lblRods")
                    Dim lblRods As Label() = ControlArrayUtils.getControlArray(Me, "lblRods")
                    'Dim lblRods() As Label
                    'lblRods = ControlArrayUtils.getControlArray(Me, "Label")
                    'lblRods = ControlArrayUtils.getControlArray(Me, "lblRods")

                    lblRods(lblNdx).Tag = dsMaterial.Tables(0).Rows(rowNdx)("MaterialsID").ToString()
                    lblRods(lblNdx).Text = dsMaterial.Tables(0).Rows(rowNdx)("Material").ToString()
                    rowNdx = rowNdx + 1 'keep track of where we are at 
                    lblNdx = lblNdx + 1

                Next

            End If
        Catch ex As Exception
            Debug.Print(MsgBox(ex.ToString))
        End Try

    End Function

Public Class ControlArrayUtils

    'Converts same type of controls on a form to a control 
    'array by using the notation ControlName_1, ControlName_2, 
    'where the _ can be replaced by any separator string

    Public Shared Function getControlArray( _
         ByVal frm As Windows.Forms.Form, _
         ByVal controlName As String, _
         Optional ByVal separator As String = "_") As System.Array

        Dim i As Short
        Dim startOfIndex As Short
        Dim alist As New ArrayList
        Dim controlType As System.Type
        Dim ctl As System.Windows.Forms.Control
        Dim ctrls() As System.Windows.Forms.Control
        Dim strSuffix As String
        Dim maxIndex As Short = -1 'Default


        'Loop through all controls, looking for 
        'controls with the matching name pattern
        'Find the highest indexed control

        For Each ctl In frm.Controls
            startOfIndex = ctl.Name.ToLower.IndexOf( _
                        controlName.ToLower & separator)
            If startOfIndex = 0 Then
                'If startOfIndex = -1 Then
                strSuffix = ctl.Name.Substring(controlName.Length)
                'Check that the suffix is an
                ' integer (index of the array)
                If IsInteger(strSuffix) Then
                    If Val(strSuffix) > maxIndex Then _
                       maxIndex = Val(strSuffix) 'Find the highest 
                    'indexed Element
                End If
            End If
        Next ctl

        'Add to the list of controls in correct order
        If maxIndex > -1 Then

            For i = 0 To maxIndex
                Dim aControl As Control = _
                  getControlFromName(frm, controlName, i, separator)
                If Not (aControl Is Nothing) Then
                    'Save the object Type (uses the last 
                    'control found as the Type)
                    controlType = aControl.GetType
                End If
                alist.Add(aControl)
            Next
        End If
        Return alist.ToArray(controlType)
    End Function

    'Converts any type of like named controls on a form 
    'to a control array by using the notation ControlName_1, 
    'ControlName_2, where the _ can be replaced by any 
    'separator string

    Public Shared Function getMixedControlArray( _
       ByVal frm As Windows.Forms.Form, ByVal controlName As String, _
       Optional ByVal separator As String = "") As Control()

        Dim i As Short
        Dim startOfIndex As Short
        Dim alist As New ArrayList
        Dim controlType As System.Type
        Dim ctl As System.Windows.Forms.Control
        Dim ctrls() As System.Windows.Forms.Control
        Dim strSuffix As String
        Dim maxIndex As Short = -1 'Default

        'Loop through all controls, looking for controls 
        'with the matching name pattern
        'Find the highest indexed control

        For Each ctl In frm.Controls
            startOfIndex = ctl.Name.ToLower.IndexOf( _
                          controlName.ToLower & separator)
            If startOfIndex = 0 Then
                strSuffix = ctl.Name.Substring(controlName.Length)
                'Check that the suffix is an integer 
                '(index of the array)
                If IsInteger(strSuffix) Then
                    If Val(strSuffix) > maxIndex Then _
                       maxIndex = Val(strSuffix) 'Find the highest 
                    'indexed Element
                End If
            End If
        Next ctl

        'Add to the list of controls in correct order
        If maxIndex > -1 Then
            For i = 0 To maxIndex
                Dim aControl As Control = getControlFromName(frm, _
                                         controlName, i, separator)
                alist.Add(aControl)
            Next
        End If
        Return alist.ToArray(GetType(Control))
    End Function

    Private Shared Function getControlFromName( _
           ByRef frm As Windows.Forms.Form, _
           ByVal controlName As String, ByVal index As Short, _
           ByVal separator As String) As System.Windows.Forms.Control
        controlName = controlName & separator & index
        For Each ctl As Control In frm.Controls
            If String.Compare(ctl.Name, controlName, True) = 0 Then
                Return ctl
            End If
        Next ctl
        Return Nothing 'Could not find this control by name
    End Function

    Private Shared Function IsInteger(ByVal Value As String) As Boolean
        If Value = "" Then Return False
        For Each chr As Char In Value
            If Not Char.IsDigit(chr) Then
                Return False
            End If
        Next
        Return True
    End Function

End Class


I get the error message: System.ArgumentNullException:Value cannot be null. Parameter name:type at System.Collections.ArrayList.ToArray(Type type) at JWI.ControlArrayUtils.getControlArray(Form frm String controlName,String separator) in C:\JWI\JWI\JWI\ControlArrayUtils.vb:line 56 at JWI.frmLOL.LoadTabData() in c:\JWI\JWI\JWI\frmLOL.vb line 332

Any assistance in understanding the error message and how to get this to work would be greatly appreciated. I've tried about 2 dozen versions of calling the array utility but no luck yet.

Thanks,
Larry
AnswerRe: Creating label array in vb.net 2007 Pin
Luc Pattyn27-May-10 5:39
sitebuilderLuc Pattyn27-May-10 5:39 
GeneralRe: Creating label array in vb.net 2007 Pin
lemarshall27-May-10 6:37
lemarshall27-May-10 6:37 
GeneralRe: Creating label array in vb.net 2007 Pin
Luc Pattyn27-May-10 7:46
sitebuilderLuc Pattyn27-May-10 7:46 
AnswerRe: Creating label array in vb.net 2007 [modified] Pin
William Winner27-May-10 8:21
William Winner27-May-10 8:21 
GeneralRe: Creating label array in vb.net 2007 Pin
lemarshall27-May-10 9:50
lemarshall27-May-10 9:50 
GeneralRe: Creating label array in vb.net 2007 Pin
William Winner27-May-10 10:42
William Winner27-May-10 10:42 
Questionvisual basic 2008 captcha to picture box help Pin
Snipe7627-May-10 2:43
Snipe7627-May-10 2:43 
AnswerRe: visual basic 2008 captcha to picture box help Pin
Henry Minute27-May-10 4:37
Henry Minute27-May-10 4:37 
Question[Need Correction]Class To Load sqlReader into Listview Pin
fenindom27-May-10 2:06
fenindom27-May-10 2:06 
AnswerRe: [Need Correction]Class To Load sqlReader into Listview Pin
Tom Deketelaere27-May-10 2:34
professionalTom Deketelaere27-May-10 2:34 
GeneralRe: [Need Correction]Class To Load sqlReader into Listview Pin
fenindom27-May-10 3:04
fenindom27-May-10 3:04 
AnswerRe: [Need Correction]Class To Load sqlReader into Listview Pin
William Winner27-May-10 8:30
William Winner27-May-10 8:30 
GeneralRe: [Need Correction]Class To Load sqlReader into Listview Pin
fenindom27-May-10 9:19
fenindom27-May-10 9:19 
GeneralRe: [Need Correction]Class To Load sqlReader into Listview Pin
William Winner27-May-10 9:23
William Winner27-May-10 9:23 
GeneralRe: [Need Correction]Class To Load sqlReader into Listview Pin
fenindom27-May-10 9:27
fenindom27-May-10 9:27 
QuestionProcedure and code for adding a plug-in for Outlook. Pin
S.Srinivasa Rao26-May-10 23:18
S.Srinivasa Rao26-May-10 23:18 
AnswerRe: Procedure and code for adding a plug-in for Outlook. Pin
Dalek Dave26-May-10 23:52
professionalDalek Dave26-May-10 23:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.