[唐胡璐]VS技巧 - 两个ListBox中的项左右移动
实现如下图所示的功能:
代码如下所示:
/// <summary> /// Move one or more items from left to right /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MoveToRight_Click(object sender, EventArgs e) { int ItemCount = AllTestCase.SelectedItems.Count; if (ItemCount > 0) { for (int i = 0; i < ItemCount; i++) { SelectedTestCase.Items.Add(AllTestCase.SelectedItem); AllTestCase.Items.Remove(AllTestCase.SelectedItem); } } else { MessageBox.Show("Please select one or more items."); } } /// <summary> /// Move all items from left to right /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MoveAllToRight_Click(object sender, EventArgs e) { SelectedTestCase.Items.AddRange(AllTestCase.Items); AllTestCase.Items.Clear(); } /// <summary> /// Move one or more items from right to left /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MoveToLeft_Click(object sender, EventArgs e) { int ItemCount = SelectedTestCase.SelectedItems.Count; if (ItemCount > 0) { for (int i = 0; i < ItemCount; i++) { AllTestCase.Items.Add(SelectedTestCase.SelectedItem); SelectedTestCase.Items.Remove(SelectedTestCase.SelectedItem); } } else { MessageBox.Show("Please select one or more items."); } } /// <summary> /// Move all items from right to left /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MoveAllToLeft_Click(object sender, EventArgs e) { AllTestCase.Items.AddRange(SelectedTestCase.Items); SelectedTestCase.Items.Clear(); }