How can i check in the listBox User Control if all the clicked items are now only Red?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I created a listBox Control User. I wanted to add a new bool type property or something that will indicate if the items that have been clicked on the listBox are only Red or some of them Red and one of them is original color.
Now this is working so if i click left click on an item in the listBox it will be regular will select one item each click and use the original color around it.
If i click on one item or multiple items with Right mouse click so the items i clicked on with the right click will be in Red.
The new proprty i want it to tell me if there are any item with its original color or that all the items that have been selected are Red only.
I want to do something if its only Red and do something else if one of the items is with the original color.
This is the SelectedIndex event of the listBox im using:
<pre class="lang-cs prettyprint prettyprinted
Code:
<span class="kwd private<span class="pln  <span class="kwd void<span class="pln  listBox1_SelectedIndexChanged<span class="pun (<span class="kwd object<span class="pln  sender<span class="pun ,<span class="pln  <span class="typ System<span class="pun .<span class="typ EventArgs<span class="pln  e<span class="pun )<span class="pln 
        <span class="pun {<span class="pln 
            item <span class="pun =<span class="pln  listBoxControl1<span class="pun .<span class="typ MyListBox<span class="pun .<span class="typ SelectedItem<span class="pun .<span class="typ ToString<span class="pun ();<span class="pln             <span class="kwd this<span class="pun .<span class="pln f1<span class="pun .<span class="typ PlayLightnings<span class="pun ();<span class="pln 
            f1<span class="pun .<span class="pln pdftoolsmenu<span class="pun ();<span class="pln 
            <span class="typ int<span class="pln  indx <span class="pun =<span class="pln  listBoxControl1<span class="pun .<span class="typ MyListBox<span class="pun .<span class="typ SelectedIndex<span class="pun ;<span class="pln 
            <span class="kwd if<span class="pln  <span class="pun (<span class="pln listBoxControl1<span class="pun .<span class="typ Indices<span class="pun .<span class="typ Contains<span class="pun (<span class="pln indx<span class="pun ))<span class="pln 
            <span class="pun {<span class="pln 
                <span class="kwd if<span class="pln  <span class="pun (<span class="pln item <span class="pun !=<span class="pln  <span class="kwd null<span class="pln  <span class="pun &&<span class="pln  <span class="pun !<span class="pln pdf1<span class="pun .<span class="typ Lightnings<span class="pun .<span class="typ Contains<span class="pun (<span class="pln item<span class="pun .<span class="typ ToString<span class="pun ()))<span class="pln 
                <span class="pun {<span class="pln 
                    pdf1<span class="pun .<span class="typ Lightnings<span class="pun .<span class="typ Add<span class="pun (<span class="pln item<span class="pun .<span class="typ ToString<span class="pun ());<span class="pln 
                <span class="pun }<span class="pln 
            <span class="pun }<span class="pln 
        <span class="pun }
[/code]
And this is the User Control code of the listBox:
I have in a new Form the User Control in the designer and in the User Control designer i have a listBox:
<pre class="lang-cs prettyprint prettyprinted
Code:
<span class="com /*----------------------------------------------------------------
 * Module Name  : ListBoxControl
 * Description  : Change listBox items color
 * Author       : Danny
 * Date         : 30/12/2012
 * Revision     : 1.00
 * --------------------------------------------------------------*/<span class="pln 

using <span class="typ System<span class="pun ;<span class="pln 
using <span class="typ System<span class="pun .<span class="typ Collections<span class="pun .<span class="typ Generic<span class="pun ;<span class="pln 
using <span class="typ System<span class="pun .<span class="typ ComponentModel<span class="pun ;<span class="pln 
using <span class="typ System<span class="pun .<span class="typ Drawing<span class="pun ;<span class="pln 
using <span class="typ System<span class="pun .<span class="typ Data<span class="pun ;<span class="pln 
using <span class="typ System<span class="pun .<span class="typ Linq<span class="pun ;<span class="pln 
using <span class="typ System<span class="pun .<span class="typ Text<span class="pun ;<span class="pln 
using <span class="typ System<span class="pun .<span class="typ Windows<span class="pun .<span class="typ Forms<span class="pun ;<span class="pln 

