i'm working on method that create a delivery order pdf but recently i have been asked to add more columns in the array. As a result my array doesn't fit in Portrait size anymore so i'm trying to rotate my page in Landscape mode but i can't manage to make it work here is my code (some word have been modified/deleted for professional secret but it remain understandable. Thanks in advance
public static void CreateDocument(String to)
{
System.Collections.Generic.List<Produit> listPro = new System.Collections.Generic.List<Produit>(MainWindow.produits);
//listPro = ;
// Create a new PDF document
PdfSharp.Pdf.PdfDocument document = new PdfSharp.Pdf.PdfDocument();
document.Info.Title = "Bon de livraison n°"+MainWindow.numOrder;
int numpage = 1;
decimal maxPerPage = 25.00M;
Decimal numberPage = listPro.Count / maxPerPage;
Console.WriteLine("nbr : "+ listPro.Count+ "/ 25 = "+ numberPage);
numberPage = Math.Ceiling(numberPage);
String watermark = "NE PAS DIFFUSER";
while (1 == 1)
{
// Create an empty page
PdfSharp.Pdf.PdfPage page = document.AddPage();
page.Orientation = PageOrientation.Landscape;
page.Rotate = (page.Rotate - 90) % 360;
// Get an XGraphics object for drawing watermark
var gfx2 = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);
XFont fontWater = new XFont("Verdana", 40, XFontStyle.Bold);
XSize size = gfx2.MeasureString(watermark, fontWater);
// Define a rotation transformation at the center of the page
gfx2.TranslateTransform(page.Width / 2, page.Height / 2);
gfx2.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
gfx2.TranslateTransform(-page.Width / 2, -page.Height / 2);
// Create a string format
XStringFormat format = new XStringFormat();
format.Alignment = XStringAlignment.Near;
format.LineAlignment = XLineAlignment.Near;
// Create a dimmed red brush
XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
// Draw the string
gfx2.DrawString(watermark, fontWater, brush,
new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
format);
gfx2.Dispose();
// Get an XGraphics object for drawing the order
XGraphics gfx = XGraphics.FromPdfPage(page);
/*gfx.TranslateTransform(page.Width / 2, page.Height / 2);
gfx.RotateTransform(-Math.Atan(page.Height / page.Width) *180 / Math.PI);
gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);*/
// Create a font
XFont font = new XFont("Verdana", 11, XFontStyle.Bold);
XFont fontRegular = new XFont("Verdana", 8, XFontStyle.Regular);
XFont fontMedium = new XFont("Verdana", 7, XFontStyle.Regular);
XFont fontLittle = new XFont("Verdana", 6, XFontStyle.Regular);
XFont fontRegularBold = new XFont("Verdana", 8, XFontStyle.Bold);
// Draw the text
gfx.DrawString("Bon de livraison", font, XBrushes.Black,
new XRect(0, 0, page.Width, 100),
XStringFormats.Center);
gfx.DrawString(to, font, XBrushes.Black,
new XRect(0, 0, page.Width, 130),
XStringFormats.Center);
gfx.DrawString("Produit à usage non thérapeutique", fontRegular, XBrushes.Black,
new XRect(0, 0, page.Width, 160),
XStringFormats.Center);
int x, y;
x = 40;
y = 80;
if (numpage == 1)
{
// gfx.DrawString(to, font, XBrushes.Black, page.Width, 100);
gfx.DrawString("Producteur : ", fontRegular, XBrushes.Black, x - 20, y - 50);
gfx.DrawString("", fontRegular, XBrushes.Black, x - 10, y - 40);
gfx.DrawString(" ", fontRegular, XBrushes.Black, x - 10, y - 30);
gfx.DrawString("", fontRegular, XBrushes.Black, x - 10, y - 20);
gfx.DrawString("N°BL", fontRegular, XBrushes.Black, x, y);
gfx.DrawLine(XPens.Black, x + 110, y - 10, x + 170, y - 10);
gfx.DrawLine(XPens.Black, x + 110, y + 4, x + 170, y + 4);
gfx.DrawLine(XPens.Black, x + 110, y - 10, x + 110, y + 4);
gfx.DrawLine(XPens.Black, x + 170, y - 10, x + 170, y + 4);
gfx.DrawString(MainWindow.numOrder.ToString(), fontRegular, XBrushes.Black, x + 113, y);
x = 700;
gfx.DrawString("to : ", fontRegular, XBrushes.Black, x - 30, y - 50);
gfx.DrawString("", fontRegular, XBrushes.Black, x - 20, y - 40);
gfx.DrawString("", fontRegular, XBrushes.Black, x - 20, y - 30);
gfx.DrawString("", fontRegular, XBrushes.Black, x - 20, y - 20);
gfx.DrawString("Technicien", fontRegular, XBrushes.Black, x - 20, y);
gfx.DrawLine(XPens.Black, x + 60, y - 10, x + 120, y - 10);
gfx.DrawLine(XPens.Black, x + 60, y + 4, x + 120, y + 4);
gfx.DrawLine(XPens.Black, x + 60, y - 10, x + 60, y + 4);
gfx.DrawLine(XPens.Black, x + 120, y - 10, x + 120, y + 4);
gfx.DrawString(MainWindow.intervenant, fontRegular, XBrushes.Black, x + 63, y);
x = 40;
y = y + 20;
gfx.DrawString("Date d'envoi des Produits", fontRegular, XBrushes.Black, x, y);
gfx.DrawLine(XPens.Black, x + 110, y - 10, x + 170, y - 10);
gfx.DrawLine(XPens.Black, x + 110, y + 4, x + 170, y + 4);
gfx.DrawLine(XPens.Black, x + 110, y - 10, x + 110, y + 4);
gfx.DrawLine(XPens.Black, x + 170, y - 10, x + 170, y + 4);
gfx.DrawString("10/03/2020", fontRegular, XBrushes.Black, x + 113, y);
x = 700;
gfx.DrawString("Nombre de produit", fontRegular, XBrushes.Black, x - 20, y);
gfx.DrawLine(XPens.Black, x + 60, y - 10, x + 120, y - 10);
gfx.DrawLine(XPens.Black, x + 60, y + 4, x + 120, y + 4);
gfx.DrawLine(XPens.Black, x + 60, y - 10, x + 60, y + 4);
gfx.DrawLine(XPens.Black, x + 120, y - 10, x + 120, y + 4);
gfx.DrawString(listPro.Count + "", fontRegular, XBrushes.Black, x + 63, y);
x = 40;
y = y + 20;
gfx.DrawString("Date d'envoi du BL", fontRegular, XBrushes.Black, x, y);
gfx.DrawLine(XPens.Black, x + 110, y - 10, x + 170, y - 10);
gfx.DrawLine(XPens.Black, x + 110, y + 4, x + 170, y + 4);
gfx.DrawLine(XPens.Black, x + 110, y - 10, x + 110, y + 4);
gfx.DrawLine(XPens.Black, x + 170, y - 10, x + 170, y + 4);
gfx.DrawString("10/03/2020", fontRegular, XBrushes.Black, x + 113, y);
}
else
{
y = 100;
}
x = 50;
y += 20;
int ydebut = y;
int xdebut = x;
if (to == "Yes")
{
gfx.DrawString("", fontRegular, XBrushes.Black, x -= 38, y + 12);
gfx.DrawString("Type", fontRegular, XBrushes.Black, x += 60, y + 12);
gfx.DrawString("N°Lymphobank", fontRegular, XBrushes.Black, x += 30, y + 12);
gfx.DrawString("Volume",fontRegular, XBrushes.Black, x+=40,y+12);
/*gfx.DrawString("IH et groupe sanguin", fontRegular, XBrushes.Black, x += 80, y + 12);
gfx.DrawString("Age", fontRegular, XBrushes.Black, x += 90, y + 12);
gfx.DrawString("Sexe", fontLittle, XBrushes.Black, x += 20, y + 12);
gfx.DrawString("Résultats Qualification Biologique du Don", fontRegular, XBrushes.Black, x += 20, y + 12);
gfx.DrawString("Blocage", fontLittle, XBrushes.Black, x += 370, y + 12);
gfx.DrawString("NbrDon", fontLittle, XBrushes.Black, x += 30, y + 12);
gfx.DrawString("Donneur Connu", fontRegular, XBrushes.Black, x += 30, y + 12);
*/// gfx.DrawLine(XPens.Black, xdebut - 40, y, x, y);
}
if (to != "YES")
{
gfx.DrawString("Numéro Lymphobank", fontMedium, XBrushes.Black, x += 3, y + 12);
gfx.DrawString("Volume (ml)", fontMedium, XBrushes.Black, x += 90, y + 12);
gfx.DrawString("IH et groupe sanguin", fontMedium, XBrushes.Black, x += 53, y + 12);
gfx.DrawString("Age", fontMedium, XBrushes.Black, x += 90, y + 12);
gfx.DrawString("Sexe", fontMedium, XBrushes.Black, x += 20, y + 12);
gfx.DrawString("Résultats Qualification Biologique du Don", fontMedium, XBrushes.Black, x += 25, y + 12);
gfx.DrawString("Blocage", fontMedium, XBrushes.Black, x += 370, y + 12);
gfx.DrawString("Nbr Don", fontMedium, XBrushes.Black, x += 40, y + 12);
gfx.DrawString("Donneur Connu", fontMedium, XBrushes.Black, x += 40, y + 12);
}
int Nproduit = 0;
foreach (Produit pro in listPro)
{
Nproduit++;
y += 15;
x = xdebut;
// NumeroYES = prod.GetYES()
//NumeroTypeProduit = prod.GetNumTypeYESProduit()
// Sexe = "" + prod.GetSexe() + "",
//DateNaissance = "" + prod.GetDateNaissance() + "",
if (to == "YES")
{
//gfx.DrawLine(XPens.Black, x -= 40, y + 3, xdebut, y + 3);
gfx.DrawString(pro.GetYES(), fontRegular, XBrushes.Black, x += 3, y + 12);
gfx.DrawString(pro.GetNumTypeYESProduit(), fontRegular, XBrushes.Black, x += 60, y + 12);
gfx.DrawString(pro.ToString(), fontRegular, XBrushes.Black, x += 30, y + 12);
gfx.DrawString(pro.GetVolume(), fontRegular, XBrushes.Black, x += 30, y + 12);
/* gfx.DrawString(pro.GetGroupeSimple(pro.GetGroupeProduit()) + pro.GetGroupeNonElargie(pro.GetGroupeProduit()), fontRegular, XBrushes.Black, x += 80, y + 12);
gfx.DrawString(pro.GetDateNaissance(), fontRegular, XBrushes.Black, x += 90, y + 12);
gfx.DrawString("" + pro.GetSexe() + "", fontRegular, XBrushes.Black, x += 20, y + 12);
gfx.DrawString(pro.GetResultatQBDTrans(), fontRegular, XBrushes.Black, x += 20, y + 12);
gfx.DrawString(pro.GetBlocage(), fontRegular, XBrushes.Black, x += 370, y + 12);
gfx.DrawString(pro.GetNombreDon(), fontRegular, XBrushes.Black, x += 30, y + 12);
gfx.DrawString(pro.GetDonneurConnu(), fontRegular, XBrushes.Black, x += 30, y + 12);
gfx.DrawLine(XPens.Black, xdebut, y + 3, x += 80 - 3, y + 3);*/
}
else
{
gfx.DrawString(pro.ToString(), fontMedium, XBrushes.Black, x += 3, y + 12);
gfx.DrawString(pro.GetVolume(), fontMedium, XBrushes.Black, x += 90, y + 12);
gfx.DrawString(pro.GetGroupeSimple(pro.GetGroupeProduit()) + pro.GetGroupeNonElargie(pro.GetGroupeProduit()), fontMedium, XBrushes.Black, x += 53, y + 12);
gfx.DrawString(pro.GetDateNaissance(), fontMedium, XBrushes.Black, x += 90, y + 12);
gfx.DrawString("" + pro.GetSexe() + "", fontMedium, XBrushes.Black, x += 20, y + 12);
gfx.DrawString(pro.GetResultatQBDTrans(), fontMedium, XBrushes.Black, x += 25, y + 12);
gfx.DrawString(pro.GetBlocage(), fontMedium, XBrushes.Black, x += 370, y + 12);
gfx.DrawString(pro.GetNombreDon(), fontMedium, XBrushes.Black, x += 40, y + 12);
gfx.DrawString(pro.GetDonneurConnu(), fontMedium, XBrushes.Black, x += 40, y + 12);
gfx.DrawLine(XPens.Black, xdebut, y + 3, x += 90 - 3, y + 3);
}
if (Nproduit == 25)
{
break;
}
}
listPro.RemoveRange(0, Nproduit);
y += 15;
x = xdebut;
if (to == "YES")
{
/*gfx.DrawLine(XPens.Black, x - 40, y + 3, x, y + 3);
gfx.DrawLine(XPens.Black, x -= 40, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 60, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 30, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 80, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 90, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 20, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 20, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 370, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 30, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 30, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 80, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, xdebut, y + 3, x, y + 3);
gfx.DrawLine(XPens.Black, xdebut - 40, ydebut, x, ydebut);*/
}
else
{
/* gfx.DrawLine(XPens.Black, x, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 90, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 90, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 20, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 25, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 370, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 40, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 40, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, x += 90, ydebut, x, y + 3);
gfx.DrawLine(XPens.Black, xdebut, y + 3, x, y + 3);
gfx.DrawLine(XPens.Black, xdebut, ydebut, x, ydebut);
*/
}
gfx.DrawString("Page "+numpage + "/" + numberPage, fontRegular, XBrushes.Black, 750, 580);
if (listPro.Count > 0)
{
Console.WriteLine("Nombre "+listPro.Count);
numpage++;
}
else
{
break;
}
}
// Save the document...
string filename = "BonDeLivraison_" + to+".pdf";
try
{
document.Save(filename);
Console.WriteLine("Fichier sauvegarder : "+filename);
//OUverture du document pur debug a enelever (mettre en commentaire)
//Process.Start(filename);
//decommenter pour que ca imprime automatiquement
RawPrinterHelper.SendFileToPrinter(MainWindow.imprimanteBon.getImprimanteText(),filename);
}
catch(Exception err)
{
MessageBox.Show(err+"", "Suppression du produit");
}
}
I hope you can help me and this is a big code, so I'll try to explain it short. I'm making a program that can place logic gates and that can connect to each other, I'm using the OnPaint(PaintEventArgs e) method and I have troubles with connecting gates with a line. (the code is also really chaotic)
I add my gates in a List
private List<Tuple<Point, int>> droppedShapes = new List<Tuple<Point, int>>();
And my lines in
private List<Tuple<Point, Point>> allLines = new List<Tuple<Point, Point>>();
So I can save the x-y coordinates.
I use buttons to connect my lines, so on each end of connection I got a button that I can click. But I can only connect the 2 inputs of one component to each other.
Here is my code, I hope you can help me!
Here you can see how I draw my components and buttons.
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 yellowBrush = new SolidBrush(Color.Yellow);
SolidBrush grayBrush = new SolidBrush(Color.Gray);
SolidBrush redBrush = new SolidBrush(Color.Red);
foreach (var line in allLines)
{
screen.DrawLine(whitePen, line.Item1, line.Item2);
}
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);
newB = new Point(location.X - 25, location.Y + 2);
newB2 = new Point(location.X - 25, location.Y - 10);
newB3 = new Point(location.X + 20, location.Y - 3);
button1 = new Button();
button2 = new Button();
button3 = new Button();
button1.Size = new Size(8, 8);
button2.Size = new Size(8, 8);
button3.Size = new Size(8, 8);
newB.Offset(0, 0);
newB2.Offset(0, 0);
newB3.Offset(0, 0);
button1.Location = newB;
button2.Location = newB2;
button3.Location = newB3;
button1.Click += Button_Click1;
button2.Click += Button_Click2;
button3.Click += Button_Click3;
drawPanel.Controls.Add(button1);
drawPanel.Controls.Add(button2);
drawPanel.Controls.Add(button3);
if (jaOfNee == true)
{
screen.DrawString(pair.Item1.ToString(), new Font(FontFamily.GenericMonospace, (float) 6), whiteBrush, location.X - 25, location.Y - 25);
}
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);
newB = new Point(location.X - 25, location.Y + 2);
newB2 = new Point(location.X - 25, location.Y - 10);
newB3 = new Point(location.X + 20, location.Y - 3);
button1 = new Button();
button2 = new Button();
button3 = new Button();
button1.Size = new Size(8, 8);
button2.Size = new Size(8, 8);
button3.Size = new Size(8, 8);
newB.Offset(0, 0);
newB2.Offset(0, 0);
newB3.Offset(0, 0);
button1.Location = newB;
button2.Location = newB2;
button3.Location = newB3;
button1.Click += Button_Click1;
button2.Click += Button_Click2;
button3.Click += Button_Click3;
drawPanel.Controls.Add(button1);
drawPanel.Controls.Add(button2);
drawPanel.Controls.Add(button3);
if (jaOfNee == true)
{
screen.DrawString(pair.Item1.ToString(), new Font(FontFamily.GenericMonospace, (float) 6), whiteBrush, location.X - 25, location.Y - 25);
}
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); //fills screen with a grid
}
}
// base.OnPaint(e);
// 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
}
}
(This is how I try to connect my 2 buttons)
foreach (var line in allLines)
{
screen.DrawLine(whitePen, line.Item1, line.Item2);
}
I don't use button 3 yet because I want to connect my inputs first so don't mind button 3.
This is how I try to save my X-Y coordinates.
private Point point1;
private Point point2;
private void Button_Click1(object sender, EventArgs e)
{
Console.WriteLine("button1");
point1 = button1.Location;
}
private void Button_Click2(object sender, EventArgs e)
{
Console.WriteLine("button2");
point2 = button2.Location;
}
private void Button_Click3(object sender, EventArgs e)
{
Console.WriteLine("button3");
}
private void DrawLine_Click(object sender, EventArgs e)
{
allLines.Add(new Tuple<Point, Point>(point1, point2));
}
After I press the button DrawLine it adds the location to the list.
Sorry for the horrible code.
I hope you understand my question a little bit more, it's really hard to explain my problem.
I'm really sorry to ask, but I'm getting desperate for not finding this after a couple of houres. So I know I can't use a foreach lus with a element.Remove() and that I need to use a for lus. But I just can't find a solution because it's a little bit more complicated. I'm making a drag and drop app with logic gates and I put these gates in a List<T> namely:
private List<Tuple<Point, int>> droppedShapes = new List<Tuple<Point, int>>();
I need to remove a gate with a click on the gate when i placed it, this is the method:
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();
}
}
}
}
I need that var location to know what gate needs to be deleted but I can't put this code in a for lus. (don't mind the very bad code) Thanks for your attention. I’m looking forward to your reply.
Here is the rest of my code if you need extra info:
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();
}
}
}
Add the ones you need to remove to a new list:
private void DeleteSelectedComponent(MouseEventArgs e)
{
if (droppedShapes == null) //Early-exit avoids arrow code, easier to read!
return;
var removeList = new List<Tuple<Point, int>>();
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
removeList.Add(pair);
}
removeList.ForEach(o => droppedShapes.Remove(o));
this.Invalidate();
}
The issue is that you can't iterate through a list and delete them at the same time. Add them to another list and remove them outside of the for-each.