如何仅使用代码制作标签?

时间:2013-10-11 09:57:05

标签: vb.net

我想通过单击按钮为表单添加标签。当我在这里使用代码时它只添加1个标签,但我想添加无限量。每次单击按钮,即使我更改了名称,它也只会添加1个标签。

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

    Dim lbl As New label
    lbl.Size = New System.Drawing.Size(159, 23) 'set your size
    lbl.Location = New System.Drawing.Point(12, 180) 'set your location
    lbl.Text = (TextBox1.Text) 'set your name
    Me.Controls.Add(lbl)  'add your new control to your forms control collection

End Sub

2 个答案:

答案 0 :(得分:3)

这样的事情:

Dim lbl As New label
lbl.Size = New System.Drawing.Size(159, 23) 'set your size (if required)
lbl.Location = New System.Drawing.Point(12, 180) 'set your location
lbl.Text = "label text goes here" 'set the text for your label
Me.Controls.Add(lbl)  'add your new control to your forms control collection

我会让你以编程方式设置位置。您可以使用类似全局变量的内容来计算已创建的标签数量......

答案 1 :(得分:1)

为列表框中的每个项目添加一个新的链接标签,我将 24 添加到 updown 整数 每次它创建一个新的链接标签时,我的链接标签都会在不同的位置结束。

将右左变暗为整数 =1018
将上下调暗为整数 = 56

将 LLCount 变暗为整数

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Do
     Dim lklabel As New LinkLabel
     lklabel.Size = New System.Drawing.Size(65, 13)
     lklabel.Location = New System.Drawing.Point(rightleft, updown)
     lklabel.Text = ListBox1.Items(LLCount)
     lklabel.Name = "lklabel" & LLCount
     Me.Controls.Add(lklabel)
     updown += 24
     LLCount += 1
     Loop Until lnklbl = ListBox1.Items.Count
  end sub