Beispiel #1
0
        // Call only on a single-threaded apartment thread.
        private void TestOnStaThread()
        {
            try
            {
                Monitor.Enter(m_lock);

                // Basic test using strings to define the list.
                var longEnumEditor = new LongEnumEditor();
                longEnumEditor.DefineEnum(new[] {"One", "Two", "Three"});
                longEnumEditor.MaxDropDownItems = 50;

                Control editingControl = longEnumEditor.GetEditingControl(null);
                var comboBox = editingControl as ComboBox;
                if (comboBox != null)
                {
                    Assert.IsTrue(comboBox.Items.Count == 3);
                    Assert.IsTrue(comboBox.Items[0].ToString() == "One");
                    Assert.IsTrue(comboBox.Items[1].ToString() == "Two");
                    Assert.IsTrue(comboBox.Items[2].ToString() == "Three");
                }

                // Use display names that are different than enum names.
                longEnumEditor = new LongEnumEditor();
                longEnumEditor.DefineEnum(new[] {"One==Neo", "Two==Zwei", "Three==Tres"});
                longEnumEditor.MaxDropDownItems = 50;

                editingControl = longEnumEditor.GetEditingControl(null);
                comboBox = editingControl as ComboBox;
                if (comboBox != null)
                {
                    Assert.IsTrue(comboBox.Items.Count == 3);
                    Assert.IsTrue(comboBox.Items[0].ToString() == "Neo");
                    Assert.IsTrue(comboBox.Items[1].ToString() == "Zwei");
                    Assert.IsTrue(comboBox.Items[2].ToString() == "Tres");
                }

                // Programmatically add a new item to the list.
                longEnumEditor.DefineEnum(new[] {"One==Neo", "Two==Zwei", "Three==Tres", "Four"});
                editingControl = longEnumEditor.GetEditingControl(null);
                comboBox = editingControl as ComboBox;
                if (comboBox != null)
                {
                    Assert.IsTrue(comboBox.Items.Count == 4);
                    Assert.IsTrue(comboBox.Items[0].ToString() == "Neo");
                    Assert.IsTrue(comboBox.Items[1].ToString() == "Zwei");
                    Assert.IsTrue(comboBox.Items[2].ToString() == "Tres");
                    Assert.IsTrue(comboBox.Items[3].ToString() == "Four");
                }

                // Use an enum.
                longEnumEditor = new LongEnumEditor(typeof (EnumTest));
                editingControl = longEnumEditor.GetEditingControl(null);
                comboBox = editingControl as ComboBox;
                if (comboBox != null)
                {
                    Assert.IsTrue(comboBox.Items.Count == 4);
                    Assert.IsTrue(comboBox.Items[0].ToString() == "Zero");
                    Assert.IsTrue(comboBox.Items[1].ToString() == "One");
                    Assert.IsTrue(comboBox.Items[2].ToString() == "Two");
                    Assert.IsTrue(comboBox.Items[3].ToString() == "Three");
                }

                // Test converter.           
                var converter = new IntEnumTypeConverter(typeof (EnumText2));

                // convert from string to int.
                Assert.IsTrue(converter.CanConvertFrom(typeof (string)));
                object result = converter.ConvertFrom("Five");
                Assert.IsTrue(result != null && (int) result == 5);

                // convert from int to string.
                Assert.IsTrue(converter.CanConvertTo(typeof (string)));
                object result2 = converter.ConvertTo(7, typeof (string));
                Assert.IsTrue(result2 != null && (string) result2 == "");

                result2 = converter.ConvertTo(8, typeof (string));
                Assert.IsTrue(result2 != null && (string) result2 == "Eight");
            }
            catch (Exception e)
            {
                m_exception = e;
            }
            finally
            {
                Monitor.Pulse(m_lock);
                Monitor.Exit(m_lock);
            }
        }