查找三个不同复选框中选中的复选框 [英] finding which checkbox selected in three diffrent checkboxes

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

问题描述

大家好..
我有三个复选框1.php 2.dot net 3.java
在此用户中,选择所需的复选框,我需要找出选中的复选框是这样的

hi all..
i have three checkbox 1.php 2.dot net 3.java
in this user select desired checkbox i need to find out which check box or boxes selected am going like this way

if(checkbox1.checked)
  strg="php";
if(checkbox2.checked)
  strg="dot net";
if(checkbox3.checked)
  strg="java";
if(checkbox1.checked || checkbox2.checked)
  strg="php"+","+"dot net";


依此类推...我需要写8种不同的条件,它变得更加复杂,我需要以简单的格式找到,任何人都可以帮我... 预先感谢


so on... i need to write 8 different conditions it became more complicated i need find in simple format can any one help me please...
thanks in advance

推荐答案

这个问题很容易解决-但这不是真正的问题.更大的问题是您正在尝试在UI中解决所有问题.

您正在以完全错误的方式处理问题表格.您不应该一直通过UI进行操作.稍后,您将需要将其转换为数据.你要做什么?

您需要从丢失的数据层开始.这很像尝试在不使用M(模型)的情况下使用MVP架构模式.请参见> http://en.wikipedia.org/wiki/Model-view-presenter [ ^ ],http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science) [
This problem is very easy to solve— but this is not a real problem. The bigger problem is that you''re trying to solve it all in UI.

You''re approaching the problem form in a completely wrong way. You should not do it via UI along. Later on, you will need to translate it to data. What you''re going to do?

You need to start from a data layer, which is missing. This is much like trying to use MVP architectural pattern without M (model). See http://en.wikipedia.org/wiki/Model-view-presenter[^], http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)[^].

Here is what you need to do.

Let''s start with meta-data.

Declare something like this:
[System.Flags]
enum Frameworks { PHP = 1, Java = 2, Dotnet = 4, }



这显示了您的数据模型的元数据.它特别应该告诉您的应用程序,您需要三个复选框来表示数据,并且它们的名称应为"PHP","Java"和"Dotnet".不幸的是,它不允许您输入一些易于理解的名称,例如"Microsoft .NET".不要担心;这是可使用.NET属性甚至资源解决的高级项目;您可以在我的文章
人类可读的枚举元数据 [



This presents the meta-data of your data model. It should tell your application, in particular, that you need three check boxes to represent data, and their names should be "PHP", "Java", and "Dotnet". Unfortunately, it won''t allow you to enter some human-readable names such as "Microsoft .NET". Not to worry; this is advanced item solvable using .NET Attributes and even resource; you can find a comprehensive solution in my article Human-readable Enumeration Meta-data[^]. For now, let''s keep it simple.

When the type for this data items is created, the data could get different values, like:

Frameworks frameworksUsed = Frameworks.PHP;
Frameworks frameworksToBeUsed = Frameworks.PHP | Frameworks.Dotnet;
Frameworks anotherCase = Frameworks.PHP | Frameworks.Java;
//etc.

//you need to present it in UI; here is how
//it will present a comma-separated list;
//thanks to the attribute Flags:
string currentlyUsed = frameworksUsed.ToString();



你明白了吗?您不应使用任何UI元素来呈现框架字符串.您应该使用数据.

现在,如何使用元数据?您最好的机会是自动生成遍历enum成员的集合或复选框.不幸的是,此类型不支持枚举(foreach).您可以解决此限制.有一些简单的技巧可以做到这一点;或者您可以使用我全面的迭代方法.我在另一篇文章中解释了简单的技巧和全面的Enumeration类:
枚举类型不枚举!解决.NET和语言限制 [ ^ ].

无论如何,您都要枚举框架并创建复选框.您可以通过枚举成员的名称来命名每个人,也可以使用我在本文中描述的易于理解的方法.更重要的是,您需要为每个复选框添加一个与相应的Frameworks成员相等的标签.
完成此操作后,可以向每个复选框的事件CheckBox.CheckedChanged添加一个句柄.例如,此事件句柄应操纵Frameworks(这种类型)的位集,该位可以是表单的成员.如果事件处理程序是这种形式的实例方法(自然),则它们将通过"this"指针具有该实例,因此它们可以在复选框的选中状态更改,选中或取消选中时修改(添加或删除)位.

每次处理程序触发时,您都应该更改当前选定框架的输出.如果这是标签,则应将其Text属性分配给frameworksUser.ToString().

这只是一个框架,但是实现起来很容易.更重要的是,它可以通过易于支持的方式来完成.如果以后您需要更改某些enum成员,则该UI将被几乎(或完全取决于您的UI技能)自动修改.

如果此类代码的某些片段不清楚,欢迎您提出后续问题.

祝你好运,

—SA



Are you getting it? You should not use any UI element to present the string of frameworks. You should use data.

Now, how to use meta-data? Your best chance is to auto-generate the set or check boxes iterating through the enum members. Unfortunately, this type does not support enumeration (foreach). You can work around this limitations. There are simple tricks to do it; or you can use my comprehensive method of iteration. I explained both simple techniques and a comprehensive Enumeration class in my other article: Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^].

No matter how, you enumerate the frameworks and create check boxes. You can name each one by a name of enumeration members or use a human-readable method I describer in my article. More important, you need to add a tag equal to corresponding Frameworks member to each check box.

When this is done, you can add a handle to the event CheckBox.CheckedChanged of each check box. This event handle should manipulate bit set of Frameworks (of this type) which can be a member of your form, for example. If the event handlers are the instance method of this form (naturally), they will have this instance through "this" pointer, so they can modify a bit (add or remove) as the check box checked state is changed, checked or unchecked.

Each time the handler fires, you should change output of the currently selected frameworks. If this is a label, you should assign its Text property to frameworksUser.ToString().

This is just a skeleton, but implementation is pretty easy. More importantly, it can be done in a way which is easy to support. Should you later need to change some of the enum members, the UI will be modified nearly (or completely, depending on you UI skills) automatically.

If some fragments of such code is not clear — your follow-up questions are welcome.

Good luck,

—SA


使用以下

Use following

strg="";

if(checkbox1.checked)
  strg=strg + "php,";
if(checkbox2.checked)
  strg=strg + "dot net,";
if(checkbox3.checked)
  strg=strg + "java,";

strg = strg.Trim(",");


您可以尝试以下JQuery示例:
You can try below JQuery sample:
<br />


这篇关于查找三个不同复选框中选中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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