Exemplo n.º 1
0
        public void TestAddNew_CancelDifferentIndex()
        {
            List <object> list = new List <object>();

            list.Add(new object());
            list.Add(new object());

            BindingList <object> l = new BindingList <object>(list);

            bool   adding_event_raised = false;
            object o = new object();

            bool            list_changed       = false;
            ListChangedType change_type        = ListChangedType.Reset;
            int             list_changed_index = -1;

            l.AddingNew += delegate(object sender, AddingNewEventArgs e)
            {
                adding_event_raised = true;
                Assert.Null(e.NewObject);
            };

            l.ListChanged += delegate(object sender, ListChangedEventArgs e)
            {
                list_changed       = true;
                change_type        = e.ListChangedType;
                list_changed_index = e.NewIndex;
            };

            object rv = l.AddNew();

            Assert.True(adding_event_raised, "2");
            Assert.NotNull(rv);

            Assert.Equal(3, l.Count);
            Assert.Equal(2, l.IndexOf(rv));
            Assert.True(list_changed, "6");
            Assert.Equal(ListChangedType.ItemAdded, change_type);
            Assert.Equal(2, list_changed_index);

            list_changed = false;

            l.CancelNew(0);

            Assert.False(list_changed, "9");
            Assert.Equal(3, l.Count);

            l.CancelNew(2);

            Assert.True(list_changed, "11");
            Assert.Equal(ListChangedType.ItemDeleted, change_type);
            Assert.Equal(2, list_changed_index);
            Assert.Equal(2, l.Count);
        }
Exemplo n.º 2
0
        public void AddNew_CancelNewMultipleIndices_RemovesAddNewIndex()
        {
            var list = new List <object> {
                new object(), new object()
            };
            var bindingList = new BindingList <object>(list);

            bool            calledAddingNew   = false;
            bool            calledListChanged = false;
            ListChangedType listChangedType   = ListChangedType.Reset;
            int             listChangedIndex  = -1;

            bindingList.AddingNew += (object sender, AddingNewEventArgs e) =>
            {
                calledAddingNew = true;
                Assert.Null(e.NewObject);
            };
            bindingList.ListChanged += (object sender, ListChangedEventArgs e) =>
            {
                calledListChanged = true;
                listChangedType   = e.ListChangedType;
                listChangedIndex  = e.NewIndex;
            };

            object newValue = bindingList.AddNew();

            Assert.True(calledAddingNew);
            Assert.NotNull(newValue);

            Assert.Equal(3, bindingList.Count);
            Assert.Equal(2, bindingList.IndexOf(newValue));
            Assert.True(calledListChanged);
            Assert.Equal(ListChangedType.ItemAdded, listChangedType);
            Assert.Equal(2, listChangedIndex);

            // Cancelling index 0 does not change the list.
            calledListChanged = false;
            bindingList.CancelNew(0);

            Assert.False(calledListChanged);
            Assert.Equal(3, bindingList.Count);

            // Cancelling index 2 changes the list.
            bindingList.CancelNew(2);

            Assert.True(calledListChanged);
            Assert.Equal(ListChangedType.ItemDeleted, listChangedType);
            Assert.Equal(2, listChangedIndex);
            Assert.Equal(2, bindingList.Count);
        }
        private void AddButton_Click(object sender, EventArgs e)
        {
            //_people.Add(CreatePerson());

            var person = _people.AddNew();

            if (person.FirstName.Contains("7"))
            {
                _people.CancelNew(_people.IndexOf(person));
            }
        }
Exemplo n.º 4
0
        // Add the new part unless the part number contains
        // spaces. In that case cancel the add.
        private void button1_Click(object sender, EventArgs e)
        {
            Part newPart = listOfParts.AddNew();

            if (newPart.PartName.Contains(" "))
            {
                MessageBox.Show("Part names cannot contain spaces.");
                listOfParts.CancelNew(listOfParts.IndexOf(newPart));
            }
            else
            {
                textBox2.Text = randomNumber.Next(9999).ToString();
                textBox1.Text = "Enter part name";
            }
        }
        public void TestAddNew_EndDifferentIndexThenCancel()
        {
            BindingList <object> l = new BindingList <object>();

            bool   adding_event_raised = false;
            object o = new object();

            bool            list_changed       = false;
            ListChangedType change_type        = ListChangedType.Reset;
            int             list_changed_index = -1;

            l.AddingNew += delegate(object sender, AddingNewEventArgs e)
            {
                adding_event_raised = true;
                Assert.IsNull(e.NewObject, "1");
            };

            l.ListChanged += delegate(object sender, ListChangedEventArgs e)
            {
                list_changed       = true;
                change_type        = e.ListChangedType;
                list_changed_index = e.NewIndex;
            };

            object rv = l.AddNew();

            Assert.IsTrue(adding_event_raised, "2");
            Assert.IsNotNull(rv, "3");

            Assert.AreEqual(1, l.Count, "4");
            Assert.AreEqual(0, l.IndexOf(rv), "5");
            Assert.IsTrue(list_changed, "6");
            Assert.AreEqual(ListChangedType.ItemAdded, change_type, "7");
            Assert.AreEqual(0, list_changed_index, "8");

            list_changed = false;

            l.EndNew(2);

            Assert.AreEqual(1, l.Count, "9");
            Assert.IsFalse(list_changed, "10");

            l.CancelNew(0);

            Assert.IsTrue(list_changed, "11");
            Assert.AreEqual(ListChangedType.ItemDeleted, change_type, "12");
            Assert.AreEqual(0, list_changed_index, "13");
        }
