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 8004917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:04:02+00:00 2026-06-04T17:04:02+00:00

I have a tough time trying to solve this problem. I have been at

  • 0

I have a tough time trying to solve this problem. I have been at for 3 hours, and still I couldn’t find out why it is doing this. Here is the code:

private void Catagory_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int selectedCategoryId = categoryIdList[categoryListBox.SelectedIndex];

        client.GetItemsAsync(selectedCategoryId);
        client.GetItemsCompleted += 
            new EventHandler<GetItemsCompletedEventArgs>(client_GetItemsCompleted);
    }

void client_GetItemsCompleted(object sender, GetItemsCompletedEventArgs e)
{
        itemIdList.Clear();
        itemNameList.Clear();
        itemNumberList.Clear();
        itemDisplayList.Clear(); //Clears the Display List Items

        if (e.Error == null)
        {
            itemIdList = e.ItemIDList;
            itemNumberList = e.itemNumber;
            itemNameList = e.Result;

            for (int i = 0; i < itemIdList.Count; i++)
            {
                itemDisplayList.Add(new ItemDisplay { itemNumber = itemNumberList[i], itemName = itemNameList[i] });
            }

            //Populating the listbox controll with the itemDisplaylist...
            Items.ItemsSource = itemDisplayList;
        }
        else
        {
            MessageBox.Show("Problem in getting the items list.");
        }
    }

When I change the category the first time it works perfectly. By perfectly, I mean that it calls the function GetItemsAsync(selectedCategoryId) and grabs the results and calls the event handler client_GetItemsCompleted() and the inner working of the event handler works as it is supposed to, it sets the lists with the proper data and displays the itemNumber and the itemName in the list box . But, when I change the category again to get different items, it doesn’t work properly, what it’s doing is that it clears the lists and populates the lists as it is supposed to, runs the for loop and populates the listBox called Items but for some reason it goes to the top of the function again and empties all the lists. Please tell my why it is executing the function again? And when I choose another category again, it executes the event handler 3 times and then 4 times and so on. Wnow why it is doing this?

  • 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-06-04T17:04:03+00:00Added an answer on June 4, 2026 at 5:04 pm

    Everytime this is executed:

      client.GetItemsCompleted += 
    

    You add a subscriber to the event, so the second time it will fire twice (the third time triple times, etc..).

    Either unsubscrice ( -= ) in the completed method:

    void client_GetItemsCompleted(object sender, GetItemsCompletedEventArgs e)
    {
        try {
           /* .... */
        }
        finally {
            client.GetItemsCompleted -= 
                new EventHandler<GetItemsCompletedEventArgs>(client_GetItemsCompleted);
        }
    }
    

    or initiate the client object before every call.

    var client = new ...();
    client.GetItemsAsync(selectedCategoryId);
    client.GetItemsCompleted += 
                new EventHandler<GetItemsCompletedEventArgs>(client_GetItemsCompleted);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tough time making this design decision. I could go with traditional new
I have now been stuck at this for some time so I though to
I'm having a tough time figuring out a tricky issue. Here is what I
I am having a tough time with this design problem and would appreciate any
I am having a tough time trying to find a solution to the following
I have been going round and round trying to find a definitive answer. Basically,
I have a really tough situation in here. There is an array of these
I'm having a tough time trying to implement a slideToggle event and at the
Its been a long time since I have programmed in C++, but I know
I'm having a tough time wrapping my head around this one. Hovering a navbar

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.