简体   繁体   中英

How do I get all checkboxes.text that is selected by a user on C#

Hello again I need help so I came here. I just can't figured out how to do this.

But here's what I want I have ac# form with 6 checkboxes on it. Checkboxes.text value are all Name of programming languages.

[]PHP

[]C

[]C++

[]VB.Net

[]Java

[]C#

Suppose a user click two or more checkboxes how can I display it.

I'm getting confuse on multiple selections part.

Here's my code

For the single selection

   If (Checkbox1.Checked == True)
{
MessageBox.Show(CheckBox1.Text);
}

For two selection

    If (CheckBox1.Checked == True && CheckBox2.Checked == True)
{
MessageBox.Show(CheckBox1.Text);
}

I use If statements on checkboxes for single or even more selected checkboxes.

How can I code it easier or even simplier. Is it possible?

Here i am saying the answer for the question by using tech like C# and Windows Presentation Form.

1.First you need to create a global function for all the check boxes. Ex:-Function Name-Check_Checked

<StackPanel Grid.Column="0" Margin="0 10 0 0">
<CheckBox Checked="Check_Checked" Content="Weld" x:Name="WeldCheck"/>
<CheckBox Checked="Check_Checked" Content="Assembly" x:Name="AssemblyCheck"/>
<CheckBox Checked="Check_Checked" Content="Plasma"  x:Name="PlasmaCheck"/>
<CheckBox Checked="Check_Checked" Content="Laser"  x:Name="LaserCheck"/>
<CheckBox Checked="Check_Checked" Content="Purchase"  x:Name="PurchaseCheck"/>
</StackPanel>
<StackPanel Grid.Column="1" Margin="0 10 0 0">
<CheckBox Checked="Check_Checked" Content="Lathe" x:Name="LatheCheck"/>
<CheckBox Checked="Check_Checked" Content="Drill" x:Name="DrillCheck"/>
<CheckBox Checked="Check_Checked" Content="Fold" x:Name="FoldCheck"/>
<CheckBox Checked="Check_Checked" Content="Roll" x:Name="RollCheck"/>
<CheckBox Checked="Check_Checked" Content="Saw" x:Name="SawCheck"/>
</StackPanel>

2.define the function in C#

private void Check_Checked(object sender, RoutedEventArgs e)
    {
        this.LengthText.Text += (string)((CheckBox)sender).Content;
        MessageBox.Show(this.LengthText.Text);
    }

With only one line of code you get the names of the check boxes in the text box and also in message box.

Thank You.

You can iterate all form's controls and get text properties of checked CheckBoxes with LINQ in one line:

    private void submitButton_Click(object sender, EventArgs e)
    {
        List<string> checkedItems=(from Control c in Controls where c is CheckBox && ((CheckBox)c).Checked select c.Text).ToList();
        MessageBox.Show(string.Join(";", checkedItems));
    }

I will do like that: make checkboxes list and then itterate through them:

List<string>Result=new List<string>();

Checkbox[] checkBox=new CheckBox[6];
checkBox[0]=checkBox1;
checkBox[1]=checkBox2;
checkBox[2]=checkBox3;
checkBox[3]=checkBox4;
checkBox[4]=checkBox5;
checkBox[5]=checkBox6;

for(int i=0; i<6; i++)
{
    if(checkBox[i].Checked)
    {
        Result.Add(checkBox[i].Text);
    }
}

And then do whatever you want with this Result list

Young grasshopper, you will most commonly do this type of operation in two steps:

  1. Collect the statuses.
  2. Display the summary of the statuses.

C# WinForms Example:

private void button_Click(object sender, EventArgs e)
{
    var sb = new StringBuilder();       // this will be your message

    // 1. Loop over all the controls in the form and collect the statuses.
    // (if you want to limit it to only some checkboxes you could put them 
    // in a gui container or array, and loop over yourContainer.Controls instead.)
    foreach (var cb in Controls)        
    {
        // append to string if this is a checkbox AND it is checked
        var c = cb as CheckBox;
        if (c != null && c.Checked)    
        {
            sb.Append(c.Text + "\n");  // ps "\n" is a new line in the message
        }
    }

    // 2. display the message if any checkbox was selected.
    var message = sb.ToString();
    if (!string.IsNullOrWhiteSpace(message)) // not empty?
        MessageBox.Show(message);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM