void nameTextBox_LostFocus(object sender, EventArgs e)
 {
     if (nameChanged)
     {
         TodoItemNameChanged @event = new TodoItemNameChanged(todoItem.id, nameTextBox.Text);
         state.LoadAndPersist(@event);
         nameChanged = false;
     }
 }
        /// <summary>
        /// Creates a random event. Needs to be updated every time new events get created!
        /// </summary>
        /// <returns>An event. </returns>
        private List<IEvent> getRandomEvent()
        {
            //returns less then specified number.
            int whichEvent = differentSeedRandom.Next(10);
            Debug.WriteLine("Case: " + whichEvent);
            switch (whichEvent)
            {
                    // List name changed
                case 0:
                    ListNameChanged listNameChanged = new ListNameChanged(getRandomString(nameLength));
                    return toList(listNameChanged);
                    // Todo item created
                case 1:
                    return toList(getRandomTodoItemCreated());
                    // Todo item description changed.
                case 2:
                    // Can only add events to items that exist.
                    if(getAvailableTodoItems().Count == 0)
                    {
                        break;
                    }
                    TodoItemDescriptionChanged todoItemDescriptionChanged = new TodoItemDescriptionChanged(getRandomTodoItemGuid(), getRandomString(descriptionLenght));
                    return toList(todoItemDescriptionChanged);
                    // Todo item name changed.
                case 3:
                    // Can only add events to items that exist.
                    if (getAvailableTodoItems().Count == 0)
                    {
                        break;
                    }
                    TodoItemNameChanged todoItemNameChanged = new TodoItemNameChanged(getRandomTodoItemGuid(), getRandomString(nameLength));
                    return toList(todoItemNameChanged);
                case 4:
                    // Need at least two items to change Priority. It's more of a swap priority.
                    if (getAvailableTodoItems().Count < 2)
                    {
                        break;
                    }
                    return getRandomTodoItemPriorityChanged();
                    // Todo item deleted.
                case 5:
                    if (getAvailableTodoItems().Count <= 1)
                    {
                        break;
                    }
                    return toList(getRandomTodoItemDeleted());
                    // Todo item Priority increased.
                case 6:
                    if (getAvailableTodoItems().Count == 0)
                    {
                        break;
                    }
                    return toList(new TodoItemPriorityIncreased(getRandomTodoItemGuid()));
                    // Todo item Priority decreased.
                case 7:
                    if (getAvailableTodoItems().Count == 0)
                    {
                        break;
                    }
                    TodoItemAggregate toDecrease = getAvailableTodoItems().ElementAt(
                                        differentSeedRandom.Next(getAvailableTodoItems().Count));

                    if (toDecrease.priority < 2)
                        break;
                    return toList(new TodoItemPriorityDecreased(toDecrease.id));
                    // Multiple delete of todo items.
                case 8:
                    if (getAvailableTodoItems().Count <=  3)
                        break;
                    // Build a list of items to remove. Random amount.
                    List<Guid> removedItems = new List<Guid>();
                    for (int i = 0; i < differentSeedRandom.Next(2, maxsimultaneousDelete + 1); i++)
                    {
                        removedItems.Add(getAvailableTodoItems().ElementAt(
                                        differentSeedRandom.Next(getAvailableTodoItems().Count)).id);
                    }
                    // Make sure there are no duplicates.
                    TodoItemsDeleted deletedItems = new TodoItemsDeleted(removedItems.Distinct().ToList());
                    return toList(deletedItems);
                case 9:
                    if (getAvailableTodoItems().Count == 0)
                        break;
                    return toList(getRandomTodoItemIndexChanged());

            }
            return getRandomEvent();
        }
 public IEvent transformEvent(TodoItemNameChanged toTransform, IEvent transformAgainst)
 {
     if (transformAgainst.GetType().Equals(typeof(TodoItemCreated)))
     {
         //Change a name on an item that has been created server side without this side knowing, impossible.
     }
     else if (transformAgainst.GetType().Equals(typeof(TodoItemDeleted)))
     {
         if (toTransform.aggregateId == ((TodoItemDeleted)transformAgainst).aggregateId)
             //Delete todo item when other side has also deleted it. No problem, remove this event.
             return new Unused();
     }
     else if (transformAgainst.GetType().Equals(typeof(TodoItemNameChanged)))
     {
         // If it is the exact same, remove the event.
         if (toTransform.Equals((TodoItemNameChanged)transformAgainst))
             return new Unused();
         // If it is On the same aggregate but not the same, conflict.
         else if (toTransform.aggregateId == ((TodoItemNameChanged)transformAgainst).aggregateId)
             return null;
     }
     else if (transformAgainst.GetType().Equals(typeof(TodoItemsDeleted)))
     {
         //If the item deleted is deleted by multiple on the server, remove this event
         if (((TodoItemsDeleted)transformAgainst).aggregateIds.Contains(toTransform.aggregateId))
             return new Unused();
     }
     return toTransform;
 }