locked
unable to catch from where ListView.SelectedIndexChanged event is being fired on a click on PictureBox control RRS feed

  • Question

  • Multi item select is supported in Smart Device Application ListView Control, so I am trying to simulate Multi item select in ListViewControl.
    In my code below, I am throwing an exception on certion condition as well as re-setting the last selected ListViewItem as Selected again to not let the new selected ListViewItem as Selected.

    After this I click on a PictureBox control for some action but very strange that ListView.SelectedIndexChanaged property is called automatically and making my code logic fail because I am toggling the BackColor of ListViewItem. There is no code on PictureBox.Click event. I tried much to know from where and how it is firing but fail to detect.
    Best regards.

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
            {
                short index = -1;
                ListViewItem selectedItem;

                try
                {
                    if (list.SelectedIndices.Count == 0)
                        return;

                    index = (short)list.SelectedIndices[0];
                    selectedItem = list.Items[index];

                    if (this.multiMails)
                    {
                        if (this.multiSelectCount > 0 /* &&  Some other condition*/)
                        {
                            try
                            {
                                list.SelectedIndexChanged -= new EventHandler(list_SelectedIndexChanged);
                                list.Items[this.lastSelectedIndex].Selected = true;
                            }
                            finally
                            {
                                list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
                            }

                            throw new Exception("Message");
                        }

                        if (list.Items[index].BackColor == Color.White)
                        {
                            list.Items[index].BackColor = Color.DarkBlue;
                            this.multiSelectCount++;
                        }

                        else if (list.Items[index].BackColor == Color.DarkBlue)
                        {
                            list.Items[index].BackColor = Color.White;
                            this.multiSelectCount--;
                        }
                    }

                    this.lastSelectedIndex = index;
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.Message);
                }
                finally
                {
                    list.Refresh();
                    Application.DoEvents();
                }
            }

    Sunday, December 27, 2009 6:28 AM

All replies

  • Hi

    Try looking at the call stack in your selectedindexchanged eventhandler. It should tell you what fired this event.

    Ray
    Sunday, December 27, 2009 7:01 AM
  • Thanks dear Raymond.
    I use a breakpoint inside list_SelectedIndexChanged event handler. First I played to throw exception on certion condition as mention above in my code. Then I just pressed PictureBox control. There is no code inside PictureBox.Click event handler but still list_SelectedIndexChanged event handler is being called. When breakpoint reach I press Ctl+Alt+C but the call stack is not showing the full calling path and not meaningful/helpful as below:

    Wasel.exe!Wasel.MailTracker.main.list_SelectedIndexChanged(object sender = {System.Windows.Forms.ListView}, System.EventArgs e = {System.EventArgs}) Line 2899 C#
    System.Windows.Forms.dll!System.Windows.Forms.ListView.OnSelectedIndexChanged(System.EventArgs e = {System.EventArgs}) + 0x15 bytes 
    System.Windows.Forms.dll!System.Windows.Forms.ListView.UpdateSelectedIndex(int iItem = 1) + 0x18 bytes 
    System.Windows.Forms.dll!System.Windows.Forms.ListView.WnProc(Microsoft.AGL.Forms.WM wm = WM_LISTVIEW_ITEM_CHANGED, int wParam = 0, int lParam = 1) + 0x66 bytes 
    System.Windows.Forms.dll!System.Windows.Forms.Control._InternalWnProc(Microsoft.AGL.Forms.WM wm = WM_LISTVIEW_ITEM_CHANGED, int wParam = 0, int lParam = 1) + 0x9 bytes 
    System.Windows.Forms.dll!Microsoft.AGL.Forms.EVL.EnterModalDialog(System.IntPtr hwnModal = 5439529) 
    System.Windows.Forms.dll!System.Windows.Forms.Form.ShowDialog() + 0x60 bytes 
    Wasel.exe!Wasel.Login.btnLogIn_Click(object sender = {System.Windows.Forms.PictureBox}, System.EventArgs e = {System.EventArgs}) Line 342 + 0x7 bytes C#

    Sunday, December 27, 2009 8:37 AM
  • Hi

    This is the call stack I would expect from actually clicking on the listview control. The item is not selected from your code as far as I can tell.
    I did a quick test but couldn't reproduce this problem.

    Will the SelectedIndexChanged event fire when you click on any other control, or the form itself?

    Ray
    Sunday, December 27, 2009 5:28 PM
  • Yes Ray, the SelectedIndexChanged event is fired clicking on any other control  e.g. Button, PictureBox even Form.
    This event is fired only once, when I click again on any other control this event is not fired.
    I observe this behavior in two applicatoins, therefore I think that this problem is not from my code, may by there is some behavior of ListView control that I don't know.

    Thanks & Regards.
    Monday, December 28, 2009 6:24 AM