繁体   English   中英

C#Windows窗体:将每个文本框与选定的ListBox项目相关联

[英]C# Windows Form: Associating a text box each with selected ListBox Item

我创建了一个约12个项目的CheckedListBox。 我想动态创建文本框,以在每次选择项目时在表单上显示并接受用户的输入值。 最终选择一个按钮时,Textboxes值将与Selected Items值相关联,以存储到某些Sql数据库表中。

到目前为止,即使我从列表框中选择了其他项目,我也只能显示一个文本框。 下面是我的代码

    private void checkedList_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        if (e.NewValue == CheckState.Checked)
        {
            int n = 6;
            selectedList.Items.Add(checkedList.SelectedItem.ToString());
            for (int x=0; x < checkedList.SelectedItems.Count; x++)
            {
                TextBox[] txtBoxes = new TextBox[n];
                for (int i = 0; i < n; i++)
                {
                    txtBoxes[i] = new TextBox();
                }
                for (int i = 0; i < n; i++)
                {
                this.Controls.Add(txtBoxes[i]);
                txtBoxes[i].Location = new Point(450, 100);
                txtBoxes[i].Size = new System.Drawing.Size(25, 20);
                txtBoxes[i].Validating += new CancelEventHandler(txt_Validating);
                }
            }
        }
        else
        {
            selectedList.Items.Remove(checkedList.SelectedItem.ToString());
        }

    }
    private void InitializeMyListBox()
    {
        for (int x = 0; x < selectedList.Items.Count; x++)
        {
            selectedList.SetSelected(x, true);
        }
        // Force the ListBox to scroll back to the top of the list.
        selectedList.TopIndex = 0;
    }

我做错了什么?

你能帮忙吗?

您已经为所有文本框设置了相同的位置。 尝试设置以下内容:

txtBoxes[i].Location = new Point(450 + i*20, 100);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM