vb.net动态创建复选框 [英] vb.net dynamically create checkboxes
问题描述
我想弄清楚如何在我不知道确切需要多少个框的情况下如何在表单上创建动态复选框。
I am trying to figure out how to go about creating dynamic checkboxes on my form when I do not know exactly how many boxes I will need.
问题是我不知道如何对多个对象进行DIM。这是我用于创建一个复选框的代码
The problem is that I do not know how to DIM more than one object. This is my code for creating one checkbox
Dim checkBox As New CheckBox()
Form1.Controls.Add(checkBox)
checkBox.Location = New Point(10, 10)
checkBox.Text = "testing"
checkBox.Checked = True
checkBox.Size = New Size(100, 20)
它的工作原理还不错,但我无法添加多个复选框而不需要添加为此:
It works just fine but i am unable to add more than one checkBox without having to do this:
Dim checkBox As New CheckBox()
Dim checkBox2 As New CheckBox()
Form1.Controls.Add(checkBox)
checkBox.Location = New Point(10, 10)
checkBox.Text = "testing"
checkBox.Checked = True
checkBox.Size = New Size(100, 20)
Form1.Controls.Add(checkBox2)
checkBox2.Location = New Point(40, 10)
checkBox2.Text = "testing2"
checkBox2.Checked = True
checkBox2.Size = New Size(100, 20)
等...
是否有一种方法可以使多个复选框变暗而不需要书写每个checkBox都有多个dim语句?
Is there a way to dim more than 1 checkbox instead of having to write multiple dim statements for each checkBoxe?
对不起,也许我应该这样说。.
Sorry maybe i should say this..
我想做这样的事情:
dim checkBox() as CheckBox
do until i = 50
Form1.Controls.Add(checkBox(i))
checkBox(i).Location = New Point(10, 10)
checkBox(i).Text = "testing " & i
checkBox(i).Checked = True
checkBox(i).Size = New Size(100, 20)
i += 1
loop
推荐答案
在 CheckBox
实例之间似乎唯一不同且未计算的项目是文本。如果是这样,那么您可以使用以下代码基于 String
的列表添加一组 CheckBox
实例。的。
It seems like the only items that are different and not calculated between the CheckBox
instances is the text. If so then you could just use the following code to add a set of CheckBox
instances based off of a list of String
's.
Dim data as String() = New String() { "testing", "testing2" }
Dim offset = 10
For Each cur in data
Dim checkBox = new CheckBox()
Form1.Controls.Add(checkBox)
checkBox.Location = New Point(offset, 10)
checkBox.Text = cur
checkBox.Checked = True
checkBox.Size = New Size(100, 20)
offset = offset + 30
Next
这篇关于vb.net动态创建复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!