Exemple #1
0
        public FigureParametrs(AbstractShape figure) : this()
        {
            Type figureType = figure.GetType();

            foreach (PropertyInfo pi in figureType.GetProperties())
            {
                if (pi.Name != "GraphicsPath" && pi.Name != "Pen")
                {
                    this.AddLabel(pi.Name);
                    this.AddNumericUpDown(pi.Name, (int)pi.GetValue(figure));
                }
            }

            this.Draw_button.Click += (sender, args) =>
            {
                foreach (PropertyInfo pi in figureType.GetProperties())
                {
                    if (pi.Name != "GraphicsPath" && pi.Name != "Pen")
                    {
                        pi.SetValue(figure,
                                    (int)(this.ElementsPanel.Controls[pi.Name + NumericUpDownPostfix] as NumericUpDown).Value);
                    }
                }

                this.DialogResult = DialogResult.OK;
            };
        }
Exemple #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ShapeEditForm"/> class with <see cref="AbstractShape"/>
        /// object passed.
        /// </summary>
        /// <param name="shape"><see cref="AbstractShape"/> object to be edited.</param>
        /// <exception cref="System.NullReferenceException">Thrown when necessary
        /// <see cref="System.Windows.Forms.Control"/> cannot be found on the
        /// <see cref="System.Windows.Forms.TableLayoutPanel"/>.</exception>
        public ShapeEditForm(AbstractShape shape) : this()
        {
            // Boundaries for NumericUpDown Controls.
            const int MinValue = 0;
            const int MaxValue = 5000;

            // Adds Controls for Width, Color and DashStyle values of Pen.
            this.AddCommonControls((int)shape.PenWidth, shape.PenColor, shape.PenDashStyle);

            // Used for getting properties of Shape (using Reflection).
            Type shapeType = shape.GetType();

            // Adding nesessary Controls for Shape's properties.
            foreach (PropertyInfo pi in shapeType.GetProperties())
            {
                if (AreValid(pi.Name, pi.PropertyType))
                {
                    this.AddLabel(pi.Name);

                    // PropertyInfo.GetValue(object) takes Object, so we must correctly unbox it's value.
                    if (pi.PropertyType == typeof(int))
                    {
                        this.AddNumericUpDown(pi.Name, (int)pi.GetValue(shape), MinValue, MaxValue);
                    }
                    else
                    {
                        // if (pi.PropertyType == typeof(float))
                        this.AddNumericUpDown(pi.Name, (int)(float)pi.GetValue(shape), MinValue, MaxValue);
                    }
                }
            }

            this.AddButton.Click += (sender, args) =>
            {
                try
                {
                    shape.PenWidth     = (float)(this.TableLayoutPanel.Controls[nameof(shape.PenWidth) + NumericUpDownPostfix] as NumericUpDown).Value;
                    shape.PenColor     = (this.TableLayoutPanel.Controls[nameof(shape.PenColor) + ButtonPostfix] as Button).BackColor;
                    shape.PenDashStyle = (DashStyle)(this.TableLayoutPanel.Controls[nameof(shape.PenDashStyle) + ComboBoxPostfix] as ComboBox).SelectedItem;

                    foreach (PropertyInfo pi in shapeType.GetProperties())
                    {
                        if (AreValid(pi.Name, pi.PropertyType))
                        {
                            pi.SetValue(shape, (int)(this.TableLayoutPanel.Controls[pi.Name + NumericUpDownPostfix] as NumericUpDown).Value);
                        }
                    }
                }
                catch (NullReferenceException e)
                {
                    MessageBox.Show(e.Message, e.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                // The next line also will close this form automatically.
                this.DialogResult = DialogResult.OK;
            };

            // Used for checking properties' names and types.
            bool AreValid(string name, Type type)
            {
                return(name != "PenWidth" &&
                       name != "PenColor" &&
                       name != "PenDashStyle" &&
                       (type == typeof(int) || type == typeof(float)));
            }
        }