【问题标题】:How can selections from a Combo Box be saved / remembered when a new form appears?当出现新表单时,如何保存/记住组合框中的选择?
【发布时间】:2014-04-04 04:08:24
【问题描述】:

假设有一个 Winform 允许用户使用组合框回答问题。一旦他们选择了一个答案,他们就可以点击“下一步”并看到一个新问题以及一个新的组合框来选择他们的答案。

如何保存上一页的答案,以便按下“上一个”,之前选择的相同答案仍将填充那个组合框?

编辑: 我在您的示例中遇到的问题与这两个有关。 "无法解析符号 comboBox1"

comboBox1.SelectedIndexChanged += SelectedAnswerChanged;
question.SelectedChoice = (AnswerChoice)comboBox1.SelectedItem;

【问题讨论】:

  • 嘿,看起来我们的 cmets 被删除了,只是想确保我的上一条评论解决了这个问题。
  • 您最后的评论是什么?我想在它被全部删除之前我没有看到它。
  • 查看我下面的 DisplayQuestion() 方法,而不是我以前的方法;尝试使用它,看看它是否修复它。
  • 它并没有直接解决我的问题,但根据您的回答,我能够环顾四周并找到解决问题的好方法。再次感谢您的时间和精力!

标签: c# winforms


【解决方案1】:

这就是你需要做的事情

将您的问题类别更改为:


public class Questions
{
    public string QuestionType { get; set; }
    public string QuestionText { get; set; }
    public Choice[] Choice { get; set; }
    public Choice SelectedChoice { get; set; }
}

这会为其添加一个Choice 对象,允许我们保存用户从组合框中选择的Choice

将您的 AnswerChoice 类更改为:


public class AnswerChoice
{
    public string AnswerText { get; set; }
    public Questions Question { get; set; }

    public override void ToString() {
        return AnswerText;
    }
}

这只是在 ToString 方法上添加了一个覆盖。这样做是允许我们将实际对象添加到 ComboBox 而不仅仅是 AnswerText。这给了它更多面向对象的方法,并允许我们在问题的SelectedChoice 中保存对该对象的引用。如果我们在没有覆盖 ToString 方法的情况下添加对象,那么组合框将显示类似于System.Namespace.ClassHere.AnswerChoice

的内容

将您的 ComboBoxControl 类更改为:


public partial class ComboBoxControl : UserControl
{
    public ComboBoxControl(Questions question)
    {
        InitializeComponent();
        label1.Text = question.QuestionText;

        Choice[] choices = question.Choice;

        foreach (var ch in choices)
        {
            comboBox1.Items.Add(ch.AnswerChoice);

            if (ch.IsDefault)
            {
              comboBox1.Text = ch.AnswerChoice;
            }
        }

        // load the saved answer if it exists
        if (question.SelectedChoice != null) {
            comboBox1.Text = string.Empty // not sure if this is needed or not
            comboBox1.SelectedItem = question.SelectedChoice;
        }           
    }
}

这里有两点改变:

  1. 我们添加实际的 AnswerChoice 对象,而不是添加 ch.AnswerChoice.AnswerText,以便我们保存对实际对象的引用以供以后使用
  2. 每当加载一个问题时,它都会检查SelectedChoice 属性是否为空。如果不是,则将该对象(已加载)设置为SelectedItem

在您的 SurveyView 表单中添加此方法:


private void SelectedAnswerChanged(object sender, EventArgs e) {
    Questions question = _presenter.GetQuestion(questionNumber);
    question.SelectedChoice = (Choice)((ComboBox)sender).SelectedItem;
}

这是我们稍后将绑定到我们创建的 ComboBox 上的 SelectedIndexChanged 事件的方法。这将保存用户选择的选项。

将您的 DisplayQuestion() 方法更改为:


private void DisplayQuestion(int questionNumber)
{
    var qNumber = _presenter.GetQuestion(questionNumber);
    if (string.Equals(qNumber.QuestionType, "ComboBoxControl"))
    {
        controlPanel.Controls.Clear();
        var comboBox = new ComboBoxControl(qNumber);
        comboBox.Dock = DockStyle.Fill;
        comboBox.SelectedIndexChanged += SelectedAnswerChanged;
        controlPanel.Controls.Add(comboBox);
    }
}

这只是将我们刚刚放在 SurveryView 表单中的 SelectedAnswerChanged 方法与我们创建的 ComboBox 上的 SelectedIndexChanged 事件联系起来。

【讨论】:

    猜你喜欢
    • 2018-05-06
    • 2023-04-02
    • 2013-11-26
    • 1970-01-01
    • 2011-07-08
    • 2011-04-26
    • 2015-02-13
    • 1970-01-01
    • 2017-05-05
    相关资源
    最近更新 更多