Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Windows窗体:将每个文本框与选定的列表框项关联_C#_Winforms_Dynamic_Controls - Fatal编程技术网

C#Windows窗体:将每个文本框与选定的列表框项关联

C#Windows窗体:将每个文本框与选定的列表框项关联,c#,winforms,dynamic,controls,C#,Winforms,Dynamic,Controls,我创建了一个包含大约12项的CheckedListBox。我想动态创建文本框,以便在每次选择某个项目时显示并接受表单上用户的输入值。最终选择按钮时,TextBox值将与要存储到某个Sql数据库表中的选定项值相关联 到目前为止,我所能做的是,即使我从列表框中选择了其他项目,也会显示一个文本框。下面是我的代码 private void checkedList_ItemCheck(object sender, ItemCheckEventArgs e) { if (e.

我创建了一个包含大约12项的CheckedListBox。我想动态创建文本框,以便在每次选择某个项目时显示并接受表单上用户的输入值。最终选择按钮时,TextBox值将与要存储到某个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;
    }
private void checkedList\u ItemCheck(对象发送方,ItemCheckEventArgs e)
{
if(e.NewValue==CheckState.Checked)
{
int n=6;
selectedList.Items.Add(checkedList.SelectedItem.ToString());
对于(int x=0;x
我做错了什么


你能帮个忙吗。

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

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

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

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

它们彼此重叠,您必须选择另一个位置或使用FlowLayoutPanel。使用单独的文本框创建自己的网格控件很少是一个错误,始终优先选择DataGridView。它们都是相互重叠的,您必须选择其他位置或使用FlowLayoutPanel。使用单独的文本框创建自己的网格控件很少不会出错,请首先选择DataGridView。谢谢您的输入。即使设置了txtboxs[i],其他文本框仍不显示。位置=新点(450+x*20100);我犯了个错误,你应该和i相乘,而不是x。所以使用这个:txtboxs[i].Location=newpoint(450+i*20100);我感谢你的意见。即使设置了txtboxs[i],其他文本框仍不显示。位置=新点(450+x*20100);我犯了个错误,你应该和i相乘,而不是x。所以使用这个:txtboxs[i].Location=newpoint(450+i*20100);