【问题标题】:restrict users from selecting value on previous index in Combobox c# winform限制用户在 Combobox c# winform 中选择先前索引的值
【发布时间】:2017-10-23 18:10:52
【问题描述】:

我在 winform 中有一个组合框。它绑定到一个枚举。枚举按顺序显示文章的状态。我希望用户遵循订单并限制用户在更新时选择以前的状态。我尝试了 selectedIndexchanged 事件,但没有成功。

public enum Articlestatus : Byte
    {
        Inplagiarism = 0,
        Consentletter = 1,
        Inreview = 2,
        AuthorRevision = 4,
        ReReview = 8,
        Reject = 16,
        Accept = 32,
        Published = 64
    }

【问题讨论】:

  • 是的,一些细节会有所帮助。
  • 如果您需要帮助修复它,您需要显示 what 不起作用。请阅读How to Ask 并采取tour
  • 如何确定状态何时为“选中”。你的意思是当他们选择一个值的那一刻,所有以前的值都应该被删除(或不允许被选择)?
  • @RufusL 未删除。不允许他们选择以前的值。

标签: c# winforms enums combobox


【解决方案1】:

一种方法是在变量中跟踪先前选择的项目,然后在SelectedIndexChanged 事件中,如果用户尝试选择更少的内容,则重新选择前一个项目:

// Keep track of currently selected index
private int lastSelectedIndex = 0;

private void Form1_Load(object sender, EventArgs e)
{
    comboBox1.DataSource = Enum.GetValues(typeof(Articlestatus));
    comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

    // Select first item and update our tracking variable
    comboBox1.SelectedIndex = 0;
    lastSelectedIndex = comboBox1.SelectedIndex;
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    // Do nothing if they re-selected the same item
    if (comboBox1.SelectedIndex == lastSelectedIndex) return;

    // If the newly selected item is less than the previous one, reset to previous one
    if (comboBox1.SelectedIndex < lastSelectedIndex)
    {
        comboBox1.SelectedIndex = lastSelectedIndex;
    }
    else
    {
        lastSelectedIndex = comboBox1.SelectedIndex;
    }
}

请注意,此代码对用户来说不是很灵活。如果他们不小心选择了错误的项目,他们就会被卡住。我想更新lastSelectedIndex 的代码应该放在其他地方,比如在一些“TaskCompleted”事件中,当该事件被触发时,表示他们已经完成了一些将他们提交给选择的事情。

【讨论】:

  • 这就是问题所在。基本上,我在 cellclick 事件上处理来自 gridview 的值,并且默认情况下某些文章具有以前的值。否则,它可以工作。
猜你喜欢
  • 2017-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-03
  • 2011-11-14
  • 1970-01-01
  • 2019-09-25
  • 1970-01-01
相关资源
最近更新 更多