如何在checkListBox中选中一个项目,然后在同一个checkListBox中输出选中的框,但在Form2.cs中。

问题描述 投票:0回答:1

我有一个学校的最终项目,这是我的任务之一,所以这个项目是关于做一个调查问卷,我正在与复选框部分挣扎,我必须做3个checkListBoxes,其中有4个选项,但问题是,当我点击程序上的提交,程序必须打开一个Form2应用程序与相同的3个checkListBoxes,但用户输入的答案必须在Form2中检查。Form2 form2 = newForm2(); form2.ShowDialog(); 我只找到了一个消息框的解决方案,所以如果有人能帮助我,我将非常感激。

谢谢你的时间!我有最后的学校项目,这是我的任务之一。

c# checkbox project final checklistbox
1个回答
0
投票

要发送你的答案,你有多种选择,你可以做一个列表,也可以发送一个字符串或一个int。由于你似乎在使用CheckBoxList,似乎你想传递所有的复选框,所以最好的办法是使用List。

首先,你需要创建Form2对象,并像这样在每个checkBoxList上粘贴所选项目的列表(我在这种情况下使用了一个按钮来打开第二个Form)。

  private void button1_Click(object sender, EventArgs e)
        {
            //you create the list
            List<int> answers1 = new List<int>();
            //add each of the checked indices
            foreach (int index in checkedListBox1.CheckedIndices)
                answers1.Add(index);
            //pass the parameter of the checked items to the other form
            new Form2(answers1).ShowDialog();
        }

然后你的Form2需要像这样。

public partial class Form2 : Form
    {
//You add the parameter of a list with the checked indices
        public Form2(List<int> answers1)
        {
            InitializeComponent();
//here you check every index selected in your Form2 checklist
            foreach(int index in answers1)
                checkedListBox1.SetItemChecked(index, true);
        }
    }

如果你需要添加更多的问题(也就是checklist),只需在Form2的参数中添加更多的checklist,并在Form1上创建时发送。

Pd.如果你想让用户只能从你的问题中选择一个选项,那么我建议你使用一个带有RadioButtons的GroupBox而不是CheckedBoxList。

© www.soinside.com 2019 - 2024. All rights reserved.