You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从父窗体QuizCreationForm向子TrueFalse控件回显选中的ListBox问题?

解决方案

步骤1:给TrueFalse控件添加加载问题的方法

在TrueFalse.cs中新增一个方法,用于接收问题数据并填充到控件的输入项中:

// 替换YourQuestionType为你项目中实际的问题实体类
public void LoadQuestion(YourQuestionType question)
{
    // 假设你的TrueFalse控件有输入题目文本框和对错复选框,根据实际控件名调整
    txtQuestionContent.Text = question.QuestionContent;
    chkIsCorrect.Checked = question.IsCorrectAnswer;
    // 其他需要回显的属性同理设置
}

步骤2:处理QuizCreationForm中ListBox的选中事件

在QuizCreationForm.cs的questionList_SelectedIndexChanged事件方法中,获取选中的问题并传递给TrueFalse控件:

private void questionList_SelectedIndexChanged(object sender, EventArgs e)
{
    if (questionList.SelectedItem == null) return;

    // 转换选中项为问题实体
    YourQuestionType selectedQuestion = (YourQuestionType)questionList.SelectedItem;
    
    // 查找窗体中的TrueFalse控件实例(如果控件在TabControl等容器内,需调整查找逻辑)
    TrueFalse tfControl = this.Controls.OfType<TrueFalse>().FirstOrDefault();
    if (tfControl != null)
    {
        tfControl.LoadQuestion(selectedQuestion);
    }
}

关键排查点

  • 确保questionList中添加的是问题实体对象而非字符串:如果之前添加的是字符串,需改为添加实体类,并在实体类中重写ToString()方法,让ListBox显示题目文本:
public class YourQuestionType
{
    public string QuestionContent { get; set; }
    public bool IsCorrectAnswer { get; set; }

    public override string ToString()
    {
        return QuestionContent; // ListBox中显示题目内容
    }
}
  • 确认questionList的SelectedIndexChanged事件已绑定到上述处理方法(可在设计器中检查事件绑定状态)。
  • 如果TrueFalse是动态加载的,需确保触发选中事件时,控件已存在于父窗体的控件集合中。

内容的提问来源于stack exchange,提问作者bloodthirsy berserker86

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 07:33:08