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

  • Home
  • SEARCH
  • 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 5998755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:27:04+00:00 2026-05-23T00:27:04+00:00

I have a databound ListBox that is behaving strangely. The ListBox’s SelectionMode property is

  • 0

I have a databound ListBox that is behaving strangely. The ListBox’s SelectionMode property is set to MultiExtended, and on a button click, I need to copy the items to another control, in this case, a TreeView. However, for some reason, every iterator I’ve tried only loops once. I’ve attempted both SelectedItems and SelectedIndices. Code excerpt:

var movedItems = new List<ListBoxUnderlyingObject>();
foreach (var selectedItem in listBox.SelectedItems)
{
    var castItem = selectedItem as ListBoxUnderlyingObject;
    var newNode = new TreeNode(castItem.SomeString);
    newNode.Name = castItem.AnotherString;
    newNode.Tag = castItem;
    newNode.ForeColor = Color.RoyalBlue;
    //parentNode was set earlier
    parentNode.Nodes.Add(newNode);
    movedItems.Add(selectedItem);
}
//use movedItems to remove items from listBox's underlying databound object and rebind

No matter how many items are selected, the loop only executes once. Same with SelectedIndices. If I attempt it with a numbered iterator, it fails with an “index out of bounds of array” error.

for(var i = 0;i < listBox.SelectedItems.Count;i++)
{
    var castItem = listBox.SelectedItems[i] as ListBoxUnderlyingObject;
    //etc., the previous line bombs on the second iteration
}

If I throw a Debug.WriteLine(listBox.SelectedItems.Count) either before or during the loop, it always reflects the correct count. I know this is probably something stupid, but I’m stumped. Help!

Follow Up

I’ve created a separate winforms project that emulates the behavior almost exactly, and SelectedItems works. I am completely baffled. Now, I’m going to try and add a new form in the original project and see if I can recreate the behavior there.

  • 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-23T00:27:05+00:00Added an answer on May 23, 2026 at 12:27 am

    Well, it turns out I did withhold some critical information. The listbox has drag-drop behavior enabled, and part of this is a handler for the MouseDown event. The handler has this code in it:

    private void listBox_MouseDown(object sender, MouseEventArgs e)
    {
        if (listBox.Items.Count = 0) return;
        listBox.DoDragDrop(listBox.SelectedItem, DragDropEffects.Move);
    }
    

    If I comment this handler out, SelectedItems behaves correctly. Now I have to figure out how to do the drag-drop stuff correctly, but that’s a different question.

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

Sidebar

Related Questions

I have a listbox that is databound to a Collection of objects. The listbox
I have the following construct, which shows a DataBound ListBox and a Button inside
I have a databound and itemtemplated ListBox: <ListBox x:Name=lbLista ScrollViewer.VerticalScrollBarVisibility=Visible> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation=Horizontal>
I have a databound list view and can set all the column widths manually
I have a ListView in WPF that is databound to a basic table that
I have an XML that needs to be databound to a WPF TreeView .
So this is pretty mystifying. I have a databound ListBox (or ListView , it
I have a listbox that is bound to a List<T> -- this is working
I have this method Verify_X which is called during databind for a listbox selected
I have a databound TextBox in my application like so: (The type of Height

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.