首页 > 解决方案 > 如何从动态创建的文本框中将数据保存到数据库中

问题描述

我在 flowlayout 面板上动态创建了文本框。文本框可以是任意数字。我正在尝试将这些值(整数)从文本框中保存到数据库中。我花了一天的时间来实现这一点,顺便说一句,我是新手。请指导我如何实现这一目标。非常感谢。我尝试保存到 List(Of...) 集合中,但它只返回最后一个值。这就是我试图实现这一目标的方法。我在另一个名为 clsHelper 的类中声明了 List 类型的共享列表。

Private Sub saveIntoList(flp As FlowLayoutPanel)
    clsHelper.list = New List(Of String)
    For Each tb in flp.Controls
        If TypeOf tb Is TextBox Then
            txtNo = DirectCast(tb,TextBox)
            If txtNo.Name = "txtNo" Then
                clsHelper.list.Add(txtNo.Text)
            End If
        End If
    Next
End Sub

带有六个文本框的 FlowLayoutPanel

标签: vb.net

解决方案


问题可能是这种If说法。只有TextBox名称"txtNo"被添加到您的list. TextBoxes不添加其他名称不同的内容。删除 ,If所有内容都TextBoxes将添加到您的list.

If txtNo.Name = "txtNo" Then
    clsHelper.list.Add(txtNo.Text)
End If

如果TextBoxes不是. FlowLayoutPanel_FlowLayoutPanel

Private Sub saveIntoList(flp As FlowLayoutPanel)
    clsHelper.list = New List(Of String)
    _saveIntoList(flp)
End Sub

Private Sub _saveIntoList(control As Control)
    If TypeOf control Is TextBox Then
        clsHelper.list.Add(DirectCast(control, TextBox).Text)
        Return
    End If
    For Each child As Control In control.Controls
        _saveIntoList(child)
    Next
End Sub


推荐阅读