Exemple #1
0
        private Rectangle CreateShape(string shapeType, int width, int height)
        {
            if (shapeType == "Rectangle")
            {
                var shape = new Rectangle();

                shape.SetWidth(width);
                shape.SetHeight(height);

                shape.Left = Random.Next(2, Canvas.Width - width + 2);
                shape.Top  = Random.Next(2, Canvas.Height - height + 2);

                return(shape);
            }

            if (shapeType == "Square")
            {
                var shape = new Square();

                shape.SetWidth(width);
                shape.SetHeight(height);

                shape.Left = Random.Next(2, Canvas.Width - width + 2);
                shape.Top  = Random.Next(2, Canvas.Height - height + 2);

                return(shape);
            }

            throw new Exception("Invalid Shape Type");
        }
Exemple #2
0
        private void AddShapeButton_Click(object sender, EventArgs e)
        {
            if (ShapeTypeCombo.SelectedItem == null)
            {
                MessageBox.Show(@"Please select a shape to add", @"Shape Type Required", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return;
            }

            if (string.IsNullOrEmpty(ShapeWidth.Text))
            {
                MessageBox.Show(@"Please enter a width", @"Width Required", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return;
            }

            if (!ShapeWidth.Text.All(char.IsDigit))
            {
                MessageBox.Show(@"Please enter a numeric value for the width", @"Width Required", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return;
            }

            if (string.IsNullOrEmpty(ShapeHeight.Text))
            {
                MessageBox.Show(@"Please enter a height", @"Height Required", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return;
            }

            var width  = Convert.ToInt32(ShapeWidth.Text);
            var height = Convert.ToInt32(ShapeHeight.Text);

            if (width < 10 || width > 100)
            {
                MessageBox.Show(@"Please enter a width between 10 and 100", @"Width Incorrect", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return;
            }

            if (height < 10 || height > 100)
            {
                MessageBox.Show(@"Please enter a height between 10 and 100", @"Height Incorrect", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return;
            }

            Rectangle r = CreateShape(ShapeTypeCombo.SelectedItem.ToString(), width, height);

            Rectangles.Add(r);

            Canvas.Controls.Add(new Panel
            {
                BorderStyle = BorderStyle.FixedSingle,
                Width       = r.GetWidth(),
                Height      = r.GetHeight(),
                Left        = r.Left,
                Top         = r.Top,
                BackColor   = Color.FromArgb(Random.Next(256), Random.Next(256), Random.Next(256))
            });
        }