UI controls

is there a way to get listbox.contains(object) using the listbox control
I went into the sampleFramework and looked at the code and created a bool function called contains but cant get it to work right. I noticed itemList is a arraylist so under that function i used itemList.Contans(myvarible) it does not seen to spot it.

also I am trying to remove string objects from the listbox at runtime but there is really no way to get the index value that I can see at runtime, is there someone willing to help me with these two things
thanks a bunch


Answer this question

UI controls

  • SweptSquash

    I added this to the sampleFramework in the listbox control under the

    Item Controlling region


    public void Remove(string s)

    {

    for(int i = itemList.Count - 1; i >= 0; i--)

    {

    ListBoxItem li = (ListBoxItem)itemList['i'];

    if (li.ItemText == s.ToString())

    {

    itemList.RemoveAt(i);

    scrollbarControl.SetTrackRange(0, itemList.Count);

    if (selectedIndex >= itemList.Count)

    {

    selectedIndex = itemList.Count - 1;

    }

    RaiseSelectionEvent(this, true);

    return;

    }

    }

    }


     



    this works just like the listbox control using the windows UI


  • tixall

    that works great!!!

    so I went ahead and created a remove(string) in there also but I keep geting this error

    System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.Collection.ArrayListEnumeratorSimple.MoveNext()


    here is the code I did


    public void Remove(string s)

    {

    int i = -1;

    foreach (ListBoxItem li in itemList)<---the error is thrown here

    {

    i = i + 1;

    if (li.ItemText == s.ToString())

    {

    itemList.RemoveAt(i);

     

    scrollbarControl.SetTrackRange(0, itemList.Count);

    if (selectedIndex >= itemList.Count)

    selectedIndex = itemList.Count - 1;

    RaiseSelectionEvent(this, true);

    }

    }

    }


     

  • wfillis

    #1 - no there isn't such a function. You are correct that its an array list but its an array listof ListBoxItems so you've got to loop through to look for the item.


            /// <summary>
            /// Searches the listbox for a particular string
            /// </summary>
            /// <returns>True if the list box contains the an item that matches the string passed in, False otherwise</returns>
            public bool Contains(string text)
            {
                foreach (ListBoxItem li in itemList)
                {
                    if (li.ItemText == text)
                    {
                        return true;
                    }
                }
                return false; //not found
            }

     

    #2 How do you want to get the index You can get the index of the selected item and then call RemoveAt(). If you want to remove others you should be able to modify the Contains() function above to return an index.


    Hope that helps.



  • venkatesh24837

    If I understand right this code, you should break the loop after you removed item ( put break after RaiseSelectionEvent(this, true); ), since collction was modified and cannot enumerate!!!

  • UI controls