Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6013169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:30:36+00:00 2026-05-23T02:30:36+00:00

Here is the problem: I have a Windows Forms application that I’m developing, and

  • 0

Here is the problem: I have a Windows Forms application that I’m developing, and in one segment I’m using a ListView control.

What I’m trying can be simply stated as: on event ListViewItemSelectionChange show a MessageBox for user to confirm the change, if not confirmed change to let’s say the first item. This change to the first item would again fire ListViewItemSelecionChange, so I unregister and re-register the event handler method, so everything should be good, right?

What actually happens is that the handler method is called twice (actually ListView should fire two events on Selection change, one for deselect, other for newly selected item, but I have an e.IsSelected statement at the beginning to catch only selected items, so actually you could say that there are four events fired).

The problem is, if I generated the first event with mouse click on ListView item, and I’ve unsubscribed before programatically changing to the first item, what generates the second event firing? Is it some focus change because of the MessageBox call? Is there any way to prevent the second event to fire?

I have a simple example solution here, it can’t be more simlified (25 SLOC), so if you can, please take a look. Note that commenting the line “if (ShowMessageBox())” stops the second event from firing, is this some focus change problem?

http://www.filedropper.com/listviewtestwithmsgbox

Edit: the relevant code:

private void listViewWithSelection1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
    // listview actually generates two ItemSelectionChanged events,
    // one for deselect of a item, and another event for a newly selected item (which we want here).
    if (e.IsSelected)
    {
        if (ShowMessageBox())
            Button1_Click(null, EventArgs.Empty);

        label1.Text += "item selected   ";
    }
}

private bool ShowMessageBox()
{
    return MessageBox.Show("Change to first item instead?", "test", MessageBoxButtons.YesNo) == DialogResult.Yes;
}

private void Button1_Click(object sender, EventArgs e)
{
    // change ti first ListView item
    listView1.ItemSelectionChanged -= listViewWithSelection1_ItemSelectionChanged;
    listView1.Items[0].Selected = true;
    listView1.ItemSelectionChanged += listViewWithSelection1_ItemSelectionChanged;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:30:36+00:00Added an answer on May 23, 2026 at 2:30 am

    Hmm, can you describe how the selection is being changed to begin with? If it’s by the user clicking to select an item, perhaps catch the Click or DoubleClick event rather than the ItemSelectionChanged event? I have this snippet I’m using on a program currently. If the user double clicks the list box (listView, in your case), do something with the selected item.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private bool ShowMessageBox()
        {
            return MessageBox.Show("Change to first item instead?", "test", MessageBoxButtons.YesNo) == DialogResult.Yes;
        }
    
        private void listView1_Click(object sender, EventArgs e)
        {
            if (ShowMessageBox())
                listView1.TopItem.Selected = true;
                label1.Text += "item selected   ";
        }
    }
    

    Edited to include relevant code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the problem. i am developing a windows forms application in c# that
I have a keyboard focus problem with Windows Forms application that hosts WPF UserControls.
I have got a problem. I have windows forms application with dynamic generated layout,
I have a WIndows Forms Application. I needed a DataGridView so i added one.
I'm trying to use the GMap.NET.WindowsForms control in my Windows forms application but I
I'm developing an Windows Forms application that requires me to call a separate program
I have a problem in a Windows Forms application with Bitmap.Save failing when I
I have a problem concerning delegates in a Windows Forms application. There are two
I have a Windows Form application that involves two Forms. The child form is
I'm battling a really weird problem here. I have a Windows 2008 R2 server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.