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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19315315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:27:35  来源:igfitidea点击:

How to make a Label using only code?

vb.net

提问by user2581791

I want to add a label to a form with the click of a button. When I use the code here it only adds 1 label, but I want to add an unlimited amount. Every time I click the button it only adds 1 label even if I change the name.

我想通过单击按钮向表单添加标签。当我在这里使用代码时,它只添加了 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

回答by 5uperdan

Something like this:

像这样的东西:

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

I'll leave you to programatically set the location. You could use something like a global variable to keep count of how many labels have been created...

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