locked
ListBox ClearSelected and SelectedIndex = -1 not working? RRS feed

  • Question

  • Hi all,

    I have been working on a complex gui for some time and have just come across this issue...
    My code, inside the ListBox's MouseUp event is as follows:


     

    Code Block

    void lb_MouseUp(object sender, MouseEventArgs e)

    {

    ListBox lb = sender as ListBox;

    if (lb != null)

    {

    KeyValuePair<string, object> kvp = (KeyValuePair<string, object>)

    lb.Items[lb.IndexFromPoint(e.X, e.Y)];

    StaticFavSet sfs = kvp.Value as StaticFavSet;

    if (sfs != null)

    {

    //lb.ClearSelected(); - Doesn't work - SelectedIndex is 0 after this call...

    lb.SelectedIndex = -1;

    foreach (KeyValuePair<string, object> objs in sfs.KeyedValues)

    {

    lb.SetSelected(lb.FindStringExact(objs.Key), true);

    }

    }

    }

    }

     

    Basically if the user clicks on a particular type of listbox item, I want the listbox to select all the keys stored in that value...

    The issue is just before the foreach loop - both ClearSelected and SelectedIndex = -1 result in the listbox's selected index resulting in 0, and leaves me with the topmost element always selected regardless if it is in the list or not...

    The ListBox is in SelectionMode.MultiExtended mode...

     

    Please help?

     

    The funny thing is, if the SelectedIndex is 0 to begin with, the action ClearSelected/SelectedIndex = -1 actually works, when SelectedIndex is 1 to begin with, it doesnt.  Could this have something to do with the BindingContext?

     

    A work around is to include a lb.SetSelected(0, false) call between the ClearSelected() and foreach loop but why does this occur? I can't believe it is by design?

    Friday, January 4, 2008 12:36 PM

Answers

  •  

    Hi floydy,

    I cannot reproduce your problem with the following code. Could you please provide more detailed information about your listbox?

    Code Block

        public partial class Form4 : Form

        {

            public Form4()

            {

                InitializeComponent();

            }

     

            private void Form4_Load(object sender, EventArgs e)

            {

                this.listBox1.SelectionMode = SelectionMode.MultiExtended;

                DataTable dt = new DataTable();

                dt.Columns.Add("cola");

                dt.Columns.Add("colb");

                for (int i = 0; i < 20; i++)

                {

                    dt.Rows.Add("aa" + i, "bb" + i%4);

                }

                dt.AcceptChanges();

                this.listBox1.DisplayMember = "cola";

                this.listBox1.ValueMember = "colb";

                this.listBox1.DataSource = dt;

            }

     

            private void listBox1_MouseUp(object sender, MouseEventArgs e)

            {

                System.Windows.Forms.ListBox lb = sender as System.Windows.Forms.ListBox;

                if (lb != null && lb.IndexFromPoint(e.X, e.Y) != -1)

                {

                    DataRowView drv = lb.Items[lb.IndexFromPoint(e.X, e.Y)] as DataRowView;

     

                    string value = drv["colb"].ToString();

                    int count = lb.Items.Count;

                    lb.SelectedIndex = -1;

                    for (int i = 0; i < count; i++)

                    {

                        DataRowView drv1 = lb.Items[i] as DataRowView;

                        if (drv1["colb"].ToString() == value)

                            this.listBox1.SetSelected(i, true);

                    }

                }

            }

        }

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Wednesday, January 9, 2008 9:28 AM

All replies

  • That is by design. Selection List and Combo-Box behave that way. Once an item is selected, the control can no longer be cleared. 

     

    2 Options:

     

    1. You either have to put a dummy entry, ex "Select an Item", to designate "nothing selected".

    2. Clear the collection of the control then re-assign the values back, but don't assign any default value. The control should reset to "untouched" state.

    Friday, January 4, 2008 5:33 PM
  • How do you explain the different behaviour between the preconditions SelectedIndex = 0 and SelectedIndex = 1 then?

    SelectedIndex = 0 means the top most element is highlighted, SelectedIndex = 1 means the second top most element is highlighted - it makes no sense that one of these (the first precondition, = 0, in this case) conditions work, and one does not.

     

    Bare in mind that MultiExtended allows users to de-select items by holding CTRL and clicking selected items.

    Friday, January 4, 2008 8:14 PM
  •  

    Hi floydy,

    I cannot reproduce your problem with the following code. Could you please provide more detailed information about your listbox?

    Code Block

        public partial class Form4 : Form

        {

            public Form4()

            {

                InitializeComponent();

            }

     

            private void Form4_Load(object sender, EventArgs e)

            {

                this.listBox1.SelectionMode = SelectionMode.MultiExtended;

                DataTable dt = new DataTable();

                dt.Columns.Add("cola");

                dt.Columns.Add("colb");

                for (int i = 0; i < 20; i++)

                {

                    dt.Rows.Add("aa" + i, "bb" + i%4);

                }

                dt.AcceptChanges();

                this.listBox1.DisplayMember = "cola";

                this.listBox1.ValueMember = "colb";

                this.listBox1.DataSource = dt;

            }

     

            private void listBox1_MouseUp(object sender, MouseEventArgs e)

            {

                System.Windows.Forms.ListBox lb = sender as System.Windows.Forms.ListBox;

                if (lb != null && lb.IndexFromPoint(e.X, e.Y) != -1)

                {

                    DataRowView drv = lb.Items[lb.IndexFromPoint(e.X, e.Y)] as DataRowView;

     

                    string value = drv["colb"].ToString();

                    int count = lb.Items.Count;

                    lb.SelectedIndex = -1;

                    for (int i = 0; i < count; i++)

                    {

                        DataRowView drv1 = lb.Items[i] as DataRowView;

                        if (drv1["colb"].ToString() == value)

                            this.listBox1.SetSelected(i, true);

                    }

                }

            }

        }

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Wednesday, January 9, 2008 9:28 AM
  • Set the Listbox property Listbox.FormattingEnabled = true Or Set .SelectedValue

    Form the Listbox Control FormattingEnabled page:

    Setting the FormattingEnabled property to true causes the Format event to be raised for each visible member of the  ListControl, SelectedIndex, SelectedValue and FormattingEnabled are related as follows:

    • If FormattingEnabled is false, SelectedIndexwill not be set to -1 when SelectedValue is blank.

    • If FormattingEnabled is true, SelectedIndexwill be set to -1 when SelectedValue is blank.

    Friday, May 18, 2018 3:53 PM