Αν θέλεις τα ονόματα των κλάσεων, δηλαδή π.χ. Form1, Form2 κλπ, τότε χρησιμοποιώντας reflection μπορείς να πάρεις τα ονόματα όλων των φορμών που βρίσκονται στο ίδιο assembly με αυτό από το οποίο τρέχει η κύρια φόρμα σου ως εξής:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Get the currently executing assembly
Dim a As Assembly
a = Assembly.GetExecutingAssembly()
'Iterate all types in the assembly
For Each t As Type In a.GetTypes()
'Find out if current type is a Form
If t.IsSubclassOf(GetType(System.Windows.Forms.Form)) Then
'Exclude our own form
If Not t.Name.Equals("Form1") Then
ComboBox1.Items.Add(t.Name)
End If
End If
Next
End Sub
Φυσικά θα κάνεις Imports System.Reflection, ενώ υποθέτω οτι έχεις μια εναρκτήρια φόρμα με όνομα Form1 και ένα combobox με όνομα ComboBox1.
Αν τώρα θέλεις να πάρεις κάποιο property της φόρμας που να έχει ένα πιό φιλικό όνομα, π.χ. το Text property ή ακόμα και κάποιο δικό σου property, θα πρέπει να δημιουργήσεις instances των φορμών σου. Αυτό είναι σαν να φτιάχνεις μια καινούρια φόρμα μόνο και μόνο για να πάρεις πράγματα μέσω του reflection, οπότε είναι πιό αργό και πιό tricky. Παρ'όλα αυτά:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Get the currently executing assembly
Dim a As Assembly
a = Assembly.GetExecutingAssembly()
'Iterate all types in the assembly
For Each t As Type In a.GetTypes()
'Find out if current type is a Form
If t.IsSubclassOf(GetType(System.Windows.Forms.Form)) Then
'Exclude our own form
If Not t.Name.Equals("Form1") Then
Dim s As String
Dim f As Form
'Create an instance of the form
f = CType(a.CreateInstance(t.FullName), Form)
'Get the form's text property, or whatever other property we like
s = f.Text
'Add this to the combobox
ComboBox1.Items.Add(s)
End If
End If
Next
End Sub
Σωτήρης Φιλιππίδης
DotSee Web Services