【问题标题】:How to check a item in checkListBox and then output the checked box in the same checkListBox, but in Form2.cs如何检查checkedListBox中的一个项目,然后在同一个checkedListBox中输出选中的框,但是在Form2.cs中
【发布时间】:2020-09-24 03:17:26
【问题描述】:

我有期末的学校项目,这是我的任务之一。 所以这个项目是关于制作一份问卷的。 我在复选框部分苦苦挣扎, 我必须制作 3 个带有 4 个选项的 checkListBoxes, 但问题是,当我在程序上单击提交时,程序必须打开一个具有相同 3 个 checkListBoxes 的 Form2 应用程序,但必须在 Form2 中检查用户输入的答案。 我唯一知道怎么做的是通过单击“提交”按钮打开 Form2 Form2 form2 = newForm2(); form2.ShowDialog(); 我只找到带有消息框的解决方案,所以如果有人可以帮助我,我将非常感激。

感谢您的宝贵时间!

【问题讨论】:

  • 您应该更改 newForm2 的构造函数以接受复选框值,并且在构造函数中您应该将这些值添加到 newForm2 中的复选框。当您在 Form1 中创建 new newFrom2 时,您应该在构造函数中从第一个表单的复选框中传递这些值。

标签: c# checkbox project final checklistbox


【解决方案1】:

要发送您的答案,您有多种选择,您可以创建一个列表或发送一个字符串或一个整数。由于您似乎正在使用 CheckBoxList,因此您似乎想要传递所有选中的框,所以最好的办法是使用 List。

首先,您需要创建 Form2 对象,然后像这样在每个 checkBoxList 上添加所选项目的列表(在这种情况下,我使用了一个按钮来打开第二个表单):

  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);
        }
    }

如果您需要添加更多问题(又名检查表),只需在 Form2 的参数中创建添加更多检查表,并在您在 Form1 上创建时发送。

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

【讨论】:

    猜你喜欢
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多