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

Visual Basic

 
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 
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 
I have to agree...that is some bizarre code.

So, you have 13 labels that all start with lblRods. So then, you're running the function to return an array of controls that all start with lblRods. All of that is great.

Your problem is with the getControlArray code. Think about what it should do and then look at exactly what it does.

It should loop through all of the controls on the form and see if the control starts with the value you passed in, in this case "lblRods". If it does start with that, then you want to add it to the array.

Here's what your code does. You search through the controls and are looking for the highest index of the controls. So, your code matches each control that starts with lblRods and instead of adding that control to an array, you instead, see if it's index is the highest index so far.

Then, if the index is greater than -1, you go through and call GetControlFromName which will return a control with the exact name that you passed it. In this case, you passed it lblRods. It will never return a control with that exact name because it doesn't exist.

So, what happens is that aList will have nothing in it and controlType will equal Nothing.

Why didn't you just add the controls to the list when you found them in the first for each loop? That's all you needed to do. And you need to make sure that the array isn't empty before returning something. How about a re-write:

VB
Public Shared Function getControlArray(ByVal frm As System.Windows.Forms.Form, _
                                       ByVal controlName As String, _
                                       ByVal TypeToReturn As Type, _
                                       Optional ByVal separator As String = "") As Control()
    Dim aList As New ArrayList

    For Each ctrl As Control In frm.Controls
        If ctrl.Name.ToLower.StartsWith(controlName.ToLower & separator) And _
           ctrl.GetType = TypeToReturn Then
            aList.Add(ctrl)
        End If
    Next

    If aList.Count = 0 Then
        Return Nothing
    Else
        Return aList.ToArray(TypeToReturn)
    End If

End Function


it's that easy...you were making it too complicated.

If you're only wanting to return a single type, then you really need to make sure to check that that type is the right type. Obviously, you can know that the only thing with the name "lblRods" is going to be a label because you wrote it that way, but then why not just hard code the values, unless you're trying to make something that can be used in other places.

When you call it then, just do:
VB
Dim labels() as Label = GetControlArray(Me, "lblRods", GetType(Label))


modified on Thursday, May 27, 2010 2:34 PM

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 
AnswerRe: Procedure and code for adding a plug-in for Outlook. Pin
Tom Deketelaere27-May-10 2:37
professionalTom Deketelaere27-May-10 2:37 
AnswerRe: Procedure and code for adding a plug-in for Outlook. Pin
Eddy Vluggen28-May-10 1:56
professionalEddy Vluggen28-May-10 1:56 
GeneralRe: Procedure and code for adding a plug-in for Outlook. Pin
Luc Pattyn28-May-10 2:38
sitebuilderLuc Pattyn28-May-10 2:38 
GeneralRe: Procedure and code for adding a plug-in for Outlook. Pin
Eddy Vluggen28-May-10 5:08
professionalEddy Vluggen28-May-10 5:08 

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.