/// <summary> /// Mouse-click handler of the picture box. /// You need to change this method. /// </summary> private void PictureBoxWindFarmMouseClick(object sender, MouseEventArgs e) { // Read mouse-click position int x = e.X; int y = e.Y; int poleHeight = _heightTrackBar.Value; decimal capacity = _capacityUpDown.Value; int bladeCount = (int)_numberOfBladesUpDown.Value; float rotorRadius = ((float)_radiusTrackBar.Value / (float)100); bool clockwise = _clockwiseCheckBox.Checked; Color poleColour = _poleColorButton.BackColor; Color rotorColour = _rotorColorButton.BackColor; if (e.Button == MouseButtons.Left) { foreach (WindTurbine turbine1 in _turbines) { if (turbine1.IsPoleClicked(x, y)) { _heightTrackBar.Value = turbine1.PoleHeight; _capacityUpDown.Value = turbine1.Capacity; _numberOfBladesUpDown.Value = turbine1.NumberOfBlades; _radiusTrackBar.Value = (int)(turbine1.RotorRadius * (float)100); _clockwiseCheckBox.Checked = turbine1.Clockwise; _poleColorButton.BackColor = turbine1.PoleColor; _rotorColorButton.BackColor = turbine1.RotorColor; return; } } // Create wind turbine at this position, using "grey" default for attributes WindTurbine turbine = new WindTurbine(poleHeight, rotorRadius, bladeCount, clockwise, poleColour, rotorColour, capacity, x, y); // Add wind turbine to farm list _turbines.Add(turbine); // Force redraw of the picture box to show changes _pictureBoxWindFarm.Refresh(); } else if (e.Button == MouseButtons.Right) { foreach (WindTurbine turbine1 in _turbines) { if (turbine1.IsPoleClicked(x, y)) { WindTurbine newTurbine = new WindTurbine(poleHeight, rotorRadius, bladeCount, clockwise, poleColour, rotorColour, capacity, (int)turbine1.CentreX, (int)turbine1.CentreY); _turbines.Remove(turbine1); _turbines.Add(newTurbine); return; } } } }
private void WindFarmForm_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { int x = e.X; int y = e.Y; mouseClicked = true; foreach (WindTurbine turbine in _turbines) { if (turbine.IsPoleClicked(x, y)) { turbineSelected = turbine; return; } } } }