Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Disable multiple items on one day (Read 6392 times)
bert
YaBB Newbies
*
Offline


delegates?!

Posts: 6
Location: Austria
Joined: Feb 19th, 2010
Disable multiple items on one day
Feb 19th, 2010 at 10:59am
Print Post  
Hello,

I just started working with the Scheduling library and I am stuck on one thing:

I am trying to disallow adding and item to a day which already has an item added. This is no problem on freshly created items with simple if statements.

But when you have two items that do not overlap  you can still drag one item over a day with another existing item, so the items will overlap.

I do not want to disable the resizing and moving of items totally, because it's a nice feature to have.
I just don't want items to be resized and moved over other existing items (and overlap them).

I have tried alot of the setting and searched the forum but I could not get any further with this, so I hope some nice person could point me in the right direction.

Thanks in advance and best regards
  
Back to top
 
IP Logged
 
Meppy
God Member
*****
Offline


MindFusion support

Posts: 1783
Joined: Jul 20th, 2005
Re: Disable multiple items on one day
Reply #1 - Feb 19th, 2010 at 11:23am
Print Post  
Hi,

You need to handle the ItemCreating and ItemModifying events. These events will allow you to prevent creation and moving of items in cells, which already have items. Here is some sample code for both event handlers:

Code
Select All
private void calendar_ItemCreating(object sender, ItemConfirmEventArgs e)
{
      ItemCollection items = calendar.Schedule.GetAllItems(e.Item.StartTime, e.Item.EndTime.AddTicks(-1));
      if (items.Contains(e.Item))
            items.Remove(e.Item);

      e.Confirm = items.Count == 0;
}

private void calendar_ItemModifying(object sender, ItemModifyConfirmEventArgs e)
{
      ItemCollection items = calendar.Schedule.GetAllItems(e.NewStartTime, e.NewEndTime.AddTicks(-1));
      if (items.Contains(e.Item))
            items.Remove(e.Item);

      e.Confirm = items.Count == 0;
} 


Both methods inspect the time interval where the item is about to appear and if that interval already contains one or more items, the modification is canceled.

I hope this helps.

Regards,
Meppy
  
Back to top
 
IP Logged
 
bert
YaBB Newbies
*
Offline


delegates?!

Posts: 6
Location: Austria
Joined: Feb 19th, 2010
Re: Disable multiple items on one day
Reply #2 - Feb 23rd, 2010 at 9:27am
Print Post  
Thanks alot Meppy, that was exactly what I needed to start with!

Your code was really nice and simple, but I tried something more advanced (or chaotic?):

