0

So I'm trying to make a list of more than one type of object, which I done a research on and found out it's, but I also researched into structures and found I can possibly make a use of that instead, but it doesn't return itself as a list.

Imports WinText = System.Windows.Forms.TextBox
Imports WinCombo = System.Windows.Forms.ComboBox

Public Class PSS      

    Public EntryItems = New List(Of Elements)

    Structure Elements
         Dim xSchool As WinCombo
         Dim xClass As WinCombo
         Dim xID As WinCombo
         Dim xName As WinText        
    End Structure
End Class    

Edit: So on a form I have a set of ComboBoxes and TextBoxes (about 20 more than on the example here). I want to put them into a list so that when I will be retrieving their results, or getting their names for SQL references, or anything similar, I will be able put them into a loop, thus making the code more efficient and shorter to do.

  • 1
    `I'm trying to make a list of more than one type of object` your List contains a struct with multiple objects. `but it doesn't return itself as a list` with no return statement, how do you expect to return that list? Do you want to return it in a property, or a function? I guess the problem is you need to populate your list. Is that right? – djv Sep 27 '17 at 14:35
  • **[Five Minute Intro To Classes and Lists](http://stackoverflow.com/a/34164458/1070452)** – Ňɏssa Pøngjǣrdenlarp Sep 27 '17 at 14:43
  • In short, I'd like to basically create a list of ComboBoxes and TextBoxes. My actual code is much more longer (and complicated) but I referred to return as in when I was testing it, the list was empty. – Krystian Mikołajczyk Sep 27 '17 at 15:02
  • 1
    You dont need to `Imports` control types in a WinForms app. Creating lists of controls is a bit odd compared to a List of data. We have no idea *what* you are doing, just a glimmer of *how* you are trying to do something – Ňɏssa Pøngjǣrdenlarp Sep 27 '17 at 15:05
  • @Plutonix ok, so on a form I have a set of ComboBoxes and TextBoxes (about 20 more than on the example here). I want to put them into a list so that when I will be retrieving their results, or getting their names for SQL references, or anything similar, I will be able put them into a loop, thus making the code more efficient and shorter to do. – Krystian Mikołajczyk Sep 27 '17 at 15:09
  • You might consider a DataGridView since what you are describing is essentially a tedious way to provide that functionality. – Ňɏssa Pøngjǣrdenlarp Sep 27 '17 at 15:22

2 Answers2

0

Structure has similarities to Class and is not a list. (Read more about here)

You can create a List(Of Structure) but you need to populate it first, like djv stated already in comments.

Private Sub Populate()
    Dim list As New List(Of Elements)

    list.Add(New Elements)
    'Add as much Elements as you want
End Sub

Assuming your Controls are placed on a Form named Form1.

For Each ctrl As Control In Form1.Controls
    If TypeOf ctrl Is TextBox Then
        Dim tb As TextBox = DirectCast(ctrl, TextBox)
        'Do whatever you want with the current TextBox
    ElseIf TypeOf ctrl Is ComboBox Then
        Dim chk As ComboBox = DirectCast(ctrl, ComboBox)
        'Do whatever you want with the current ComboBox
    End If
Next
MatSnow
  • 7,357
  • 3
  • 19
  • 31
  • In short, I'd like to basically create a list of ComboBoxes and TextBoxes. My actual code is much more longer (and complicated) but my target is just to make a list from different types and innitialy I thought to make a structure that would consist ComboBoxes and TextBoxes and then put those in a list but clearly that's not the way. – Krystian Mikołajczyk Sep 27 '17 at 15:04
  • Is it possible if I used `Dim ControlList As New List(Of Control)`? Edit v1.2 and then populate it by using `ControlList.Add(exampleTextOrComboBox)`? – Krystian Mikołajczyk Sep 27 '17 at 15:24
0

At first, credits to MatSnow.

Taking his concept of As Control I was able to make a List(Of Control) which allowed me to make and add items to it.

Dim ControlList As New List(Of Control)
ControlList.Add(ComboBox1)
ControlList.Add(TextBox1)
'And so on