在VB.NET中将无名文本框保存到数据库中

时间:2014-05-04 15:46:12

标签: database vb.net textbox

我是一名正在学习编程课程的学生。我创建了一段代码,用于创建文本框,供用户输入乙烯基音轨的名称。由于您可以选择创建的文本框的数量,这些文本框最终没有名称(例如textbox1,textbox2 ...),这使我不确定如何将文本框的值发送到数据库表。该表当前为空,只有一列。有人可以告诉我如何将这些值发送到数据库,以及为轨道创建新列的代码。以下是我目前的代码:

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Label11.Show()
    GroupBox1.Show()

    GroupBox1.AutoSize = True

    Dim number As Integer
    number = TextBox8.Text
    Dim boxes(number) As TextBox
    Dim newbox As TextBox

    For i As Integer = 1 To number
        newbox = New TextBox
        newbox.Size = New Drawing.Size(100, 20)
        newbox.Location = New Point(10, 10 + 25 * (i - 1))
        AddHandler newbox.TextChanged, AddressOf TextBox_TextChanged
        boxes(i) = newbox
        GroupBox1.Controls.Add(newbox)
        newbox.Name = ("Trackbox" & i)

    Next

End Sub

1 个答案:

答案 0 :(得分:0)

您的box数组需要在表单级别声明。

Private boxes() As TextBox

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Label11.Show()
    GroupBox1.Show()

    GroupBox1.AutoSize = True

    Dim number As Integer
    'need to insert validation code here to make sure a number is entered in TextBox8
    number = Integer.Parse(TextBox8.Text)
    ReDim boxes(number) 
    Dim newbox As TextBox

    For i As Integer = 1 To number
        newbox = New TextBox
        newbox.Size = New Drawing.Size(100, 20)
        newbox.Location = New Point(10, 10 + 25 * (i - 1))
        AddHandler newbox.TextChanged, AddressOf TextBox_TextChanged
        GroupBox1.Controls.Add(newbox)
        newbox.Name = ("Trackbox" & i.ToString)
        boxes(i) = newbox
    Next

End Sub

然后您可以按如下方式引用文本框:

MessageBox.Show(boxes(5).Text)