none
Help for a learner. RRS feed

  • Question

  • Dim dtSet As New DataSet()
            myCmd.Fill(dtSet, "Table1")
            Dim dt As DataTable = dtSet.Tables(0)
            Dim dc As DataColumn
            For Each dc In dt.Columns
                ListBox1.Items.Add(dc.ColumnName)
            Next dc
    
    
    
    


    I have this code that puts a list of all access DB fields, works fine. I would like to replace the number and names of fields here.

    Dim controlsNames As String() = {"FirstName", "SurName", "Email", "Phone", "Age"}
            Dim txtbox As TextBox
            For i As Integer = 1 To 5
                txtbox = New TextBox

    Can some one please help me.

    Thanks.

    Wednesday, August 1, 2018 6:11 AM

Answers

  • 
    
    Dim controlsNames As String() = {"FirstName", "SurName", "Email", "Phone", "Age"}
            Dim txtbox As TextBox
            For i As Integer = 1 To 5
                txtbox = New TextBox

    This code seems to be incomplete, what is it used for? I don't quite understand your purpose, please tell me in detail.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mikkidrip Wednesday, August 1, 2018 8:42 AM
    Wednesday, August 1, 2018 7:09 AM

All replies

  • You can use a similar ‘For Each dc In dt.Columns’ loop. But, if you want the controlNames array, try one of the solutions:

    Dim controlsNames As String() = dt.Columns.Cast(Of DataColumn).Select(Function(c) c.ColumnName).ToArray
    For i As Integer = 1 To controlsNames.Count
       . . .
    Next
     
    


    Wednesday, August 1, 2018 7:03 AM
  • 
    
    Dim controlsNames As String() = {"FirstName", "SurName", "Email", "Phone", "Age"}
            Dim txtbox As TextBox
            For i As Integer = 1 To 5
                txtbox = New TextBox

    This code seems to be incomplete, what is it used for? I don't quite understand your purpose, please tell me in detail.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mikkidrip Wednesday, August 1, 2018 8:42 AM
    Wednesday, August 1, 2018 7:09 AM
  • These are parts of code I had gathered from the net.

    The second bit of code helps create a number of textboxes (1 to 5), Then names them as created with the list in controlNames. I want the name for each to come from first code(Listbox list) which is collected from a Access Database.


     

    Wednesday, August 1, 2018 7:20 AM
  • Hi

    Sorry, I still don't understand what you mean.Do you want to replace the name of the Text Box with the table name?I hope the code can help you.

     Dim i = 1
                For Each dc In dt.Columns
                    For Each ct As Control In Me.Controls
                        If ct.Name = "TextBox" & i.ToString Then
                            ct.Name = dc.ColumnName.ToString
                        End If
                    Next
                    i += 1
                Next dc

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 1, 2018 8:26 AM
  • Thanks Alex that shown me the way, all good now.

    Thanks a lot.

    Wednesday, August 1, 2018 8:43 AM