locked
ListBox SelectedIndex changing ? RRS feed

  • Question

  • Hi,

    I used System.Windows.Forms.ListBox control and I know event SelectedIndexChanged, but I need to know before selected index about to change so I can turn off other selected item in another control. So I need to make sure other item in other control to unselected before my listbox set its selected index.

    Is there a tricky way to do that ?


    It's hard to be advanced programmer

    Thursday, November 15, 2012 10:51 AM

Answers

  • You could use the MouseClick event which is triggered before the SelectedIndexChanged event.

    Something like:

           private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                //your code
            }
    
            private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
            {
                //your code
            }
    
            private void listBox1_MouseClick(object sender, MouseEventArgs e)
            {
                listBox2.SelectedItem = null;
            }      
    
            private void listBox2_MouseClick(object sender, MouseEventArgs e)
            {
                listBox1.SelectedItem = null;
            }   

    • Proposed as answer by Bob Wu-MT Friday, November 16, 2012 7:39 AM
    • Marked as answer by Bob Wu-MT Tuesday, December 4, 2012 10:01 AM
    Thursday, November 15, 2012 11:10 AM
  • you can also try ListBox1_Click event...

    it will be hit before selectedIndexChanged event.


    Hirendra Sisodiya from authorcode.com

    • Marked as answer by Bob Wu-MT Tuesday, December 4, 2012 10:01 AM
    Thursday, November 15, 2012 11:15 AM

All replies

  • You could use the MouseClick event which is triggered before the SelectedIndexChanged event.

    Something like:

           private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                //your code
            }
    
            private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
            {
                //your code
            }
    
            private void listBox1_MouseClick(object sender, MouseEventArgs e)
            {
                listBox2.SelectedItem = null;
            }      
    
            private void listBox2_MouseClick(object sender, MouseEventArgs e)
            {
                listBox1.SelectedItem = null;
            }   

    • Proposed as answer by Bob Wu-MT Friday, November 16, 2012 7:39 AM
    • Marked as answer by Bob Wu-MT Tuesday, December 4, 2012 10:01 AM
    Thursday, November 15, 2012 11:10 AM
  • you can also try ListBox1_Click event...

    it will be hit before selectedIndexChanged event.


    Hirendra Sisodiya from authorcode.com

    • Marked as answer by Bob Wu-MT Tuesday, December 4, 2012 10:01 AM
    Thursday, November 15, 2012 11:15 AM