RadListBox SelectedIndex

7 posts, 0 answers
  1. Chris Ruegsegger
    Chris Ruegsegger avatar
    46 posts
    Member since:
    Mar 2004

    Posted 16 May 2007 Link to this post

    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
  2. Georgi
    Admin
    Georgi avatar
    212 posts

    Posted 16 May 2007 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Chris Ruegsegger
    Chris Ruegsegger avatar
    46 posts
    Member since:
    Mar 2004

    Posted 18 May 2007 Link to this post

    Thank you Georgi, your suggestion is a much better way to do what I wanted.

    Chris
  5. Tim
    Tim avatar
    54 posts
    Member since:
    Apr 2007

    Posted 06 Jun 2007 Link to this post

    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...
  6. Georgi
    Admin
    Georgi avatar
    212 posts

    Posted 07 Jun 2007 Link to this post

    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
  7. Tim
    Tim avatar
    54 posts
    Member since:
    Apr 2007

    Posted 07 Jun 2007 Link to this post

    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?

  8. Georgi
    Admin
    Georgi avatar
    212 posts

    Posted 08 Jun 2007 Link to this post

    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
Back to Top
UI for WinForms is Visual Studio 2017 Ready