Get the name of a textbox and save it in an MS Visual Basic array?


Cordial greeting colleagues, it turns out that I have a form, which creates labels and textbox automatically from the load of the form, according to the number of records that are in a query to the database that I have. using this code:

Imports MySql.Data.MySqlClient
Public Class actividades
    Private Sub actividades_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'variable para almacenar la conexion 
        Dim con As MySqlConnection
        con = conectar() 'se le asigna a la variable con la funcion de conexion 

        Dim sql As String = " SELECT nombre_actividad FROM actividades" 'variable donde se almacenara la consulta SQL
        Dim comando As New MySqlCommand(sql, con) ' ejecuta la consulta almacenada en SQL con la conexion CON
        comando.Connection = con
        Dim resultado As MySqlDataReader = comando.ExecuteReader() 'almacena en la variable resultado los datos obtenidos en la consulta

        'condicional para verificar 
        If resultado.HasRows Then

            'ciclo para recorrer los datos almacenados en el resultado 
            Do While resultado.Read()

                'agregamos los datos obtenidos 
                Dim lbs As New Label 'variable para crear labels a partir de su nombre
                Dim txtbox As New TextBox 'variable para crear textbox a partir de su nombre
                ReDim cajas(contartxtbox)
                'funcion para referirse al Textbox, y asignarle parametros 
                With txtbox
                    .Name = contartxtbox 'aqui se le agrega el nombre unico al textbox
                    .Location = New System.Drawing.Point(400, espacio) 'posicion donde va a quedar el textbox dentro del formulario
                    .Size = New System.Drawing.Size(70, 20) 'tamaño de la caja del textbox
                    .Text = "txt" & contartxtbox
                    .Parent = Me

                End With

                'funcion para referise al los labels y asignarle parametros
                With lbs
                    .Name = contarlb 'aqui se le asigna un nombre unico a cada label creado
                    .Text = resultado.Item("nombre_actividad") 'aqui se le asigna el texto contenido dentro del label apartir de la base de datos
                    .Parent = Me
                    .Location = New Point(50, espacio) 'posicion donde va a quedar el label dentro del formulario 
                    .Size = New Size(200, 50) 'tamaño de la caja del label
                End With


                contarlb += 1
                contartxtbox += 1
                espacio += 50

            Console.WriteLine("datos no encontrados")
        End If
        'btnconsultar.Enabled = False


End Sub

It works perfectly, now what I need is to save in an array, the name of each textbox that is created automatically according to the number of records of the bd try with the following:


But it does not save anything inside the array, what I need in a timely manner is to save the name of each textbox in order to use it for some operations, how could I do it?

asked by Kevin Burbano 19.02.2018 в 18:33

1 answer


In reality, you do not need to store the controls that you have dynamically created in any place. You can access them by using the Controls collection that any container has, and the OfType method that returns only the objects that are of a type. As follows:

Private cajas = Me.Controls.OfType(Of TextBox)()

If you had other TextBoxes apart from those that you generate dynamically, the best thing is that you generate an exclusive prefix to those you generate. For example, if you always start with dyn , to find them you would simply do so:

Private cajas = Me.Controls.OfType(Of TextBox)().Where(Function(x) x.Name.StartsWith("dyn"))
answered by 20.02.2018 / 09:16