vb.net 动态创建复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3620847/
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-09 15:01:18  来源:igfitidea点击:

vb.net dynamically create checkboxes

vb.netcheckbox

提问by StealthRT

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.

当我不知道确切需要多少个复选框时,我试图弄清楚如何在表单上创建动态复选框。

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)

etc...

等等...

Is there a way to dim more than 1 checkbox instead of having to write multiple dim statements for each checkBoxe?

有没有办法使 1 个以上的复选框变暗,而不必为每个复选框编写多个变暗语句?

Sorry maybe i should say this..

对不起,也许我应该这样说..

I'm looking to do something like 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

我正在做这样的事情:

 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

回答by JaredPar

It seems like the only items that are different and not calculated between the CheckBoxinstances is the text. If so then you could just use the following code to add a set of CheckBoxinstances based off of a list of String's.

似乎在CheckBox实例之间唯一不同且未计算的项目是文本。如果是这样,那么您可以使用以下代码CheckBox根据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

回答by Jonathan

Put it in a loop, including the new statement but varing the position.

把它放在一个循环中,包括新语句但改变位置。

You could also clone the object, maybe with performance penalties ... Sorry but don't know Vb.net, I will give you the c# code hoping it will be similar. I think this it is not the best solution for your case (a loop will do the trick), but maybe it will be for someone with a similar but more generic problem.

你也可以克隆这个对象,也许会降低性能......对不起,但不知道 Vb.net,我会给你 c# 代码,希望它会相似。我认为这不是您的情况的最佳解决方案(循环可以解决问题),但也许它适用于有类似但更通用问题的人。

CheckBox CB2 = (CheckBox)CloneObject(CheckBox1);

//change the location here... Form1.Controls.Add(checkBoxCB2 )

//改变这里的位置... Form1.Controls.Add(checkBoxCB2 )

private object CloneObject(object o)
{
   Type t = o.GetType();
   PropertyInfo[] properties = t.GetProperties();

   Object p = t.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, o, null);

   foreach(PropertyInfo pi in properties)
      {
         if(pi.CanWrite)
           {
              pi.SetValue(p, pi.GetValue(o, null), null);
           }
      }

   return p;
}