Exemplo n.º 6
0
        public void AddNew_EndDifferenceIndexThanCancel_Success()
        {
            var bindingList = new BindingList <object>();

            bool            calledAddingNew   = false;
            bool            calledListChanged = false;
            ListChangedType listChangedType   = ListChangedType.Reset;
            int             listChangedIndex  = -1;

            bindingList.AddingNew += (object sender, AddingNewEventArgs e) =>
            {
                calledAddingNew = true;
                Assert.Null(e.NewObject);
            };
            bindingList.ListChanged += (object sender, ListChangedEventArgs e) =>
            {
                calledListChanged = true;
                listChangedType   = e.ListChangedType;
                listChangedIndex  = e.NewIndex;
            };

            // Make sure AddNew changed the list.
            object newValue = bindingList.AddNew();

            Assert.True(calledAddingNew);
            Assert.NotNull(newValue);

            Assert.Equal(1, bindingList.Count);
            Assert.Equal(0, bindingList.IndexOf(newValue));
            Assert.True(calledListChanged);
            Assert.Equal(ListChangedType.ItemAdded, listChangedType);
            Assert.Equal(0, listChangedIndex);

            // EndNew with an invalid index does not change the list.
            calledListChanged = false;
            bindingList.EndNew(2);
            Assert.Equal(1, bindingList.Count);
            Assert.False(calledListChanged);

            // CancelNew with a valid index changes the list.
            bindingList.CancelNew(0);
            Assert.True(calledListChanged);
            Assert.Equal(ListChangedType.ItemDeleted, listChangedType);
            Assert.Equal(0, listChangedIndex);
        }
Exemplo n.º 7
0
        public void AddNew_CancelDifferentIndexThenEnd_Success()
        {
            var list = new BindingList <object>();

            bool            calledAddingNew   = false;
            bool            calledListChanged = false;
            ListChangedType listChangedType   = ListChangedType.Reset;
            int             listChangedIndex  = -1;

            list.AddingNew += delegate(object sender, AddingNewEventArgs e)
            {
                calledAddingNew = true;
                Assert.Null(e.NewObject);
            };
            list.ListChanged += delegate(object sender, ListChangedEventArgs e)
            {
                calledListChanged = true;
                listChangedType   = e.ListChangedType;
                listChangedIndex  = e.NewIndex;
            };

            // Make sure AddNew changed the list.
            object newValue = list.AddNew();

            Assert.True(calledAddingNew);
            Assert.NotNull(newValue);

            Assert.Equal(1, list.Count);
            Assert.Equal(0, list.IndexOf(newValue));
            Assert.True(calledListChanged);
            Assert.Equal(ListChangedType.ItemAdded, listChangedType);
            Assert.Equal(0, listChangedIndex);

            // Calling CancelNew on an invalid index does not change the list.
            calledListChanged = false;
            list.CancelNew(2);
            Assert.Equal(1, list.Count);
            Assert.False(calledListChanged);

            // Calling EndNew does not change the list.
            list.EndNew(0);
            Assert.Equal(1, list.Count);
            Assert.False(calledListChanged);
        }
Exemplo n.º 8
0
        public bool tarkistanum; //bool tarkistaa onko Nimike-kentässä numeroita
        public void btnF2Tallenna_Click(object sender, EventArgs e)
        {
            tarkistanum = txbUusNimike.Text.Any(Char.IsDigit); // Tarkistetaan onko kentässä numeroita
            Nimike uusiNimike = Blista.AddNew();

            if (tarkistanum == true)
            {
                //Jos kentässä numeroita, poistetaan se listasta.
                Blista.CancelNew(Blista.IndexOf(uusiNimike));
                MessageBox.Show("Nimikkeessä ei voi olla numeroita!");
                txbUusNimike.Clear();
                ActiveControl = txbUusNimike; // Aseta kontrolli tekstiboksiin.
            }
            else
            {
                //Kun tiedot lisätään listaan
                MessageBox.Show("Tiedot lisätty!");
                Close();
            }
        }
Exemplo n.º 9
0
        public void AddNew_CancelNew_Success()
        {
            var bindingList = new BindingList <object>();

            bool            calledAddingNew   = false;
            bool            calledListChanged = false;
            ListChangedType listChangedType   = ListChangedType.Reset;
            int             listChangedIndex  = -1;

            bindingList.AddingNew += (object sender, AddingNewEventArgs e) =>
            {
                calledAddingNew = true;
                Assert.Null(e.NewObject);
            };
            bindingList.ListChanged += (object sender, ListChangedEventArgs e) =>
            {
                calledListChanged = true;
                listChangedType   = e.ListChangedType;
                listChangedIndex  = e.NewIndex;
            };

            object newValue = bindingList.AddNew();

            Assert.True(calledAddingNew);
            Assert.NotNull(newValue);

            Assert.Equal(1, bindingList.Count);
            Assert.Equal(0, bindingList.IndexOf(newValue));
            Assert.True(calledListChanged);
            Assert.Equal(ListChangedType.ItemAdded, listChangedType);
            Assert.Equal(0, listChangedIndex);

            calledListChanged = false;
            bindingList.CancelNew(0);
            Assert.Equal(0, bindingList.Count);
            Assert.True(calledListChanged);
            Assert.Equal(ListChangedType.ItemDeleted, listChangedType);
            Assert.Equal(0, listChangedIndex);
        }