选择checkboxCombobox的一个值 [英] select one value of checkboxCombobox

查看:681
本文介绍了选择checkboxCombobox的一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了多个 checkboxcomboboxes 。对于我的解决方案,其中一个框需要在特定情况下作为组合框。我需要只选择一个值。我尝试以下:

i am using several checkboxcomboboxes. for my sollution one of these boxes needs to behave as a combobox in a specific situation. i need to select one value only. i tried the following:

    private void PreDefSerials_SelectedValueChanged(object sender, EventArgs e)
    {
        if (!one_select)
            return;
        else
        {
            // set selected value
            if (PreDefSerials.SelectedIndex != 0)
            PreDefSerials.CheckBoxItems[PreDefSerials.SelectedIndex].CheckState = CheckState.Checked;
            return;
        }
    }

EDTI:

如何将项目的所有检查状态设置为未选择,并且aftwerwards使最新选择的值的检查状态选择?

how can i set all the checkstateds of the items to not-selected and aftwerwards make the checkstate of the latest selected value to selected?

推荐答案

我不熟悉这个控件,所以我可能没有所有的语法正确,但我认为你可以尝试循环通过你的项目和取消选中任何东西检查。此外,您必须暂时关闭该活动,否则此活动可能会在每次取消选中时继续触发:

I'm not familiar with that control, so I might not have all of the syntax correct, but I think you can try looping through your items and "unchecking" anything that was checked. Also, you would have to turn "off" the event for the time being or else this event would probably keep firing on every "uncheck":

private void PreDefSerials_SelectedValueChanged(object sender, EventArgs e)
{
  if (!one_select)
    return;
  else
  {
    if (PreDefSerials.SelectedIndex > -1)
    {
      //only uncheck items if the current item was checked:
      if (PreDefSerials.CheckBoxItems[PreDefSerials.SelectedIndex].CheckState == CheckState.Checked)
      {
        // stop firing event for now:
        PreDefSerials.SelectedValueChanged -= PreDefSerials_SelectedValueChanged;

        for (int i = 0; i < PreDefSerials.CheckBoxItems.Count; i++)
        {
          if (i != PreDefSerials.SelectedIndex)
          {
            PreDefSerials.CheckBoxItems[i].CheckState = CheckState.Unchecked;
          }
        }

        // wire event again:
        PreDefSerials.SelectedValueChanged += PreDefSerials_SelectedValueChanged;
      }
    }
  }
}

假设当您检查一个项目时,它将被检查并触发此事件。此代码只是通过列表,然后取消选中其他一切。根据需要重构。

This assumes that when you check an item, it will get checked and fire this event. This code just goes through the list and then "unchecks" everything else. Refactor as needed.

这篇关于选择checkboxCombobox的一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