Home / Community & Support / Knowledge Base / RadControls for WinForms / ComboBox and ListBox / Moving up/down a selected item through the Items collection

Moving up/down a selected item through the Items collection

Article Info

Rating: Not rated

 

Article information

Article relates to

RadListBox for WinForms

Created by

Georgi Stoyanov, Telerik

Last modified

May 16, 2007

Last modified by

Georgi Stoyanov, Telerik


 
HOW-TO
Move up/down a selected item through the Items collection

SOLUTION
The goal here is to rearrange the items by moving up/down a selected item through the Items collection. Suppose there are two buttons on the form and a listbox. One of the buttons is moving up the items, and one for moving down.

Here is the approach that we recommend on using:

private void btnUp_Click(object sender, EventArgs e)     
{     
    if (radListBox1.SelectedIndex > 0)     
    {     
        int index = radListBox1.SelectedIndex;     
        RadListBoxItem current = radListBox1.SelectedItem as RadListBoxItem;     
        radListBox1.Items.Remove(current);     
        radListBox1.Items.Insert(index - 1, current);     
        radListBox1.SelectedIndex = index - 1;     
    }     
}     
    
private void btnDown_Click(object sender, EventArgs e)     
{     
    if ((radListBox1.SelectedIndex > -1) && (radListBox1.SelectedIndex < radListBox1.Items.Count - 1))     
    {     
        int index = radListBox1.SelectedIndex;     
        RadListBoxItem current = radListBox1.SelectedItem as RadListBoxItem;     
        radListBox1.Items.Remove(current);     
        radListBox1.Items.Insert(index + 1, current);     
        radListBox1.SelectedIndex = index + 1;     
    }     
}    
 

Comments

There are no comments yet.
If you'd like to comment on this KB article, please, send us a Support Ticket.
Thank you!

Please Sign In to rate this article.