Code
Select All
        private void _calendar_ItemModifying(object sender, ItemModifyConfirmEventArgs e)
        {
            ItemCollection items = _calendar.Schedule.GetAllItems(e.NewStartTime, e.NewEndTime);



            if (items.Count > 0)
            {
                if (items.Contains(e.Item)) items.Remove(e.Item);



                foreach (Item item in items)
                {
                    // resize
                    if (e.ResizeEnd || e.ResizeStart)
                    {
                        // right to left
                        if (e.NewStartTime < item.EndTime && e.NewStartTime > item.StartTime)
                        {
                            e.NewStartTime = item.EndTime;
                        }

                        // left to right
                        if (e.NewEndTime > item.StartTime && e.NewStartTime < item.StartTime)
                        {
                            e.NewEndTime = item.StartTime;
                        }



                    }
                        // move
                    else
                    {
                        if (e.NewEndTime > item.StartTime && e.NewStartTime < item.StartTime)
                        {
                            TimeSpan ts;
                            ts = e.NewEndTime - e.NewStartTime;
                            e.NewStartTime = item.EndTime;
                            e.NewEndTime = item.EndTime.Add(ts);
                        }
                        if (e.NewStartTime < item.EndTime)
                        {
                            TimeSpan ts;
                            ts = e.NewEndTime - e.NewStartTime;
                            e.NewEndTime = item.StartTime;
                            e.NewStartTime = item.StartTime.Subtract(ts);
                        }
                    }


                }
            }
 



this works nicely if i have one item [ITEM1] and create a [ITEM2]. but if i have a little space between [ITEM1] and [ITEM2] and create a new [ITEM3] which is bigger than the space between 1&2 and move the ITEM3 into the space, it only snaps to one ITEM and not to both.

I guess I have the loop at a wrong position.
  
Back to top
 
IP Logged
 
bert
YaBB Newbies
*
Offline


delegates?!

Posts: 6
Location: Austria
Joined: Feb 19th, 2010
Re: Disable multiple items on one day
Reply #3 - Feb 23rd, 2010 at 9:42am
Print Post  
hmm actually I think your code was the better solution Smiley

I just modified it abit to
Code
Select All
		ItemCollection items = _calendar.Schedule.GetAllItems(e.NewStartTime, e.NewEndTime.AddDays(-1));

		if (items.Contains(e.Item))
		    items.Remove(e.Item);

		e.Confirm = items.Count == 0;
 



so its pretty much working to my likings now.
well there is no autoresize of moved items, but I can live with that
  
Back to top
 
IP Logged
 
Meppy
God Member
*****
Offline


MindFusion support

Posts: 1783
Joined: Jul 20th, 2005
Re: Disable multiple items on one day
Reply #4 - Feb 23rd, 2010 at 9:55am
Print Post  
What are you trying to achieve - some sort of automatic alignment of the modified items to the rest of the items?

Regards,
Meppy
« Last Edit: Feb 23rd, 2010 at 10:55am by Meppy »  
Back to top
 
IP Logged
 
bert
YaBB Newbies
*
Offline


delegates?!

Posts: 6
Location: Austria
Joined: Feb 19th, 2010
Re: Disable multiple items on one day
Reply #5 - Feb 23rd, 2010 at 10:04am
Print Post  
well yes kind of.
i want the items to automatically snap to the next available space, and even resize themselves.

maybe its easier to describe with screenshots Smiley
i want item "blah3" to automatically resize itself to the space left between "blah1" and "blah2" wenn moved there


to




best regards,
bert

  
Back to top
 
IP Logged
 
Meppy
God Member
*****
Offline


MindFusion support

Posts: 1783
Joined: Jul 20th, 2005
Re: Disable multiple items on one day
Reply #6 - Feb 23rd, 2010 at 12:19pm
Print Post  
Handle the Calendar.ItemModified event (in addition to ItemModifying, which should remain intact) and use the following implementation for the handler:

Code
Select All
DateTime closestStart = DateTime.MinValue;
DateTime closestEnd = DateTime.MaxValue;
foreach (Item item in calendar.Schedule.Items)
{
      if (item.EndTime < e.Item.StartTime && item.EndTime > closestStart)
            closestStart = item.EndTime;

      if (item.StartTime > e.Item.EndTime && item.StartTime < closestEnd)
            closestEnd = item.StartTime;
}

if (closestStart != DateTime.MinValue &&
      closestEnd != DateTime.MaxValue)
{
      e.Item.StartTime = closestStart;
      e.Item.EndTime = closestEnd;
      calendar.Invalidate();
} 


The above code should check an item, which has just been modified and resize it so that it fills the available space completely.

Regards,
Meppy
  
Back to top
 
IP Logged
 
bert
YaBB Newbies
*
Offline


delegates?!

Posts: 6
Location: Austria
Joined: Feb 19th, 2010
Re: Disable multiple items on one day
Reply #7 - Feb 23rd, 2010 at 2:02pm
Print Post  
thank you Meppy, you are really alot of help!

that code is perfect if there is a big gap between two items and you move a small item into the gap. it will fill up the gap.

now i am trying to modify your code to cut items if they're bigger than the gap Smiley
  
Back to top
 
IP Logged
 
Meppy
God Member
*****
Offline


MindFusion support

Posts: 1783
Joined: Jul 20th, 2005
Re: Disable multiple items on one day
Reply #8 - Feb 23rd, 2010 at 3:52pm
Print Post  
Check out this sample:

https://mindfusion.eu/_samples/_sample_AutoItems.zip

Regards,
Meppy
  
Back to top
 
IP Logged
 
bert
YaBB Newbies
*
Offline


delegates?!

Posts: 6
Location: Austria
Joined: Feb 19th, 2010
Re: Disable multiple items on one day
Reply #9 - Feb 24th, 2010 at 11:09am
Print Post  
Thats the perfect solution for my needs! Thank you alot meppy!
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint