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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:41:49+00:00 2026-05-26T19:41:49+00:00

Winforms’ ListBox appears to have a strange behavior. When I set the SelectionMode to

  • 0

Winforms’ ListBox appears to have a strange behavior. When I set the SelectionMode to one, I expect that I can click an item, and it becomes selected. This holds true, but if I click an item, drag up and down the list, the selection changes.

Now, that wouldn’t be too much of a big deal except that I need to perform drag and drop between some controls. So, when they select an item, and drag it down the list, a newly-selected item is actually the one it registers as dragging, and the wrong item gets sent over.

So, I then further bandage it by saving a reference to the selected item on mousedown, but it winds up as a bad user experience. My users drag an item to the other listbox, which does work, but the original listbox doesn’t have the “correct” item selected anymore, and they become confused as to which item was actually dropped on the second control.

So, is there any way to change this sort of behavior? I want an item to be selected at MouseDown, ignoring the MouseUp part of things. Simply consuming the event doesn’t seem to be enough, and I’d rather not have to override ListBox (we have to write documents for any new classes being created).

  • 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-26T19:41:50+00:00Added an answer on May 26, 2026 at 7:41 pm

    I guess if you call DoDragDrop this behavior would disappear. Windows doesn’t dispatch MouseOver messages while in drag&drop mode.

    Example of propper drag&drop:

        private void listBox_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
        {
            // Get the index of the item the mouse is below.
            indexOfItemUnderMouseToDrag = listBox.IndexFromPoint(e.X, e.Y);
    
            if (indexOfItemUnderMouseToDrag != ListBox.NoMatches) {
    
                // Remember the point where the mouse down occurred. The DragSize indicates
                // the size that the mouse can move before a drag event should be started.                
                Size dragSize = SystemInformation.DragSize;
    
                // Create a rectangle using the DragSize, with the mouse position being
                // at the center of the rectangle.
                dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width /2),
                                                               e.Y - (dragSize.Height /2)), dragSize);
            } else
                // Reset the rectangle if the mouse is not over an item in the ListBox.
                dragBoxFromMouseDown = Rectangle.Empty;
    
        }
    
        private void listBox_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
            // Reset the drag rectangle when the mouse button is raised.
            dragBoxFromMouseDown = Rectangle.Empty;
        }
    
        private void listBox_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) 
        {
            if ((e.Button & MouseButtons.Left) == MouseButtons.Left) {
    
                // If the mouse moves outside the rectangle, start the drag.
                if (dragBoxFromMouseDown != Rectangle.Empty && 
                    !dragBoxFromMouseDown.Contains(e.X, e.Y)) 
                {
                        DragDropEffects dropEffect = listBox.DoDragDrop(listBox.Items[indexOfItemUnderMouseToDrag], DragDropEffects.All | DragDropEffects.Link);
    
                        // If the drag operation was a move then remove the item.
                        if (dropEffect == DragDropEffects.Move) {                        
                            listBox.Items.RemoveAt(indexOfItemUnderMouseToDrag);
    
                            // Selects the previous item in the list as long as the list has an item.
                            if (indexOfItemUnderMouseToDrag > 0)
                                listBox.SelectedIndex = indexOfItemUnderMouseToDrag -1;
    
                            else if (ListDragSource.Items.Count > 0)
                                // Selects the first item.
                                listBox.SelectedIndex =0;
                        }
                    }
                }
            }
        }
    

    …and SelectedIndexChanged still works!

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

Sidebar

Related Questions

Winforms-How can I make dialog boxes appear centered on MainForm? That is as opposed
In WinForms, I can design a form and have a TabValue to control which
In Winforms you can say if ( DesignMode ) { // Do something that
WinForms have those three boxes in the upper right hand corner that minimize, maximize,
We have a WinForms control that is an extended version of ComboBox that supports
WinForms .NET 3.5 We have a contextMenuStrip for data grid. This contextMenuStrip can hold
Winforms .net 3.5 app. In my app I have a generic class that looks
WinForms, .NET 3.5 I have a ComboBox that needs to display a growing list
In WinForms you can set the bounds of a control using SetBounds , or
In WinForms, how can I create a UserControl that when I put on my

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.