0
votes

I have tried looking round for a solution to my problem but have failed to find one as it seems every one is a step or two ahead of my problem.

I am trying to select an item from it being checked from a checkboxlist rather than selecting an item from it being selected.

What I intend to do from knowing that is to make the resulting event that would fire off, after a button is clicked and the checked options are checked, to show text in a label off the checked items.

The program, based off the decorator pattern, would allow the user to choose from a set of 3/4 checkable options of which when a button is pressed will show text relating to those items in a label on the end of the base text. At the moment, all I have managed is to get it to do so on the selected item one at a time only similar to the first example.

For example, when an option called Monitor is checked it would show in the label:

You're getting a Computer and a monitor.

If there are multiple checked items such as Monitor and Keyboard then it would say:

You're getting a Computer and a monitor and a keyboard.

1
Just want to clarify, you want to trigger the list based on a checkbox tick event? - bonCodigo
Yes, windows forms application. The list of options is generated on the application starting, user presses a button of which the event described, text appearing in label as per the option checked, is triggered. - madman

1 Answers

0
votes

You may change the Label.Text property of the target Label when the ItemCheck event of the CheckedListBox is fired based on the new checked item value.

Example

Assuming that you have a Label of name label1, a CheckedListBox of name checkedListBox1 and a Form of name Form1, the following may apply

public class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        label1.Text = "You are getting "; //Change the Text property of label1 to "You are getting "
        checkedListBox1.ItemCheck += new ItemCheckEventHandler(checkedListBox1_ItemCheck); //Link the ItemCheck event of checkedListBox1 to checkedListBox1_ItemCheck; not required as long as you link the event through the designer
    }
    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        if (e.NewValue == CheckState.Checked && e.CurrentValue == CheckState.Unchecked) //Continue if the new CheckState value of the item is changing to Checked
        {
            label1.Text += "a " + checkedListBox1.Items[e.Index].ToString() + ", "; //Append ("a " + the item's value + ", ") to the label1 Text property
        }
        else if (e.NewValue == CheckState.Unchecked && e.CurrentValue == CheckState.Checked) //Continue if the new CheckState value of the item is changing to Unchecked
        {
            label1.Text = label1.Text.Replace("a " + checkedListBox1.Items[e.Index].ToString() + ", ", ""); //Replace ("a " + the item's value + ", ") with an empty string and assign this value to the label1 Text property
        }
    }
}

Sample Input

[x] Monitor
[x] Keyboard
[ ] Mouse
[x] Computer

Sample Output

You are getting a Monitor, a Keyboard, a Computer, 

Thanks,
I hope you find this helpful :)