Updating RadListControl items issue

2 posts, 1 answers
  1. Christian
    Christian avatar
    12 posts
    Member since:
    Sep 2014

    Posted 27 Sep 2014 Link to this post


    ( First of all I need to clarify that I could manage both C# or VB solution to solve this issue )

    I have a RadListControl where I would like to use it to list the runing processes in the system (with a condition that no more than 1 process with the same name),
    that thing is done and I'm trying to update the list each 2 seconds (firstly determining If exists changes to update the current list),
    I'm saving the SelectedItems property of the control to restore the selected items in the RadListControl after updating the list and here is the problem, after clearing the items of the control using the Items.Clear method the Scrollbar moves up to the top of the control and I need to scrolldown to the desired position again and again.

    I would like to keep the current position after updating the items in the control, just like Windows TaskManager does for example.

    I also tried to set a collection of RadDataItem as the control DataSource, but then the Image property for each item is empty (I don't know why).

    I attached a gif in this post that demonstrates the problem,
    As you can see in the Gif, when I run a new process (the window which is at the left border of the gif) the process list is moved to top, and the same thing happens when I close that process.

    I don't know what more to try, here is the relevant part of the code:

    Private Sub Timer_RefreshProcessList_Tick(ByVal sender As Object, ByVal e As EventArgs) _
    Handles Timer_RefreshProcessList.Tick
        ' Processes that shouldn't be listed.
        Dim BlackListedProcesses As String() =
        ' Get the runing processes.
        Dim Processes As Process() = Process.GetProcesses
        ' Filter the processes by its name
        ' then set the RadListDataItem items containing the names and the process icons.
        Dim ProcessItems As IEnumerable(Of RadListDataItem) =
            (From proc As Process In Processes
            Where Not BlackListedProcesses.Contains(proc.ProcessName)
            Order By proc.ProcessName Ascending).
            GroupBy(Function(proc As Process) proc.ProcessName).
            Select(Function(procs As IGrouping(Of String, Process))
                       If Not procs.First.HasExited Then
                               Return New RadListDataItem With
                                        .Active = False,
                                        .Text = String.Format("{0}.exe", procs.First.ProcessName),
                                        .Image = ResizeImage(Icon.ExtractAssociatedIcon(procs.First.MainModule.FileName).ToBitmap,
                                                             Width:=16, Height:=16)
                           Catch ex As Exception
                               Return Nothing
                           End Try
                           Return Nothing
                       End If
                   End Function)
        ' If the RadListControl does not contain any item then...
        If Me.RadListControl_ProcessList.Items.Count = 0 Then
            With Me.RadListControl_ProcessList
                .Items.AddRange(ProcessItems) ' Add the RadListDataItems for first time.
            End With
            Exit Sub
        End If
        ' If RadListDataItems count is not equal than the runing process list count then...
        If Me.RadListControl_ProcessList.Items.Count <> ProcessItems.Count Then
            ' Save the current selected items.
            Dim SelectedItems As IEnumerable(Of String) =
                From Item As RadListDataItem
                In Me.RadListControl_ProcessList.SelectedItems
                Select Item.Text
            ' For Each ctrl As RadListDataItem In ProcessItems
            '     ctrl.Dispose()
            ' Next
            With Me.RadListControl_ProcessList
                ' .AutoScroll = False
                ' .SuspendSelectionEvents = True
                ' .SuspendItemsChangeEvents = True
                ' .SuspendLayout()
                ' .BeginUpdate()
                .Items.Clear() ' Clear the current RadListDataItems
                .Items.AddRange(ProcessItems) ' Add the new RadListDataItems.
                ' .EndUpdate()
                ' .ResumeLayout()
            End With
            ' Restore the selected item(s).
            For Each Item As RadListDataItem In Me.RadListControl_ProcessList.Items
                If SelectedItems.Contains(Item.Text) Then
                    Item.Selected = True
                    Item.Active = True
                    With Me.RadListControl_ProcessList
                        ' .ScrollToItem(Item)
                        ' .ListElement.ScrollToItem(Item)
                        ' .ListElement.ScrollToActiveItem()
                    End With
                End If
            Next Item
            With Me.RadListControl_ProcessList
                ' .AutoScroll = True
                ' .SuspendSelectionEvents = False
                ' .SuspendItemsChangeEvents = False
            End With
        End If
    End Sub

    Thanks in advance.


    What I did time ago to solve this issue with a normal ListBox control is this (I can't reproduce it in a RadListControl):

    ' [ListBox] Select item without jump
    ' Original author of code is "King King"
    ' Url: stackoverflow.com/questions/19479774/how-to-prevent-listbox-jumps-to-item
    ' Examples :
    ' Select_Item_Without_Jump(ListBox1, 50, ListBoxItemSelected.Select)
    ' For x As Integer = 0 To ListBox1.Items.Count - 1
    '    Select_Item_Without_Jump(ListBox1, x, ListBoxItemSelected.Select)
    ' Next
    ''' <summary>
    ''' Indicates whether the ListBox Item should be Selected or Unselected.
    ''' </summary>
    Private Enum ListBoxItemSelected
        ''' <summary>
        ''' Indicate that ListBox Item should be Selected.
        ''' </summary>
        [Select] = 1
        ''' <summary>
        ''' Indicate that ListBox Item should be Unselected.
        ''' </summary>
        [Unselect] = 0
    End Enum
    ''' <summary>
    ''' Selects or unselects a ListBox Item without jumping to the Item location on the layout.
    ''' </summary>
    Public Shared Sub Select_Item_Without_Jump(lb As ListBox, index As Integer, selected As ListBoxItemSelected)
        Dim i As Integer = lb.TopIndex ' Store the selected item index
        lb.BeginUpdate() ' Disable drawing on control
        lb.SetSelected(index, selected) ' Select the item
        lb.TopIndex = i ' Jump to the previous selected item
        lb.EndUpdate() ' Eenable drawing
    End Sub
  2. Answer
    Dimitar avatar
    2983 posts

    Posted 01 Oct 2014 Link to this post

    Hello Christian,

    Thank you for contacting us.

    If I understand correctly you want to preserve the scrollbar value along with the selected item, which can be done as follows:
    Private Sub radButton1_Click(sender As Object, e As EventArgs)
        Dim scrollValue As Integer = radListControl1.ListElement.VScrollBar.Value
        Dim selectedIndex As Integer = radListControl1.SelectedIndex
        'reset the items
        radListControl1.ListElement.VScrollBar.Value = scrollValue
        radListControl1.SelectedIndex = selectedIndex
    End Sub

    I hope this helps.

    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
Back to Top