gpt4 book ai didi

c# - 在运行时创建控件

转载 作者:太空宇宙 更新时间:2023-11-03 19:17:51 24 4
gpt4 key购买 nike

我无法从运行时创建的 textboxes 获取值。

我希望用户从 checkedlistbox 中选择一些东西,并在每次单击按钮时创建的 textboxes 中输入他想要的任何值。

我怎样才能得到那些texboxes的名字?它们真的存在吗?我是初学者,我真的不明白它们是如何创建的。

这是我创建文本框的代码。

    private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int x = 466;
int y = 84;
foreach (var itemChecked in checkedListBox1.CheckedItems)
{
int i = 0;
TextBox tb = new TextBox();
tb.Location = new Point(x, y);
tb.Name = "txtBox" + i++.ToString();
Controls.Add(tb);
y += 30;
}

最佳答案

只需将 i 放在 foreach 之外即可。

int i = 0;
foreach (var itemChecked in checkedListBox1.CheckedItems)
{
i++;
string textBoxName = "textBox" + i.ToString();
TextBox tb = new TextBox();
tb.Location = new Point(x, y);
//tb.Name = "txtBox" + i++.ToString(); <--Your Version
tb.Name = textBoxName;
//...
//Other stuff or your codes
}

关于c# - 在运行时创建控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14894712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com