Click here to Skip to main content
14,667,169 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to select the checkboxes from below form (wpf) and pass the value to C# as string array.
(output should be like this: TEST A, TEST C, TEST D)

in my C# code, on clicking OK button, the checked values should be passed and output as string.

Here is my sample xaml file

<listbox x:name="Options" selectionmode="Multiple" height="287" margin="14,12,10,0" verticalalignment="Top" rendertransformorigin="0.5,0.5"> <checkbox content="TEST A" ischecked="False" fontsize="15" height="35" width="190">
<checkbox content="TEST B" ischecked="False" fontsize="15" height="35" width="100">
 <checkbox content="TEST C" ischecked="False" fontsize="15" height="35" width="100">
<checkbox content="TEST D" ischecked="False" fontsize="15" height="35" width="100" rendertransformorigin="1.923,3.075">


Can anyone share example code to do it. I am new user to wpf and C#.
Any help appreciated.

What I have tried:

using below code i can get only one item at a time, but i want to get all checked items as a string list with "," separated.
private void OptOK_Click(object sender, RoutedEventArgs e)
{ 
    foreach (CheckBox item in Options.Items) 
    { 
        if (item.IsChecked == true)
        { 
            string ItemValue = item.Content.ToString();
            MessageBox.Show((ItemValue + "is checked."));
        } 
    } 
}
Posted
Updated 9-Sep-20 21:10pm
v2

Rate this:
Please Sign up or sign in to vote.

Solution 1

It should be obvious you'd only get one at a time - the message box is displayed each time you find a checked checkbox.

You just need to concatenate all the checkbox strings in the loop before you display the message box. Something like:

private void OptOK_Click(object sender, RoutedEventArgs e)
{ 
    string ItemValue = "";

    foreach (CheckBox item in Options.Items) 
    { 
        if (item.IsChecked == true)
        { 
            if (itemValue != "") {itemValue = itemValue + ", ";}
 
            ItemValue =  ItemValue + item.Content.ToString();
        } 
    }

    MessageBox.Show((ItemValue + " is checked."));
}
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Here is an example how to accomplish this.

MainWindow.xaml
<Window x:Class="CheckBoxTest.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:CheckBoxTest"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <ListBox Name="ListBoxOptions">
            <CheckBox Content="TEST A"></CheckBox>
            <CheckBox Content="TEST B"></CheckBox>
            <CheckBox Content="TEST C"></CheckBox>
            <CheckBox Content="TEST D"></CheckBox>
        </ListBox>
        <Button Grid.Row="1"

                Width="100"

                Height="25"

                Margin="5"

                Name="ButtonShowResults"

                Content="Show Results"

                Click="ButtonShowResults_OnClick"></Button>
    </Grid>
</Window>

MainWindow.xaml.cs
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace CheckBoxTest
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonShowResults_OnClick(object sender, RoutedEventArgs e)
        {
            var selectedItems = ListBoxOptions.Items.Cast<CheckBox>().Where(x => x.IsChecked == true).Select(x => x.Content).ToList();

            MessageBox.Show(string.Join("\n", selectedItems));
        }
    }
}

Short explanation:
First of all the Items of the ListBox which are just objects are cast to CheckBoxes as you want to access their Content properties.
Then these CheckBoxes are filtered by IsChecked == true.
Then their Contents which is just an IEnumerable will be converted to a List.
Finally the List will be concatenated with Line Feeds so that the MessageBox will show
e.g.
TEST A
TEST B

If you want to have a comma separated List instead
you can replace the separator like this.
string.Join(",", selectedItems)
   
v2

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100