This is a migrated thread and some comments may be shown as answers.

RadListBox SelectedIndex

6 Answers 350 Views
ComboBox and ListBox (obsolete as of Q2 2010)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Chris
Top achievements
Rank 1
Chris asked on 16 May 2007, 12:58 PM
I have a RadListBox full of items.  I have two buttons "Up" and "Down" to move items around the RadListBox.  So if I have an item selected in the box and click the Up button then it swaps the currently selected item with the one above it.  Here's the code, it's very simple:
        Dim selIndex As Integer = RadListBox.SelectedIndex 
        If selIndex <> 0 Then 
            Dim selItem As String = RadListBox.Items.Item(selIndex).Text 
            Dim aboveItem As String = RadListBox.Items.Item(selIndex - 1).Text 
            RadListBox.Items.Item(selIndex - 1).Text = selItem 
            RadListBox.Items.Item(selIndex).Text = aboveItem 
        End If 
        RadListBox.SelectedIndex = selIndex - 1 
 


My problem is with the last line.  The SelectedIndex part highlights the correct item, but a black rectangle stays around the previously selected item.  I've tried refreshing the control, setting the focus, etc...  What do I need to do to get the black rectangle to follow the SelectedIndex item?

Chris

6 Answers, 1 is accepted

Sort by
0
Georgi
Telerik team
answered on 16 May 2007, 03:43 PM
Hi Chris,

As far as I understand, your goal is to rearrange the items by moving up/down a selected item through the Items collection on every button click. In your code you're using the Text property of RadListBoxItem which seems to change the position of the selected item, but you are not actually moving the item.

To achieve your goal I'll suggest the following approach:

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;  
    }  

I hope this helps!
 

Sincerely yours,
Georgi
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Chris
Top achievements
Rank 1
answered on 18 May 2007, 09:07 PM
Thank you Georgi, your suggestion is a much better way to do what I wanted.

Chris
0
Tim
Top achievements
Rank 1
answered on 07 Jun 2007, 02:08 AM
I'm trying to something the same but different and can't seem to figure out the Telerik way.  I have 2 list RadListBoxes... in 1 is a list of names, in the other, nothing.  When the user clicks a name I want to add it to the empty list box and remove it from the names listbox.  I've tried a similar version to what you have listed below with no avail... my code:

RadListItem ri = lstEnrolledStudents.SelectedItem as RadListBoxItem;
lstEnrolledStudents.Items.Add(ri);
lstAvailableStudents.Items.Remove(ri);

and it returns and error: Element already added! 

The error comes even after the first click...
0
Georgi
Telerik team
answered on 07 Jun 2007, 12:22 PM
Hello dsdtech,

The reason for getting this error is right here in your code snippet:

RadListItem ri = lstEnrolledStudents.SelectedItem as RadListBoxItem;
lstEnrolledStudents.Items.Add(ri);
lstAvailableStudents.Items.Remove(ri);

You are actually trying to add the same item. You should do something like this:

RadListItem ra = lstAvailableStudents.SelectedItem as RadListBoxItem;
lstAvailableStudents.Items.Remove(ri);
lstEnrolledStudents.Items.Add(ri);

I hope this information helps!

 
All the best,
Georgi
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Tim
Top achievements
Rank 1
answered on 07 Jun 2007, 05:19 PM
Thanks for the response Georgi,

sorry, my typo, guess I was tired and wasn't watching what I was typing.... what you posted is actually what i had in code and get the same result.... any other ideas?

0
Georgi
Telerik team
answered on 08 Jun 2007, 11:40 AM
This is the correct approach, dsdtech. Obviously the problem is somewhere else.

We will ask you to share more details on your project. Better yet, please send us a sample project that we can review locally. Once we check the problem, we will do our best to help you out. You can send us this information in a new support ticket.

 
Best wishes,
Georgi
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
ComboBox and ListBox (obsolete as of Q2 2010)
Asked by
Chris
Top achievements
Rank 1
Answers by
Georgi
Telerik team
Chris
Top achievements
Rank 1
Tim
Top achievements
Rank 1
Share this question
or