Мне очень жаль спрашивать, но я отчаянно не могу найти это после нескольких часов.Так что я знаю, что не могу использовать foreus lus с element.Remove()
и что мне нужно использовать a для lus.Но я просто не могу найти решение, потому что оно немного сложнее.Я делаю приложение перетаскивания с логическими воротами и помещаю эти ворота в List<T>
, а именно:
private List<Tuple<Point, int>> droppedShapes = new List<Tuple<Point, int>>();
Мне нужно удалить ворота, щелкнув по воротам, когда я их поместил,это метод:
private void DeleteSelectedComponent(MouseEventArgs e)
{
if (droppedShapes != null)
{
foreach (var pair in droppedShapes)
{
var location = pair.Item1;
if (Math.Abs(location.X - e.X) < 25 && Math.Abs(location.Y - e.Y) < 25) //looks at width and height of component
{
droppedShapes.Remove(pair);
this.Invalidate();
}
}
}
}
Мне нужно var location
, чтобы знать, какие ворота необходимо удалить, но я не могу поместить этот код в for lus.(не обращайте внимания на очень плохой код) Спасибо за ваше внимание.Я с нетерпением жду вашего ответа.Вот остальная часть моего кода, если вам нужна дополнительная информация:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Demo3
{
public partial class MainProgram : Form
{
private Graphics screen;
private Bitmap backBuffer;
private Point? currentLocation = null;
private List<Tuple<Point, int>> droppedShapes = new List<Tuple<Point, int>>(); // Use your own Shape-interface in stead of the listbox index
private List<String> ports = new List<string>();
private HelpClass helpclass;
private bool delete = false;
public MainProgram()
{
InitializeComponent();
backBuffer = new Bitmap(drawPanel.Width, drawPanel.Height);
screen = Graphics.FromImage(backBuffer);
this.currentLocation = new Point(20, 20);
helpclass = new HelpClass();
FillBox();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
screen.Clear(Color.Black);
Pen whitePen = new Pen(Color.White);
SolidBrush tealBrush = new SolidBrush(Color.Teal);
SolidBrush whiteBrush = new SolidBrush(Color.White);
SolidBrush grayBrush = new SolidBrush(Color.Gray);
SolidBrush redBrush = new SolidBrush(Color.Red);
foreach (var pair in this.droppedShapes)
{
var shapeType = pair.Item2; // Reveal your own shape object here
var location = pair.Item1;
switch (shapeType) // Reveal your own shape object here
{
case 0:
screen.DrawRectangle(whitePen, location.X - 10, location.Y - 10, 20, 20);
screen.DrawString("&", new Font(FontFamily.GenericMonospace, (float)18), whiteBrush, location.X - 11, location.Y - 13);
screen.DrawLine(whitePen, location.X - 20, location.Y + 6, location.X - 10, location.Y + 6);
screen.DrawLine(whitePen, location.X - 20, location.Y - 6, location.X - 10, location.Y - 6);
screen.DrawLine(whitePen, location.X + 20, location.Y, location.X + 10, location.Y);
break;
case 1:
screen.DrawRectangle(whitePen, location.X - 10, location.Y - 10, 20, 20);
screen.DrawString("1", new Font(FontFamily.GenericMonospace, (float)10), whiteBrush, location.X, location.Y - 9);
screen.DrawString(">", new Font(FontFamily.GenericMonospace, (float)8), whiteBrush, location.X - 8, location.Y - 10);
screen.DrawLine(whitePen, location.X + 1, location.Y + 1, location.X - 5, location.Y + 1);
screen.DrawLine(whitePen, location.X - 20, location.Y + 6, location.X - 10, location.Y + 6);
screen.DrawLine(whitePen, location.X - 20, location.Y - 6, location.X - 10, location.Y - 6);
screen.DrawLine(whitePen, location.X + 20, location.Y, location.X + 10, location.Y);
break;
case 2:
screen.DrawRectangle(whitePen, location.X - 10, location.Y - 10, 20, 20);
screen.DrawString("1", new Font(FontFamily.GenericMonospace, (float)12), whiteBrush, location.X - 7, location.Y - 9);
screen.DrawLine(whitePen, location.X - 20, location.Y, location.X - 10, location.Y);
screen.DrawLine(whitePen, location.X + 20, location.Y, location.X + 10, location.Y);
screen.DrawLine(whitePen, location.X + 16, location.Y, location.X + 10, location.Y - 6);
break;
case 3:
screen.DrawRectangle(whitePen, location.X - 10, location.Y - 10, 20, 20);
screen.DrawString("&", new Font(FontFamily.GenericMonospace, (float)18), whiteBrush, location.X - 11, location.Y - 13);
screen.DrawLine(whitePen, location.X - 20, location.Y + 6, location.X - 10, location.Y + 6);
screen.DrawLine(whitePen, location.X - 20, location.Y - 6, location.X - 10, location.Y - 6);
screen.DrawLine(whitePen, location.X + 20, location.Y, location.X + 10, location.Y);
screen.DrawLine(whitePen, location.X + 16, location.Y, location.X + 10, location.Y - 6);
break;
case 4:
screen.DrawRectangle(whitePen, location.X - 10, location.Y - 10, 20, 20);
screen.DrawString("1", new Font(FontFamily.GenericMonospace, (float)10), whiteBrush, location.X, location.Y - 9);
screen.DrawString(">", new Font(FontFamily.GenericMonospace, (float)8), whiteBrush, location.X - 8, location.Y - 10);
screen.DrawLine(whitePen, location.X + 1, location.Y + 1, location.X - 5, location.Y + 1);
screen.DrawLine(whitePen, location.X - 20, location.Y + 6, location.X - 10, location.Y + 6);
screen.DrawLine(whitePen, location.X - 20, location.Y - 6, location.X - 10, location.Y - 6);
screen.DrawLine(whitePen, location.X + 20, location.Y, location.X + 10, location.Y);
screen.DrawLine(whitePen, location.X + 16, location.Y, location.X + 10, location.Y - 6);
break;
default:
break;
}
}
for (int i = drawPanel.Left - 5; i < drawPanel.Right + 10 ; i += 5)
{
for (int j = drawPanel.Top - 50 ; j < drawPanel.Bottom - 5 ; j += 5)
{
screen.FillEllipse(redBrush, i, j, 2, 2);
}
}
// draw current location
if (currentLocation != null)
{
Point p = currentLocation.Value;
screen.FillEllipse(tealBrush, p.X + 1, p.Y, -4, -4);
}
drawPanel.CreateGraphics().DrawImage(backBuffer, 0, 0); //allows you to draw
}
private void DrawPanel_MouseClick(object sender, MouseEventArgs e)
{
if (delete == false)
{
if (CheckIfCollision(e))
{
droppedShapes.Add(new Tuple<Point, int>(new Point(helpclass.WidthSnap(e.X, drawPanel.Width), helpclass.HeightSnap(e.Y, drawPanel.Height)), (int)ComponentList.SelectedIndex));
this.Invalidate();
}
}
else
{
DeleteSelectedComponent(e);
}
}
private void drawPanel_MouseMove(object sender, MouseEventArgs e)
{
this.currentLocation = new Point(e.X, e.Y);
this.Invalidate();
}
private void drawPanel_MouseLeave(object sender, EventArgs e)
{
this.currentLocation = null;
this.Invalidate();
}
private void FillBox()
{
AddComponent();
foreach (string component in ports)
{
ComponentList.Items.Add(component);
}
}
private void AddComponent()
{
ports.Add("AND - Port");
ports.Add("OR - Port");
ports.Add("NOT - Port");
ports.Add("NAND - Port");
ports.Add("NOR - Port");
}
private bool CheckIfCollision(MouseEventArgs e) //checks if position is already taken
{
String message = "";
foreach (var pair in this.droppedShapes)
{
var location = pair.Item1;
if (Math.Abs(location.X - e.X) < 25 && Math.Abs(location.Y - e.Y) < 25) //looks at width and height of component
{
message += "Position is already taken!";
MessageBox.Show(message);
return false;
}
}
return true;
}
private void deleteButton_CheckedChanged(object sender, EventArgs e)
{
if (deleteButton.Checked)
{
delete = true;
//Console.WriteLine("true");
}
else
{
delete = false;
//Console.WriteLine("false");
}
}
private void DeleteSelectedComponent(MouseEventArgs e)
{
if (droppedShapes != null)
{
foreach (var pair in droppedShapes)
{
var location = pair.Item1;
if (Math.Abs(location.X - e.X) < 25 && Math.Abs(location.Y - e.Y) < 25) //looks at width and height of component
{
droppedShapes.Remove(pair);
this.Invalidate();
}
}
}
}
private void restartButton_Click(object sender, EventArgs e)
{
droppedShapes.Clear();
this.Invalidate();
}
}
}