<span class="com /*
 *  Introduction :
 * 
 *  By default the color is red.
 *  Added a property to change the color.
 *  Right mouse click on item to change item color.
 *  Left mouse click on item to change the item color back.
 *  If the listBox is empty the control will be filled with 10 "Test" items.
 * */<span class="pln 

namespace <span class="typ Lightnings_Extractor<span class="pln  <span class="com // to check how and change the namespace to listBoxControl<span class="pln 
<span class="pun {<span class="pln 
    <span class="kwd public<span class="pln  <span class="kwd partial<span class="pln  <span class="kwd class<span class="pln  <span class="typ ListBoxControl<span class="pln  <span class="pun :<span class="pln  <span class="typ UserControl<span class="pln 
    <span class="pun {<span class="pln 
        <span class="kwd private<span class="pln  <span class="typ Color<span class="pln  m_MyListColor<span class="pun ;<span class="pln 
        <span class="kwd private<span class="pln  <span class="typ List<span class="str <int><span class="pln  m_itemIndexes <span class="pun =<span class="pln  <span class="kwd new<span class="pln  <span class="typ List<span class="str <int><span class="pun ();<span class="pln 
        <span class="kwd private<span class="pln  <span class="typ List<span class="str <int><span class="pln  m_coloringItemIndexes <span class="pun =<span class="pln  <span class="kwd new<span class="pln  <span class="typ List<span class="str <int><span class="pun ();<span class="pln 
        <span class="kwd public<span class="pln  <span class="kwd static<span class="pln  <span class="typ int<span class="pln  counter<span class="pun ;<span class="pln 
        <span class="kwd public<span class="pln  <span class="kwd event<span class="pln  <span class="typ EventHandler<span class="pun <<span class="typ ItemEventArgs<span class="pun ><span class="pln  <span class="typ ItemRemoved<span class="pun ;<span class="pln 

        <span class="kwd public<span class="pln  <span class="typ List<span class="str <int><span class="pln  <span class="typ Indices<span class="pln 
        <span class="pun {<span class="pln 
            get <span class="pun {<span class="pln  <span class="kwd return<span class="pln  m_itemIndexes<span class="pun ;<span class="pln  <span class="pun }<span class="pln 
        <span class="pun }<span class="pln 

        <span class="kwd public<span class="pln  <span class="typ ListBoxControl<span class="pun ()<span class="pln 
        <span class="pun {<span class="pln 
            <span class="typ InitializeComponent<span class="pun ();<span class="pln 

            counter <span class="pun =<span class="pln  <span class="lit 0<span class="pun ;<span class="pln 
            <span class="kwd if<span class="pln  <span class="pun (<span class="pln listBox1<span class="pun .<span class="typ Items<span class="pun .<span class="typ Count<span class="pln  <span class="pun ==<span class="pln  <span class="lit 0<span class="pun )<span class="pln 
            <span class="pun {<span class="pln 
                <span class="kwd for<span class="pln  <span class="pun (<span class="typ int<span class="pln  i <span class="pun =<span class="pln  <span class="lit 0<span class="pun ;<span class="pln  i <span class="pun <<span class="pln  <span class="lit 10<span class="pun ;<span class="pln  i<span class="pun ++)<span class="pln 
                <span class="pun {<span class="pln 
                    listBox1<span class="pun .<span class="typ Items<span class="pun .<span class="typ Add<span class="pun (<span class="str "Test "<span class="pln  <span class="pun +<span class="pln  i<span class="pun );<span class="pln 
                <span class="pun }<span class="pln 
            <span class="pun }<span class="pln 
        <span class="pun }<span class="pln 

        <span class="kwd private<span class="pln  <span class="kwd void<span class="pln  listBox1_MouseDown<span class="pun (<span class="kwd object<span class="pln  sender<span class="pun ,<span class="pln  <span class="typ MouseEventArgs<span class="pln  e<span class="pun )<span class="pln 
        <span class="pun {<span class="pln 
            <span class="typ int<span class="pln  index <span class="pun =<span class="pln  listBox1<span class="pun .<span class="typ IndexFromPoint<span class="pun (<span class="pln e<span class="pun .<span class="pln X<span class="pun ,<span class="pln  e<span class="pun .<span class="pln Y<span class="pun );<span class="pln 

            <span class="kwd if<span class="pln  <span class="pun (<span class="pln e<span class="pun .<span class="typ Button<span class="pln  <span class="pun ==<span class="pln  <span class="typ System<span class="pun .<span class="typ Windows<span class="pun .<span class="typ Forms<span class="pun .<span class="typ MouseButtons<span class="pun .<span class="typ Right<span class="pun )<span class="pln 
            <span class="pun {<span class="pln 
                <span class="kwd if<span class="pln  <span class="pun (<span class="pln m_itemIndexes<span class="pun .<span class="typ Contains<span class="pun (<span class="pln index<span class="pun ))<span class="pln 
                    <span class="kwd return<span class="pun ;<span class="pln 

                m_itemIndexes<span class="pun .<span class="typ Add<span class="pun (<span class="pln index<span class="pun );<span class="pln 
                <span class="typ DrawItem<span class="pun (<span class="pln index<span class="pun );<span class="pln 
                counter <span class="pun +=<span class="pln  <span class="lit 1<span class="pun ;<span class="pln 
            <span class="pun }<span class="pln 
            <span class="kwd else<span class="pln  <span class="kwd if<span class="pln  <span class="pun (<span class="pln e<span class="pun .<span class="typ Button<span class="pln  <span class="pun ==<span class="pln  <span class="typ MouseButtons<span class="pun .<span class="typ Left<span class="pun )<span class="pln 
            <span class="pun {<span class="pln 
                <span class="kwd if<span class="pln  <span class="pun (!<span class="pln m_itemIndexes<span class="pun .<span class="typ Contains<span class="pun (<span class="pln index<span class="pun ))<span class="pln 
                    <span class="kwd return<span class="pun ;<span class="pln 

                m_itemIndexes<span class="pun .<span class="typ Remove<span class="pun (<span class="pln index<span class="pun );<span class="pln 
                <span class="typ DrawItem<span class="pun (<span class="pln index<span class="pun );<span class="pln 
                <span class="typ OnItemRemoved<span class="pun (<span class="pln index<span class="pun ,<span class="pln  listBox1<span class="pun .<span class="typ Items<span class="pun [<span class="pln index<span class="pun ].<span class="typ ToString<span class="pun ());<span class="pln 
            <span class="pun }<span class="pln   
            listBox1<span class="pun .<span class="typ SelectedIndex<span class="pln  <span class="pun =<span class="pln  index<span class="pun ;<span class="pln 
        <span class="pun }<span class="pln 

        <span class="kwd protected<span class="pln  <span class="kwd virtual<span class="pln  <span class="kwd void<span class="pln  <span class="typ OnItemRemoved<span class="pun (<span class="typ int<span class="pln  indx<span class="pun ,<span class="pln  <span class="kwd string<span class="pln  name<span class="pun )<span class="pln 
        <span class="pun {<span class="pln 
            <span class="typ EventHandler<span class="pun <<span class="typ ItemEventArgs<span class="pun ><span class="pln  handler <span class="pun =<span class="pln  <span class="typ ItemRemoved<span class="pun ;<span class="pln 

            <span class="kwd if<span class="pun (<span class="pln handler <span class="pun !=<span class="pln  <span class="kwd null<span class="pun )<span class="pln 
                <span class="typ ItemRemoved<span class="pun (<span class="kwd this<span class="pun ,<span class="pln  <span class="kwd new<span class="pln  <span class="typ ItemEventArgs<span class="pun ()<span class="pln  <span class="pun {<span class="pln   <span class="typ Index<span class="pln  <span class="pun =<span class="pln  indx<span class="pun ,<span class="pln  <span class="typ Name<span class="pln  <span class="pun =<span class="pln  name<span class="pun });<span class="pln 
        <span class="pun }<span class="pln 

        <span class="kwd private<span class="pln  <span class="kwd void<span class="pln  listBox1_DrawItem<span class="pun (<span class="kwd object<span class="pln  sender<span class="pun ,<span class="pln  <span class="typ DrawItemEventArgs<span class="pln  e<span class="pun )<span class="pln 
        <span class="pun {<span class="pln 
            m_MyListColor <span class="pun =<span class="pln  <span class="typ MyListColor<span class="pun ;<span class="pln 
            <span class="kwd if<span class="pln  <span class="pun (<span class="pln m_MyListColor<span class="pun .<span class="typ IsEmpty<span class="pln  <span class="pun ==<span class="pln  <span class="kwd true<span class="pun )<span class="pln 
            <span class="pun {<span class="pln 
                m_MyListColor <span class="pun =<span class="pln  <span class="typ Color<span class="pun .<span class="typ Red<span class="pun ;<span class="pln 
            <span class="pun }<span class="pln 

            bool selected <span class="pun =<span class="pln  <span class="pun (<span class="pln e<span class="pun .<span class="typ State<span class="pln  <span class="pun &<span class="pln  <span class="typ DrawItemState<span class="pun .<span class="typ Selected<span class="pun )<span class="pln  <span class="pun ==<span class="pln  <span class="typ DrawItemState<span class="pun .<span class="typ Selected<span class="pun ;<span class="pln 

            <span class="kwd if<span class="pln  <span class="pun (<span class="pln m_itemIndexes<span class="pun .<span class="typ Contains<span class="pun (<span class="pln e<span class="pun .<span class="typ Index<span class="pun ))<span class="pln 
            <span class="pun {<span class="pln 
                using <span class="pun (<span class="kwd var<span class="pln  brush <span class="pun =<span class="pln  <span class="kwd new<span class="pln  <span class="typ SolidBrush<span class="pun (<span class="pln m_MyListColor<span class="pun ))<span class="pln 
                <span class="pun {<span class="pln 
                    e<span class="pun .<span class="typ Graphics<span class="pun .<span class="typ FillRectangle<span class="pun (<span class="pln brush<span class="pun ,<span class="pln  e<span class="pun .<span class="typ Bounds<span class="pun );<span class="pln 
                <span class="pun }<span class="pln 
            <span class="pun }<span class="pln 
            <span class="kwd else<span class="pln 
            <span class="pun {<span class="pln 
                e<span class="pun .<span class="typ DrawBackground<span class="pun ();<span class="pln 
            <span class="pun }<span class="pln 

            <span class="kwd string<span class="pln  item <span class="pun =<span class="pln  listBox1<span class="pun .<span class="typ Items<span class="pun [<span class="pln e<span class="pun .<span class="typ Index<span class="pun ].<span class="typ ToString<span class="pun ();<span class="pln 
            e<span class="pun .<span class="typ Graphics<span class="pun .<span class="typ DrawString<span class="pun (<span class="pln item<span class="pun ,<span class="pln  e<span class="pun .<span class="typ Font<span class="pun ,<span class="pln  selected <span class="pun ||<span class="pln  m_itemIndexes<span class="pun .<span class="typ Contains<span class="pun (<span class="pln e<span class="pun .<span class="typ Index<span class="pun )<span class="pln  <span class="pun ?<span class="pln  <span class="typ Brushes<span class="pun .<span class="typ White<span class="pln  <span class="pun :<span class="pln  <span class="typ Brushes<span class="pun .<span class="typ Black<span class="pun ,<span class="pln  e<span class="pun .<span class="typ Bounds<span class="pun ,<span class="pln  <span class="typ StringFormat<span class="pun .<span class="typ GenericDefault<span class="pun );<span class="pln 

            <span class="kwd if<span class="pln  <span class="pun (<span class="pln selected<span class="pun )<span class="pln 
                e<span class="pun .<span class="typ DrawFocusRectangle<span class="pun ();<span class="pln 
        <span class="pun }<span class="pln 

        <span class="kwd private<span class="pln  <span class="kwd void<span class="pln  <span class="typ DrawItem<span class="pun (<span class="typ int<span class="pln  index<span class="pun )<span class="pln 
        <span class="pun {<span class="pln 
            <span class="typ Rectangle<span class="pln  rectItem <span class="pun =<span class="pln  listBox1<span class="pun .<span class="typ GetItemRectangle<span class="pun (<span class="pln index<span class="pun );<span class="pln 
            listBox1<span class="pun .<span class="typ Invalidate<span class="pun (<span class="pln rectItem<span class="pun );<span class="pln 
        <span class="pun }<span class="pln 

        <span class="pun [<span class="typ Browsable<span class="pun (<span class="kwd true<span class="pun )]<span class="pln 
        <span class="kwd public<span class="pln  <span class="typ Color<span class="pln  <span class="typ MyListColor<span class="pln 
        <span class="pun {<span class="pln 
            get <span class="pun {<span class="pln  <span class="kwd return<span class="pln  m_MyListColor<span class="pun ;<span class="pln  <span class="pun }<span class="pln 
            <span class="typ set<span class="pln 
            <span class="pun {<span class="pln 
                m_MyListColor <span class="pun =<span class="pln  value<span class="pun ;<span class="pln 
                <span class="typ Refresh<span class="pun ();<span class="pln 
            <span class="pun }<span class="pln 
        <span class="pun }<span class="pln 

        <span class="pun [<span class="typ Browsable<span class="pun (<span class="kwd true<span class="pun )]<span class="pln 
        <span class="kwd public<span class="pln  <span class="typ ListBox<span class="pln  <span class="typ MyListBox<span class="pln 
        <span class="pun {<span class="pln 
            get <span class="pun {<span class="pln  <span class="kwd return<span class="pln  listBox1<span class="pun ;<span class="pln  <span class="pun }<span class="pln 
            <span class="typ set<span class="pln 
            <span class="pun {<span class="pln 
                listBox1 <span class="pun =<span class="pln  value<span class="pun ;<span class="pln 
                <span class="typ Refresh<span class="pun ();<span class="pln 
            <span class="pun }<span class="pln 
        <span class="pun }<span class="pln 

        <span class="kwd private<span class="pln  <span class="kwd void<span class="pln  <span class="typ ListBoxControl_Load<span class="pun (<span class="kwd object<span class="pln  sender<span class="pun ,<span class="pln  <span class="typ EventArgs<span class="pln  e<span class="pun )<span class="pln 
        <span class="pun {<span class="pln 
            <span class="kwd this<span class="pun .<span class="pln listBox1<span class="pun .<span class="typ SelectedIndex<span class="pln  <span class="pun =<span class="pln  <span class="lit 0<span class="pun ;<span class="pln 
        <span class="pun }<span class="pln 
    <span class="pun }<span class="pln 

    <span class="kwd public<span class="pln  <span class="kwd class<span class="pln  <span class="typ ItemEventArgs<span class="pln  <span class="pun :<span class="pln  <span class="typ EventArgs<span class="pln 
    <span class="pun {<span class="pln 
        <span class="kwd public<span class="pln  <span class="typ int<span class="pln  <span class="typ Index<span class="pln  <span class="pun {<span class="pln  get<span class="pun ;<span class="pln  <span class="typ set<span class="pun ;<span class="pln  <span class="pun }<span class="pln 
        <span class="kwd public<span class="pln  <span class="kwd string<span class="pln  <span class="typ Name<span class="pln  <span class="pun {<span class="pln  get<span class="pun ;<span class="pln  <span class="typ set<span class="pun ;<span class="pln  <span class="pun }<span class="pln 
    <span class="pun }<span class="pln 
<span class="pun }
[/code]
I want to make something that will indicate if there is any item with its original color or not. If there is no so it means the selected items are in Red do something. If there is one item with its original color do something else.
<
danieli

View the full article
 
Back
Top