Checkedlistbox

Im looking for some assistance...what im trying to do is check to see what items are checked and say if item 1 and 3 are checked do this but if 2 and 4 are checked do this...I've tried a bunch of code but nothing seems to work..im doing this all in VB

Private Sub btn_ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ok.Click
Dim itemChecked As Object

For Each itemChecked In chklb_syscus.CheckedItems

If itemChecked = "ITEM1" And itemChecked = "ITEM2" Then

MessageBox.Show("ITEM 1 and ITEM 2 Was checked")

End If

Next
End sub




Answer this question

Checkedlistbox

  • LogicalGeekBoy

    Would this code still work if say i have this setup

    Item 1
    Item 2
    Item 3
    Item 4

    And a person selects Item 1 and Item 2 and Item 3....and i want it to do something
    But if a person only selects Item 1 and Item 2 do something else.  The problem i was runing into is when i program it to check to see if Item 1 and Item 2 are picked it would run the Item 1 and Item 2 code not the Item 1 /Item 2/ Item 3 code I guess i could do

    if( checkedItems.Contains( m_SomeItems[0] ) && checkedItems.Contains( m_SomeItems[1] )) && checkedItems.Contains( m_SomeItems[2] ))

  • Tadwick

    awsome this works thanks again :)


  • Jimmy Li - MSFT

    Your code is not quite right indeed. You are iterating over a collection and checking whether the current items equals two different values... which is always false.

    I have made a piece of sample code in C# (sorry), but if you study it just a little bit you will see how it works.
    I add a list of objects to the listbox (strings), and later on I compare the reference of the objects using CheckListBox.CheckedItemCollection.Contains.



    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace CheckedListBoxTest
    {
        public partial class Form1 : Form
        {
            private string[] m_SomeItems = new string[ 4 ];

            public Form1()
            {
                for( int i = 0; i < m_SomeItems.Length; ++i )
                {
                    m_SomeItems[ i ] = String.Format( "Item {0}", i );
                }

                InitializeComponent();
            }

            private void Form1_Load( object sender, EventArgs e )
            {
                foreach( string item in m_SomeItems )
                {
                    this.checkedListBox1.Items.Add( item );
                }
            }

            private void button1_Click( object sender, EventArgs e )
            {
                System.Windows.Forms.CheckedListBox.CheckedItemCollection checkedItems = checkedListBox1.CheckedItems;
                if( checkedItems.Contains( m_SomeItems[0] ) && checkedItems.Contains( m_SomeItems[1] ))
                {
                    MessageBox.Show( "Items 1 and 2 checked!");
                }
                if( checkedItems.Contains( m_SomeItems[2] ) && checkedItems.Contains( m_SomeItems[3] ))
                {
                    MessageBox.Show( "Items 3 and 4 checked!");
                }
            }

            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose( bool disposing )
            {
                if( disposing && ( components != null ) )
                {
                    components.Dispose();
                }
                base.Dispose( disposing );
            }

            #region Windows Form Designer generated code

            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
                this.button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                //
                // checkedListBox1
                //
                this.checkedListBox1.FormattingEnabled = true;
                this.checkedListBox1.Location = new System.Drawing.Point( 44, 12 );
                this.checkedListBox1.Name = "checkedListBox1";
                this.checkedListBox1.Size = new System.Drawing.Size( 195, 124 );
                this.checkedListBox1.TabIndex = 0;
                //
                // button1
                //
                this.button1.Location = new System.Drawing.Point( 88, 187 );
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size( 75, 23 );
                this.button1.TabIndex = 1;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler( this.button1_Click );
                //
                // Form1
                //
                this.AutoScaleDimensions = new System.Drawing.SizeF( 6F, 13F );
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size( 292, 266 );
                this.Controls.Add( this.button1 );
                this.Controls.Add( this.checkedListBox1 );
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler( this.Form1_Load );
                this.ResumeLayout( false );

            }

            #endregion

            private System.Windows.Forms.CheckedListBox checkedListBox1;
            private System.Windows.Forms.Button button1;
        }
    }

     



  • OlgaYev

    Easy, just check whether all three items were checked first and act upon it. If not all three were checked but the first two were checked, do something else.



  • Checkedlistbox