current resource in Scheduler

12 posts, 1 answers
  1. Denis Cilliers
    Denis Cilliers avatar
    62 posts
    Member since:
    Jan 2004

    Posted 21 Dec 2012 Link to this post

    Hi There

    I tried to implemented the code on the forum for the getting the current resource, as detailed in the link below
    Previous Thread on this topic

    but this code does not return the current resource it provides the first of the group of resources 
    So when you try to obtain the second resource and select that one the code returns the first one

    It does not seem to cater to the fact that more than one resource can be displayed at once
    Sub GetResource()
     
        'Get the Resource
        Dim point As Point = frmMain.rsdDiaryMain.PointToClient(Cursor.Position)
        Dim groupedDayViewElement As SchedulerDayViewGroupedByResourceElement =
                TryCast(frmMain.rsdDiaryMain.SchedulerElement.ViewElement, SchedulerDayViewGroupedByResourceElement)
        Dim cellElement As SchedulerCellElement = SchedulerUIHelper.GetCellAtPoint(point,groupedDayViewElement.GetDayViewElements())
        Dim dayViewElement As SchedulerDayViewElement = TryCast(cellElement.Parent.Parent.Parent.Parent.Parent.Parent, SchedulerDayViewElement)
        Dim resource As EventId = dayViewElement.View.GetResources(0).Id
     
        'Set the ATP dropdown for this resource
        Me.ddlResources.SelectedIndex = CType(resource.KeyValue, Integer)
    End Sub

    Any idea on how to select the current resource even when you have 2 or more resources showing on the scheduler?
  2. Denis Cilliers
    Denis Cilliers avatar
    62 posts
    Member since:
    Jan 2004

    Posted 21 Dec 2012 Link to this post

    This code seems to be working now go figure?

    May I revise my request then to how to get the current resource for the cell when formating it
    We have a requirement to block a slot for a given resource.  So when I format the cell I need to determine the resource of the cell and then determine if it needs to be blocked.

    I had in mind the following 

    'Cell format event
     
    'Send the e.CellElement to the GetResource(CellElement)
     
    Private Function GetCurrentResourceID(ByVal cellElement As SchedulerCellElement) As Integer
     
            'Get the Resource
            Dim dayViewElement As SchedulerDayViewElement = TryCast(cellElement.Parent.Parent.Parent.Parent.Parent.Parent, SchedulerDayViewElement)
            Dim resourceID As Integer = dayViewElement.View.GetResources(0).Id.KeyValue
     
            Return resourceID
     
        End Function

    But currently the CellElement is returning a nothing value for the TryCast
    Thanks for help 
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 21 Dec 2012 Link to this post

    Hi Denis,

    Thank you for writing.

    The code snippet you have provided gets the resource under the mouse pointer and it is working correctly on my end. Here is an alternative and more elegant way to do the same:
    Sub GetResource()
        Dim point As Point = rsdDiaryMain.PointToClient(Cursor.Position)
        Dim groupedDayViewElement As SchedulerDayViewGroupedByResourceElement =
                TryCast(rsdDiaryMain.SchedulerElement.ViewElement, SchedulerDayViewGroupedByResourceElement)
     
        If groupedDayViewElement Is Nothing Then
            Return
        End If
     
        For Each dayViewElement In groupedDayViewElement.GetDayViewElements()
            If dayViewElement.ControlBoundingRectangle.Contains(point) Then
                Dim resource As EventId = dayViewElement.View.GetResources(0).Id
                MessageBox.Show(CType(resource.KeyValue, Integer))
                Return
            End If
        Next 
    End Sub

    If this still does not work for you, then you might have not set the ResourceIds correctly. Please verify your ResourceMappingInfo if you are using a data source or check the initialization of your resources and make sure they have valid IDs set.

    If you continue experiencing difficulties, I would suggest you to open a new support ticket and attach a sample project which demonstrates your scenario. This will let me provide you with further support.

    Do not hesitate to contact us if you need further assistance.

    Kind regards,
    Ivan Todorov
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
  5. Denis Cilliers
    Denis Cilliers avatar
    62 posts
    Member since:
    Jan 2004

    Posted 05 Mar 2013 Link to this post

    Hi Ivan 

    I have implemented your code from December and that part is working great

    I now have a requirement to get the resource during the Cell_Format event
    This is to determine based on a resource if the cell is active or inactive

    I have this code currently and it works but is very slow 
    any help would be appreciated

    Public Shared Function GetCurrentResource(ByVal cellElement As SchedulerCellElement) As Integer
      
           'Get the Resource
           Dim intReturn As Integer = 0
      
           Try
               Dim dayViewElement As SchedulerViewVisualElement = cellElement.FindAncestor(Of SchedulerViewVisualElement)()
               Dim resource As EventId = dayViewElement.View.GetResources(0).Id
      
               'Return the value of the Resource
               intReturn = CType(resource.KeyValue, Integer)
           Catch ex As Exception
               intReturn = 0
           End Try
             
           Return intReturn
      
       End Function

  6. Svett
    Admin
    Svett avatar
    728 posts

    Posted 07 Mar 2013 Link to this post

    Hello Denis,

    You can determine the resource of the cell element by using the following code snippet:
    Private Sub radScheduler1_CellFormatting(sender As Object, e As SchedulerCellEventArgs)
        Dim x As Integer = e.CellElement.ControlBoundingRectangle.X + e.CellElement.ControlBoundingRectangle.Width / 2
        Dim y As Integer = e.CellElement.ControlBoundingRectangle.Y + e.CellElement.ControlBoundingRectangle.Height / 2
        Dim resource As Resource = Me.GetResourceAtPoint(New Point(x, y))
    End Sub
     
    Private Function GetResourceAtPoint(location As Point) As Resource
        Dim headerResources As SchedulerResourcesHeaderElement = Me.radScheduler1.SchedulerElement.FindDescendant(Of SchedulerResourcesHeaderElement)()
     
        If headerResources Is Nothing Then
            Return Nothing
        End If
     
        Dim index As Integer = 0
     
        For Each child As SchedulerVisualElement In headerResources.Children
            Dim cell As SchedulerCellElement = TryCast(child, SchedulerCellElement)
     
            If cell IsNot Nothing Then
                Dim bounds As Rectangle = child.ControlBoundingRectangle
     
                If (bounds.X <= location.X AndAlso location.X <= bounds.Right) OrElse (bounds.Y <= location.Y AndAlso location.Y <= bounds.Bottom) Then
                    Return headerResources.GetResource(index)
                End If
     
                index += 1
            End If
        Next
     
        Return Nothing
    End Function

    I hope this helps.

    Regards,
    Svett
    the Telerik team
    WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
  7. Khaled
    Khaled avatar
    6 posts
    Member since:
    Oct 2011

    Posted 27 Oct 2014 in reply to Svett Link to this post

    Hi Svett,
    Please advise on how to get the resourcesID in cell formatting event while the resourcecell is being drawn,
    as I will use this resourceID to get some data from sqlDatabase and concatenate it to the resource name

    Thanks in advance,
    Khaled
  8. George
    Admin
    George avatar
    500 posts

    Posted 30 Oct 2014 Link to this post

    Hi Khaled,

    Thank you for writing.

    The method my colleague provided returns a Resource which has an Id property, however I would strongly advice against sending a query to the database in the CellFormatting event. This event is fired multiple times and such queries will slow down the application dramatically. Instead, I would suggest a different approach - send a query to the database once, for each resource and save that result. You can then change the text of the cells without the need of formatting event:
    Private resourceMapping As New Dictionary(Of String, String)()
    Public Sub New()
        InitializeComponent()
     
        For Each resource As IResource In Me.Scheduler.Resources
            resourceMapping(resource.Name) = Me.SendQueryToDatabase(resource)
        Next
     
        AddHandler Me.Load, AddressOf Form1_Load1
    End Sub
     
    Private Sub Form1_Load1(sender As Object, e As EventArgs)
        Dim cells As IEnumerable(Of SchedulerResourceHeaderCellElement) = Me.Scheduler.SchedulerElement.FindDescendant(Of SchedulerResourcesHeaderElement)().Children.OfType(Of SchedulerResourceHeaderCellElement)()
     
        For Each cell As SchedulerResourceHeaderCellElement In cells
            cell.Text = Me.resourceMapping(cell.Text)
        Next
    End Sub

    I hope this helps.

    Regards,
    George
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  9. Khaled
    Khaled avatar
    6 posts
    Member since:
    Oct 2011

    Posted 05 Nov 2014 in reply to Svett Link to this post

    Hi Svett's,
    Thanks for your prompt response,
    --I would like to tell you that this class "SchedulerResourceHeaderCellElement" seem to be not existing in telerik classes as the auto correct intellisence in VS2010 advised to change it to either "SchedulerHeaderCellElement" or "SchedulerResourceHeaderElement"
    --Also please be aknowledged that I will use the final loop through IEnumerable"cells" that you mentioned in your reply to update the displayed resources name with the corresponding  TotalAppointments hours in the active View, so what do you think is that solution is good to display the resources concatenated with their total hours in the active view or do you recommend another solution.

    Example:
    ======
    RoomA -- TotalAppointemtsHours(5)
    RoomB -- TotalAppointemtsHours(12)
    RoomC -- TotalAppointemtsHours(33)


    Thanks in advance,
    Khaled Ezzat


  10. George
    Admin
    George avatar
    500 posts

    Posted 10 Nov 2014 Link to this post

    Hello Khaled,

    Thank you for replying.

    In older versions, indeed the SchedulerResourceHeaderCellElement type is not existent. You need to replace it with SchedulerCellElement:
    Dim cells As IEnumerable(Of SchedulerResourceHeaderCellElement) = Me.Scheduler.SchedulerElement.FindDescendant(Of SchedulerResourcesHeaderElement)().Children.OfType(Of SchedulerCellElement)()

    The cells are not many which means that iterating them will not be a problem. Sending a SQL query in the formatting event will be much slower due to the amount of times it is fired. The proposed solution seems okay on my end judging from the provided information.

    I hope this helps.

    Regards,
    George
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  11. Khaled
    Khaled avatar
    6 posts
    Member since:
    Oct 2011

    Posted 17 Nov 2014 in reply to Svett Link to this post

    Hello George,
    Thanks for your reply that was really helpful, however, finally I'd like to know If it's possible to get the resourceid not text of the cell?

    Best Regards,
    Khaled
  12. Khaled
    Khaled avatar
    6 posts
    Member since:
    Oct 2011

    Posted 19 Nov 2014 in reply to Svett Link to this post

    Hello George,

    well I found my way to get the resourceid as I concatenated the resource text with the resource id then replaced the resourceid with total hours while looping the way you showed me, however my problem now is that the scheduler resources doesn't show the total hours from the first time it is populated or loaded but whenever I reload or change its data I found the total hours being shown, however I checked the data of the Resource cells and found that their id was actually replaced by the Total hours however that is not shown from the first time, so please help

    Best Regards,
    Khaled
  13. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 20 Nov 2014 Link to this post

    Hello Khaled,

    Thank you for writing back.

    Here is a sample code snippet demonstrating how to get the resource id for the resource header cells. Additionally, total appointments duration is displayed for each resource:
    Sub New()
        InitializeComponent()
     
        Dim colors() As Color = {Color.LightBlue, Color.LightGreen, Color.LightYellow, _
                                 Color.Red, Color.Orange, Color.Pink, Color.Purple, Color.Peru, Color.PowderBlue}
     
        Dim names() As String = {"Alan Smith", "Anne Dodsworth", "Boyan Mastoni", "Richard Duncan", "Maria Shnaider"}
     
        For i As Integer = 0 To names.Length - 1
            Dim resource As New Telerik.WinControls.UI.Resource()
            resource.Id = New EventId(i)
            resource.Name = names(i)
            resource.Color = colors(i)
            Me.RadScheduler1.Resources.Add(resource)
        Next i
     
     
     
        Dim rand As New Random
        For index = 1 To 10
            Dim app As New Appointment(DateTime.Now.AddHours(index), TimeSpan.FromMinutes(30), "App" & index)
            app.ResourceId = Me.RadScheduler1.Resources(rand.Next(0, Me.RadScheduler1.Resources.Count)).Id
            Me.RadScheduler1.Appointments.Add(app)
        Next
         
        AddHandler Me.RadScheduler1.CellFormatting, AddressOf RadScheduler1_CellFormatting
        Me.RadScheduler1.ActiveView.ResourcesPerView = 3
        Me.RadScheduler1.GroupType = GroupType.Resource
    End Sub
     
    Private Sub RadScheduler1_CellFormatting(sender As Object, e As SchedulerCellEventArgs)
        If e.CellElement.Class = "ResourceCell" Then
            Dim resourceByText As Resource = GetResource(e.CellElement.Text)
            If e.CellElement.Tag = Nothing Or resourceByText IsNot Nothing Then
                e.CellElement.Tag = e.CellElement.Text
            End If
     
            If e.CellElement.Tag IsNot Nothing Then
                Dim resource As Resource = GetResource(e.CellElement.Tag.ToString())
                If resource IsNot Nothing Then
                    e.CellElement.Text = resource.Name & " " & resource.Id.KeyValue & " " & _
                        GetAppointmentsDuration(resource).ToString() & "min"
                End If
            End If
        End If
    End Sub
     
    Private Function GetResource(resourceName As String) As Resource
        For Each r As Resource In Me.RadScheduler1.Resources
            If r.Name = resourceName Then
                Return r
            End If
        Next
        Return Nothing
    End Function
     
    Private Function GetAppointmentsDuration(resource As Resource) As Object
        Dim duration As New TimeSpan
        For Each a As Appointment In Me.RadScheduler1.Appointments
            If a.ResourceId.KeyValue = resource.Id.KeyValue Then
                duration = duration.Add(a.Duration)
            End If
        Next
        Return duration
    End Function

    It is important to subscribe to the CellFormatting event after populating the scheduler with appointments and before changing the GroupType to GroupType.Resource in order to get the correct duration.

    Note that in Q2 2014 we have introduced an improved RadScheduler. I would recommend you to upgrade. Thus, you can benefit from all the introduced fixes and new features.

    I hope this information helps. If you have any additional questions, please let me know.

    Regards,
    Desislava
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
UI for WinForms is Visual Studio 2017 Ready