0

我正在使用 vb.net windows 窗体应用程序中的 windows 窗体应用程序,其中有两个按钮:

  1. 添加新的文本框
  2. 将多个文本框数据插入数据库

当我点击第一个按钮时,它将创建新的文本框,如果我点击第二个按钮,它应该在数据库中插入多个文本框的数据。

数据库只有两个字段

  1. id 主键
  2. 姓名

文本框包含名称。

请建议我实现这一点的方法,我对此一无所知。

4

1 回答 1

3

In your click-event assuming the ID-field is auto-incrementing in the database:

dim myTxt as New TextBox
myTxt.Name = Me.Controls.Count.ToString '"random" name
myTxt.Location = New Point(10, 10) 'set the position according to your layout
myTxt.Tag = "For DB" 'something to identify it by
myTxt.Visible = True

Me.Controls.Add(myTxt) 'add it to form's control collection.
Me.Refresh             'If in panel etc. change Me with that control.

Then in your second click event:

... open database, do checks etc.

For each c As Control in Me.Controls
    If typeof c Is TextBox AndAlso c.Tag ="For DB" Then
        Dim txt As String = CType(c, TextBox).Text
        'insert txt into database
    End If
Next

(A data-grid can do this for you as well).

Also you can use an array to perform the creation, and you can interact with each textbox you create by its index.

For y As UShort = 0 To indexsize_b - 1
    For x As UShort = 0 To indexsize_a - 1
    array(x, y) = New TextBox
    array(x, y).Size = New Size(60, 20)
    array(x, y).Location = New Point(5 + 60 * x, 40 + 20 * y)
    array(x, y).Visible = True
    Me.Controls.Add(array(x, y))
    Next
Next

Interact via something like:

string s = array(a, b).text
于 2012-11-22T15:36:51.150 回答