【问题标题】:Add text to automatic labels and textboxes vb.net将文本添加到自动标签和文本框 vb.net
【发布时间】:2016-05-17 10:16:17
【问题描述】:

以下代码将标签和文本框创建/格式化为组框。

我的问题是我想定期将文本框文本更改为10(如插入),但我不知道该怎么做。

Private Sub ResizeData()
    ' Create as many textboxes as fit into window
    grpData.Controls.Clear()
    Dim x As Integer = 0
    Dim y As Integer = 10
    Dim z As Integer = 20
    While y < grpData.Size.Width - 100
        labData = New Label()
        grpData.Controls.Add(labData)
        labData.Size = New System.Drawing.Size(30, 20)
        labData.Location = New System.Drawing.Point(y, z)


        labData.Text = Convert.ToString(x + 1)

        txtData = New TextBox()
        grpData.Controls.Add(txtData)
        txtData.Size = New System.Drawing.Size(50, 20)
        txtData.Location = New System.Drawing.Point(y + 30, z)
        txtData.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        txtData.Tag = x


        x += 1
        z = z + txtData.Size.Height + 5
        If z > grpData.Size.Height - 40 Then
            y = y + 100
            z = 20
        End If
    End While

End Sub

我需要这样的东西:

txtData1.text="1"
txtData2.text="0"
txtData3.text="1"
txtData4.text="0"

...等等。

谢谢!

【问题讨论】:

    标签: vb.net winforms textbox


    【解决方案1】:
    Private Sub ResizeData()
        ' Create as many textboxes as fit into window
        grpData.Controls.Clear()
        Dim a As Integer = 1
        Dim x As Integer = 1
        Dim y As Integer = 10
        Dim z As Integer = 20
        While y < grpData.Size.Width - 100
    
            Dim labData As New Label()
            grpData.Controls.Add(labData)
            labData.Size = New System.Drawing.Size(30, 20)
            labData.Location = New System.Drawing.Point(y, z)
    
    
            labData.Text = Convert.ToString(a)
    
    
            Dim txtData As New TextBox()
            grpData.Controls.Add(txtData)
            txtData.Size = New System.Drawing.Size(50, 20)
            txtData.Location = New System.Drawing.Point(y + 30, z)
            txtData.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
            txtData.Tag = x
            txtData.Text = x
    
            a += 1
            If x = 1 Then
                x = 0
            ElseIf x = 0 Then
                x = 1
            End If
    
            z = z + txtData.Size.Height + 5
            If z > grpData.Size.Height - 40 Then
                y = y + 100
                z = 20
            End If
        End While
    
    End Sub
    

    要只给前 2 个文本框赋值“1”,请使用上面相同的代码,修改这 3 行:

     Dim txtData As New TextBox() With {.Name = "txt" & a}
     txtData.Text = 0
     If txtData.Name = "txt1" Or txtData.Name = "txt2" Then txtData.Text = 1 'add this line just below the above one
    

    【讨论】:

    • 我已经测试了你的代码,它可以工作.. 使用标签!我想要文本框中的文本 (txtData.text)。谢谢!
    • 上面的代码已经更新:标签递增,文本框在 1 和 0 之间切换
    • 相当不错!谢谢!
    • 顺便说一句,只是为了好奇,如果我只想前两个文本框有文本“1”怎么办?
    【解决方案2】:

    也许使用布尔值会起作用。不是更清洁的方式,但它应该可以工作

    让一切变暗的地方:

    Dim even as boolean
    

    并且在txtData的所有属性更改之后:

    if even then
        txtData.Text=1
    else
        txtData.Text=0
    end if
    even= not even
    

    【讨论】:

    • 不。没用。它只会改变最后一个文本框的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    相关资源
    最近更新 更多