Monday, June 15, 2009

ListBox Operations

Duplicate Data Check
This Code Snippet Is used to Check if Left ListBox has duplicate Data of Right List Box or not
protected void CheckDuplicateItemsFromBox(ListBox lsbRight, ListBox lsbLeft)
{
for (int countRight = 0; countRight < lsbRight.Items.Count; countRight++)
{
for (int countLeft = 0; countLeft < lsbLeft.Items.Count; countLeft++)
{
if (lsbRight.Items[countRight].Text == lsbLeft.Items[countLeft].Text)
{
lsbLeft.Items.RemoveAt(countLeft);
}
}

}
}




Move Left ListBox to Right

protected void MoveLeftListBoxToRightListBox(ListBox leftListBox, ListBox rightListBox)
{
while (leftListBox.Items.Count > 0 && leftListBox.SelectedItem != null)
{
ListItem selectedItem = leftListBox.SelectedItem;
selectedItem.Selected = false;
rightListBox.Items.Add(selectedItem);
leftListBox.Items.Remove(selectedItem);
}

}


Move Right ListBox to Left


protected void MoveRightListBoxToLeftListBox(ListBox leftListBox, ListBox rightListBox)
{
while (rightListBox.Items.Count > 0 && rightListBox.SelectedItem != null)
{
ListItem selectedItem = rightListBox.SelectedItem;
selectedItem.Selected = false;
leftListBox.Items.Add(selectedItem);
rightListBox.Items.Remove(selectedItem);
}
}

No comments:

Post a Comment