Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Combo Box SwapItem

Combo Box SwapItem

Scheduled Pinned Locked Moved C#
databasehelpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PhilDanger
    wrote on last edited by
    #1

    Hi everyone, I'm trying to write a simple swap function for two items in a combo box (this is used for moving an item in a combo box up or down depending on the button the user clicks). My problem is that when I set ComboBox.DroppedDown = true, when the user clicks the button again while it is dropped down, (or anywhere else besides the combo box) it switches back to selecting the index that was initially selected. This is a problem when they want to move the same item more than one position -- it doesn't follow the user's selection.

            /// 
            /// Swaps the names of the frames in the combo box.
            /// 
            private void SwapFrameNames(int oldIndex, int newIndex)
            {
                cmbFrames.DroppedDown = true; //drop the combo box down so the user can see what's going on
                
                //perform the swap
                object temp = cmbFrames.Items[newIndex];
                cmbFrames.Items[newIndex] = cmbFrames.Items[oldIndex];
                cmbFrames.Items[oldIndex] = temp;
    
                //select the new position so we follow the user's selection
                cmbFrames.SelectedIndex = newIndex;
            }
    

    The code works fine if I comment out the first line of the function -- it follows the selection as expected. Any ideas? Thanks, Phil

    C L 2 Replies Last reply
    0
    • P PhilDanger

      Hi everyone, I'm trying to write a simple swap function for two items in a combo box (this is used for moving an item in a combo box up or down depending on the button the user clicks). My problem is that when I set ComboBox.DroppedDown = true, when the user clicks the button again while it is dropped down, (or anywhere else besides the combo box) it switches back to selecting the index that was initially selected. This is a problem when they want to move the same item more than one position -- it doesn't follow the user's selection.

              /// 
              /// Swaps the names of the frames in the combo box.
              /// 
              private void SwapFrameNames(int oldIndex, int newIndex)
              {
                  cmbFrames.DroppedDown = true; //drop the combo box down so the user can see what's going on
                  
                  //perform the swap
                  object temp = cmbFrames.Items[newIndex];
                  cmbFrames.Items[newIndex] = cmbFrames.Items[oldIndex];
                  cmbFrames.Items[oldIndex] = temp;
      
                  //select the new position so we follow the user's selection
                  cmbFrames.SelectedIndex = newIndex;
              }
      

      The code works fine if I comment out the first line of the function -- it follows the selection as expected. Any ideas? Thanks, Phil

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Not really a fix, but the swap is going to take a split second to occur, right ? So why not drop it AFTER doing the swap, if having it open stops the swap from working ?

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      P 1 Reply Last reply
      0
      • C Christian Graus

        Not really a fix, but the swap is going to take a split second to occur, right ? So why not drop it AFTER doing the swap, if having it open stops the swap from working ?

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        P Offline
        P Offline
        PhilDanger
        wrote on last edited by
        #3

        Hi Christian, Thanks for your suggestion, doing as you said fixed the problem of it not following the user's selection, but it uncovered what was part of the root problem: when the ComboBox is open, and the user clicks anywhere that isn't part of the dropdown/box, it consumes the mouse event(s) so that the button that is clicked on is not activated -- this means the user has to click twice to continue moving the item down the list. So I guess this is a new question now: is there any way to pass the mouse events on to the rest of the form after the dropdown closes itself? Thanks again, Phil

        1 Reply Last reply
        0
        • P PhilDanger

          Hi everyone, I'm trying to write a simple swap function for two items in a combo box (this is used for moving an item in a combo box up or down depending on the button the user clicks). My problem is that when I set ComboBox.DroppedDown = true, when the user clicks the button again while it is dropped down, (or anywhere else besides the combo box) it switches back to selecting the index that was initially selected. This is a problem when they want to move the same item more than one position -- it doesn't follow the user's selection.

                  /// 
                  /// Swaps the names of the frames in the combo box.
                  /// 
                  private void SwapFrameNames(int oldIndex, int newIndex)
                  {
                      cmbFrames.DroppedDown = true; //drop the combo box down so the user can see what's going on
                      
                      //perform the swap
                      object temp = cmbFrames.Items[newIndex];
                      cmbFrames.Items[newIndex] = cmbFrames.Items[oldIndex];
                      cmbFrames.Items[oldIndex] = temp;
          
                      //select the new position so we follow the user's selection
                      cmbFrames.SelectedIndex = newIndex;
                  }
          

          The code works fine if I comment out the first line of the function -- it follows the selection as expected. Any ideas? Thanks, Phil

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi Phil, two ideas: 1. the DroppedDown property manipulation should not be there at all; it is not needed to swap frame names; if your app needs it, put it elsewhere (or modify your method name) 2. you might want to use SelectedItem instead of SelectedIndex; this should even work if the selected index is different from oldIndex/newIndex; it will fail if your ComboBox holds duplicates (not a good idea anyhow)

          private static void SwapFrameNames(ComboBox cb, int oldIndex, int newIndex) {
          // remember current selection
          object sel=cb.SelectedItem;
          // unselect everything
          cb.SelectedItem=null;
          // swap
          object temp = cb.Items[newIndex];
          cb.Items[newIndex] = cb.Items[oldIndex];
          cb.Items[oldIndex] = temp;
          // restore selection if any (was null, so must search index now)
          cb.SelectedItem=sel;
          }

          Hope this helps. :)

          Luc Pattyn


          try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups