从数据库更改按钮的文本(字符串列表)

时间:2017-08-29 11:33:08

标签: vb.net winforms

我是vb.net的新手,所以我正在寻求帮助, 我有方法在面板布局中创建按钮,我想要的是从数据库表(但客户TEXT)为每个按钮设置TEXT, 那怎么办呢?

这是生成按钮的方法:

Private Sub GenerateTable(columnCount As Integer, rowCount As Integer)
    'Clear out the existing controls, we are generating a new table layout
    TableLayoutPanel1.Controls.Clear()

    'Clear out the existing row and column styles
    TableLayoutPanel1.ColumnStyles.Clear()
    TableLayoutPanel1.RowStyles.Clear()

    'Now we will generate the table, setting up the row and column counts first
    TableLayoutPanel1.ColumnCount = columnCount
    TableLayoutPanel1.RowCount = rowCount

    For x As Integer = 0 To columnCount - 1
        'First add a column
        TableLayoutPanel1.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))

        For y As Integer = 0 To rowCount - 1
            'Next, add a row.  Only do this when once, when creating the first column
            If x = 0 Then
                TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.AutoSize))
            End If

            'Create the control, in this case we will add a button
            Dim cmd As New Button()
            cmd.Width = 200
            cmd.Height = 40
            Dim count As Integer = TableLayoutPanel1.Controls.OfType(Of Button)().ToList().Count
            fill_combobox
            cmd.Name = "button_" & (count + 1)
            cmd.Text = "button_" & (count + 1)
            AddHandler cmd.Click, AddressOf Button_Click
            Panel1.Controls.Add(cmd)
            'cmd.Text = String.Format("({0}, {1})", x, y)
            'Finally, add the control to the correct location in the table
            TableLayoutPanel1.Controls.Add(cmd, x, y)
        Next
    Next
End Sub

这是如何将所有客户TEXT或id作为数据库中的字符串列表调用:

Dim myList As New List(Of String)()
Private Sub fill_combobox()
    Try
        If SQL.conn.State = ConnectionState.Open Then
            SQL.conn.Close()
        End If
        SQL.conn.Open()
        Dim cmd As New SqlCommand()
        cmd.Connection = SQL.conn
        cmd.CommandText = "SELECT cust_id FROM customers order by date"
        Dim dr As SqlDataReader = cmd.ExecuteReader

        While dr.Read
            Dim esection As String = dr.Item(0)
            myList.Add(String.Format(esection))
        End While
        dr.Close()
        SQL.conn.Close()

    Catch ex As SqlException
        MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

我详细阐述了Subazs'评论。创建一个集合或List(按钮)可能更容易,然后在将它们添加到流程表中时将所有按钮添加到其中。然后,您可以在以后回调这些内容,它们将存储在列表中,直到您处理该表单。注意如果您像这样编码,它们与表单一致。

我也稍微改变了你的代码,你必须记得处理cmd以及关闭连接(我有一个糟糕的经历,不这样做,这让我付出了代价!)

Dim myList As New List(Of String)
Dim myButtons As List(Of Button)

Public Sub Run_Me()
    Retrieve_Customers()
    'You can change the 5 and it will change the layout panel for you... :)
    GenerateTable_and_Buttons(5)
    MsgBox("All done!")
End Sub

'Replaces your (Fill_Combo_Box) method
Private Sub Retrieve_Customers()
    Try
        myList = New List(Of String)
        If Sql.conn.State = ConnectionState.Open Then Sql.conn.Close()
        Sql.conn.Open()

        'Do you want to add in the customer name instead of ID? - if so we can change here...
        Dim qry As String = "SELECT cust_id FROM customers order by date"
        Dim cmd As New SqlCommand(qry, Sql.conn)
        Dim dr As SqlDataReader = cmd.ExecuteReader
        While dr.Read
            Dim s As String = dr.Item(0)
            myList.Add(String.Format(s))
        End While
        dr.Close()

        cmd.Dispose()
        Sql.conn.Close()

    Catch ex As SqlException
        MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
    End Try

    'Now we have built a list of the customer - myList we can go on to count these...

End Sub

Private Sub GenerateTable_and_Buttons(columnCount As Integer)
    If myList Is Nothing Then Exit Sub 'Just exit if we have no customers?
    myButtons = New List(Of Button)

    'This will round UP to make sure we have enough rows - also can do something like +1 on end if want extra?
    Dim rowCount As Integer = CInt(Math.Round(Math.Ceiling(myList.Count / columnCount), 0))
    Dim tlp As TableLayoutPanel = Me.TableLayoutPanel1

    'Clear out the existing controls, we are generating a new table layout
    tlp.Controls.Clear()

    'Clear out the existing row and column styles
    tlp.ColumnStyles.Clear()
    tlp.RowStyles.Clear()

    'Now we will generate the table, setting up the row and column counts first
    tlp.ColumnCount = columnCount
    tlp.RowCount = rowCount

    Dim count As Integer = 0

    For x As Integer = 0 To columnCount - 1
        'First add a column
        tlp.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))

        For y As Integer = 0 To rowCount - 1
            'Next, add a row.  Only do this when once, when creating the first column
            If x = 0 Then
                tlp.RowStyles.Add(New RowStyle(SizeType.AutoSize))
            End If

            'Create the control, in this case we will add a button
            Dim cmd As New Button
            cmd.Width = 200
            cmd.Height = 40

            '<<Add the customer ID into the TAG of the control, the tag is great place to store things
            'We can then access the tag in another method, ie if we want to open it using your Button_Click
            'method. We can say :
            'Dim CustID As String = sender.Tag
            'Then we can do other things with the ID....>>

            If mylist.count > count then
            cmd.Tag = myList(count)
            cmd.Text = myList(count)
            end if

            'fill_combobox() ' I dont know why this is here?

            cmd.Name = "button_" & (count + 1)
            cmd.Text = "button_" & (count + 1)
            AddHandler cmd.Click, AddressOf Button_Click
            myButtons.Add(cmd)

            '<<<<<<<<<<<<<<<<<
            Panel1.Controls.Add(cmd) '<<<<<<<<< Whats Panel1????
            '>>>>>>>>>>>>>>>>>>

            'Finally, add the control to the correct location in the table
            tlp.Controls.Add(cmd, x, y)

            count += 1
        Next
    Next
End Sub