Click here to Skip to main content
15,904,415 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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";

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
Posted
Updated 14-Jun-11 20:54pm
v2
Comments
RakeshMeena 15-Jun-11 3:15am    
Why are you downvoting? If you have a problem, post it! downvoting ain't gonna serve any purpose.

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:
C#
[System.Flags]
enum Frameworks { PHP = 1, Java = 2, Dotnet = 4, }


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:
C#
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();


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
 
Share this answer
 
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(",");
 
Share this answer
 
v2
Comments
tulasiram3975 15-Jun-11 3:02am    
Oh Great Thank You Sir
Prerak Patel 15-Jun-11 3:30am    
You are welcome. Mark it as answer if it is solved now.
You can try below JQuery sample:
<br />
$("input:checkbox:checked").each(function()<br />
{<br />
    strg += $(this).val() + ",";<br />
});<br />



Please note that I've considered that you have only these three checkboxes in your page. If there are more, then you can put these checkboxes under a container (say div tag) and use the container's id like:

<br />
$("#containerID input:checkbox:checked").each(function()<br />
{<br />
    strg += $(this).val() + ",";<br />
});<br />
 
Share this answer
 
v3
Comments
tulasiram3975 15-Jun-11 5:41am    
Good Call.. Thank You

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900