C#中的10个复选框多种组合 [英] 10 check-boxes multiple combinations in c#

查看:98
本文介绍了C#中的10个复选框多种组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不编写一百万条if语句的情况下进行遍历?这是我弄错的代码示例,因为它将仅选中每个复选框,然后继续进行下一个操作.忘了说所有十个复选框都在一个面板中,它们需要以任何组合方式进行选中.我不想写&&的堆或||我什至无法计算组合,请帮助我.

How do I loop through without writing a million if statements? Here's a sample of the code I have it wrong as it will only check each box and then move on to the next. Forgot to say that all ten check boxes are in a panel and they need to be check in any combination. I don't want to write heaps of && or || I cant even count the combinations Please help me.

if (cbxTitle.Checked == true)
{
      searched = "title";
}
else if (cbxAuthor.Checked == true)
{
      searched = "author";
}
else if (cbxYear.Checked == true)
{
      searched = "year";
}
else if (cbxWeight.Checked == true)
{
      searched = "weight";
}

推荐答案

似乎您希望包括用户选择的所有搜索条件.如果没有,那么我肯定会使用单选按钮.

It seems you want to include all search conditions selected by the user. If you didn't, then I'd definitely use radio buttons.

为了避免无尽的 if / else 语句,我将在所有 CheckBox 控件上使用 foreach 循环,并且-假设您的变量名保持一致-然后可以使用该名称的子字符串附加到搜索条件中.

To avoid endless if/else statements, I'd use a foreach loop on all CheckBox controls, and--assuming you've been consistent in your variable names--you could then use a substring of the name to be appended to your search criteria.

因此您的算法将或多或少:

So your algorithm would be more or less:

  • 获取所有类型为复选框
  • 的控件的列表
  • 对于找到的每个控件,如果已被选中,请提取其名称
  • 通过删除"cbx"前缀来获得此控件表示的搜索条件
  • 将此条件附加到您的搜索"字符串

应您的要求,我要添加一个样本.

At your request, I'm adding a sample.

由于它是搜索应用程序,所以我认为您将拥有搜索选项"表格.

Since it's a search application, I thought you would have a "Search Options" form.

这是初始化此表单的部分.

Here's the section that initializes this form.

namespace SearchApplication
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;

    public partial class SearchOptionsForm : Form
    {
        // prefix you used for all your checkboxes
        private const string prefix = @"cbx";

        // store *all* checkboxes in your form
        // if you had other checkboxes in your form, 
        // you would need to think how you would want to differentiate them
        private IEnumerable<CheckBox> searchOptionControls;

        // represents all choices made by the user to customize the search type
        private string searched;

        // since your 'searched' variable is a string, 
        // you need a filter separator to be able to tell user selections apart
        private const string optionSeparator = @";";

        public SearchOptionsForm()
        {
            this.InitializeComponent();

            // initialize your collection of checkboxes
            this.searchOptionControls = this.Controls.OfType<CheckBox>();
        }
    }
}

这是用户点击应用"后对用户输入做出反应的代码

And here's the code that reacts to the user input, after she clicks "Apply"

namespace SearchApplication
{
    using System;
    using System.Windows.Forms;

    public partial class SearchOptionsForm : Form
    {
        /// <summary>
        /// Updates the 'searched' string with the selections made by the user.
        /// </summary>
        /// <param name="sender">The 'Apply' button.</param>
        /// <param name="e">Not used in this implementation.</param>
        private void ApplyClicked(object sender, EventArgs e)
        {
            // reset your filter after every click
            this.searched = string.Empty;

            // inspect all controls of interest
            foreach (var currentOption in this.searchOptionControls)
            {
                // determine if user wants to use this filter 
                if (currentOption.Checked)
                {
                    // append to your existing search options
                    searched += currentOption.Name.Substring(prefix.Length);

                    // include the filter separator
                    searched += optionSeparator;
                }
            }

            // note that filter order in the string doesn't match display order
            this.textBoxSearchFilters.Text = searched;
        }
    }
}

这篇关于C#中的10个复选框多种组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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