Combo Box SwapItem
-
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
-
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
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 )
-
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 )
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
-
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
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] }