Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 从动态创建的文本框中读取数据_Vb.net_Winforms - Fatal编程技术网

Vb.net 从动态创建的文本框中读取数据

Vb.net 从动态创建的文本框中读取数据,vb.net,winforms,Vb.net,Winforms,我尝试在用vb.net创建动态文本框后收集数据 Private Sub btn_OK_lines_number_Click(sender As Object, e As EventArgs) Handles btn_OK_lines_number.Click Dim i As Integer Dim x As Integer Dim Z As Integer Z = 150 If IsNumeric(txt_lines_number.Text)

我尝试在用vb.net创建动态文本框后收集数据

Private Sub btn_OK_lines_number_Click(sender As Object, e As EventArgs) 
    Handles btn_OK_lines_number.Click
    Dim i As Integer
    Dim x As Integer
    Dim Z As Integer
    Z = 150
    If IsNumeric(txt_lines_number.Text) Then
        Int32.TryParse(txt_lines_number.Text, x)
        For i = 1 To x
            Dim newTB As New TextBox
            Dim newLB As New Label
            newLB.Name = "lbl_workstation_number_line" & i
            newLB.Text = "Nbr Work Station in Line" & i
            newLB.Size = New Size(190, 20)
            newLB.ForeColor = Color.White
            newLB.Font = New Font("consolas", 12, FontStyle.Regular, GraphicsUnit.Pixel)
            newLB.Location = New Point(20, Z + i * 30)
            newTB.Name = "Textbox" & i
            newTB.Size = New Size(170, 20)
            newTB.Location = New Point(200, Z + i * 30)
            Me.Controls.Add(newTB)
            Me.Controls.Add(newLB)
        Next
        i = i + 1
    Else
        MessageBox.Show("please enter a number")
        txt_lines_number.Text = ""
    End If
End Sub

假设只有一行,只创建一个文本框。您可以在此处设置名称:

newTB.Name = "Textbox" & i
其中,生成的文本框命名为
Textbox1
。问题是,您不能像对待
txt\u行数
那样,直接在代码中引用标识符
Textbox1
。您甚至不能将其作为类的成员引用(
Me.Textbox1
)。此名称在编译时不存在,因此它不是您可以使用的标识符,并且它根本不是类的成员。该名称从来没有匹配的
Dim
语句

不过,您可以再次查看
控件
集合,在该集合中,您将文本框添加到表单中:

Me.Controls("Textbox1")

您可能还需要将值强制转换为文本框:

Dim box As TextBox = DirectCast(Me.Controls("Textbox1"), TextBox)
MessageBox.Show(box.Text)    
记住这个案子在这里很重要

进一步将其保存在DB中超出了一个问题的范围。有很多方法可以做到这一点,就像世界上有程序员一样。您应该先尝试一下,遇到具体问题时,请带着新问题回到这里。

谢谢, 这是我的尝试,成功了

Dim userInput As TextBox = Form1.Controls.Item("TextBox" & i.ToString)
mycommand.Parameters.AddWithValue("@workstation", userInput.Text)

:D

因为您创建了动态数量的输入控件,作业的正确工具将是
DataGridView
控件

创建一个类来表示您的数据

Public Class LineInfo
    Public Property Number As Integer
    Public Property WorkStationNumber As Integer
End Class
在表单设计器中创建“DataGridView”

Private Sub btn_OK_lines_number_Click(sender As Object, e As EventArgs) Handles btn_OK_lines_number.Click
    Dim linesAmount As Integer
    If Integer.TryParse(txt_lines_number.Text, linesAmount) = False Then
        MessageBox.Show("please enter a number")
        txt_lines_number.Text = ""
        Exit Sub
    End If

    ' Create class instance for every line
    Dim lines = 
        Enumerable.Range(1, linesAmount)
                  .Select(Function(i) New LineInfo With { .Number = i })
                  .ToList()

    'Set lines as DataSource to the DataGridView
    Me.DataGridView1.DataSource = lines
End Sub
DataGridView将显示所有行,并提供输入字段以更新工作站编号

您可以稍后通过将
数据源
转换回
列表

Dim lines = DirectCast(Me.DataGridView1.DataSource, List(Of LineInfo))

' Now you can access all data and save it to the database
Dim parameters = 
    lines.Select(Function(line)
                     Return new SqlParameter With 
                     {
                         .ParameterName = $"@workstation{line.Number}",
                         .SqlDbType = SqlDbType.Int,
                         .Value = line.WorkStationNumber
                     }
                 End Function)
         .ToList()

myCommand.Parameters.AddRange(parameters)

您可以在datagridview中自由更改不同列的样式、字体颜色。

Hi-Billel。你已经解释了你想做什么,但没有说明出了什么问题。你遇到了什么问题?因为你从来没有问过问题,很难说清楚,但我想这可能是的重复。嗨,科里,我不知道如何从文本框调用每个值将其保存在DB上:我正在使用这个函数保存其他正常情况下的数据:mycommand.Parameters.AddWithValue(@workstation),TEXTBOX.TEXT)
Dim lines = DirectCast(Me.DataGridView1.DataSource, List(Of LineInfo))

' Now you can access all data and save it to the database
Dim parameters = 
    lines.Select(Function(line)
                     Return new SqlParameter With 
                     {
                         .ParameterName = $"@workstation{line.Number}",
                         .SqlDbType = SqlDbType.Int,
                         .Value = line.WorkStationNumber
                     }
                 End Function)
         .ToList()

myCommand.Parameters.AddRange(parameters)