c# Drawing lines between 2 buttons - c#

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.

Related

How to print data to next page in C# PrintDocument

I have written this code to print a lab report. It works fine but when I click on the print button and if the data of the report is more than one page it doesn't create the next page. Now I want the multiple-page setting I am totally confused about how to do it.
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int position = 60;
for (int j = NoOfItemsTill; j < types.Count; j++)
{
e.Graphics.DrawString(types[j], new Font("Arial", 16, FontStyle.Bold), Brushes.Black, new Point(30, yposition - 30 + position));
e.Graphics.DrawImage(imae, 33, yposition + position, imae.Width, imae.Height);
e.Graphics.DrawString("Test", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(40, yposition + 5 + position));
e.Graphics.DrawString("Normal Value", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(310, yposition + 5 + position));
e.Graphics.DrawString("Result ", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(560, yposition + 5 + position));
if (checkBox1showprise.Checked == true)
{
e.Graphics.DrawString("Price ", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(700, yposition + 5 + position));
}
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[1].Value.ToString() == types[j])
{
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[2].Value.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(30, yposition + 50 + position));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[3].Value.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(310, yposition + 50 + position));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[4].Value.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(560, yposition + 50 + position));
if (checkBox1showprise.Checked == true)
{
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[5].Value.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(700, yposition + 50 + position));
}
position += 25;
}
}
yposition = yposition + 100;
}
// reset vaerables
// NoOfItemPerpage = 0;
NoOfItemsTill = 0;
}
I want that after every 5 elements of my array, the next page is created and the data is printed onto the next page.

WPF - print datagrid contents

I Have looked around and I have not found a solid answeer to this question. I am trying to print my datagrid content when I press a button, the main problem is that my datagrid has too much data and only whatever is shown in the screen is printing. I need It to print all data and if the data does not fit in current page create a mew page and print the rest.
Here is my solution printing Data Grid View using System.Drawing.Printing
using System.Drawing.Printing;
private int PageCounter { get; set; } = 1;
private int RowCounter { get; set; }
Print Button
private void BtnPrint_Click(object sender, EventArgs e)
{
PrintDocument printDoc = new PrintDocument();
IQueryable<PaperSize> paperSizes = printDoc.PrinterSettings.PaperSizes.Cast<PaperSize>().AsQueryable();
printDoc.DefaultPageSettings.PaperSize = paperSizes.First(ps => ps.Kind == PaperKind.A4);
printDoc.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
pageCounter = 1;
printDoc.PrintPage += PrintDoc_PrintPage;
printDoc.Print();
}
Print Method
private void Print_Document(object sender, PrintPageEventArgs e)
{
if (e.Graphics == null)
throw new Exception("Unable to find page Graphics!");
int left = 30;
int cellLeft = left;
int top = 50;
int cellWidth = 0;
int headerHeight = 30;
string headerName = string.Empty;
string cellValue = string.Empty;
Rectangle rect = new();
int pageWidth = e.PageSettings.PaperSize.Width - 60;
int pageHeight = e.PageSettings.PaperSize.Height - 100;
Graphics g = e.Graphics;
Font font = new(FontFamily, 9);
Pen p = new(Brushes.Black, 1f);
Pen borderP = new(new SolidBrush(Color.FromArgb(240, 240, 240)), 1f);
StringFormat stringFormatCenter = new()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
StringFormat stringFormatRight = new()
{
Alignment = StringAlignment.Far,
LineAlignment = StringAlignment.Center
};
StringFormat stringFormatLeft = new()
{
LineAlignment = StringAlignment.Center
};
if (PageCounter == 1)
{
g.DrawRectangle(p, new Rectangle(left, top, pageWidth, 30));
top += 30;
g.DrawRectangle(p, new Rectangle(left, top, pageWidth, 30));
top += 30;
g.DrawRectangle(p, new Rectangle(left, top, pageWidth, 30));
top += 30;
g.DrawRectangle(p, new Rectangle(left, top, pageWidth / 2, 30));
g.DrawRectangle(p, new Rectangle(left + (pageWidth / 2), top, pageWidth / 2, 30));
top += 30;
top = 50;
g.DrawString
("Company Name"
, new Font(FontFamily, 14f, FontStyle.Bold)
, Brushes.Black
, new Rectangle(left, top, pageWidth, 30)
, stringFormatCenter);
top += 30;
g.DrawString
("Business Type"
, new Font(FontFamily, 12f, FontStyle.Bold)
, Brushes.Black
, new Rectangle(left, top, pageWidth, 30)
, stringFormatCenter);
top += 30;
g.DrawString
("Report Name"
, new Font(FontFamily, 12f, FontStyle.Bold)
, Brushes.Black
, new Rectangle(left, top, pageWidth, 30)
, stringFormatCenter);
top += 30;
g.DrawString
("User Name: " + SelectedUser.Name
, new Font(FontFamily, 9, FontStyle.Bold)
, Brushes.Black
, new Rectangle(left, top, pageWidth / 2, 30)
, stringFormatLeft);
g.DrawString
("Report Date: " + DateTime.Now.ToString("dd-mm-yyyy hh:mm:ss")
, new Font(FontFamily, 9, FontStyle.Bold)
, Brushes.Black
, new Rectangle(left + (pageWidth / 2), top, pageWidth / 2, 30)
, stringFormatRight);
top += 30;
g.DrawString
("Login Id: " + SelectedUser.LoginId
, new Font(FontFamily, 9, FontStyle.Bold)
, Brushes.Black
, new Rectangle(left, top, pageWidth / 2, 30)
, stringFormatLeft);
g.DrawString
("Printed By: " + LoggedUser.Name
, new Font(FontFamily, 9, FontStyle.Bold)
, Brushes.Black
, new Rectangle(left + (pageWidth / 2), top, pageWidth / 2, 20)
, stringFormatRight);
top += 30;
g.DrawString
("Rights detail as follows:"
, new Font(FontFamily, 8, FontStyle.Bold)
, Brushes.Black
, new Rectangle(left, top, pageWidth, 20)
, stringFormatLeft);
top += 20;
}
g.FillRectangle(new SolidBrush(Color.FromArgb(234, 239, 250)), new Rectangle(left, top, pageWidth, headerHeight));
foreach (string name in PrintableRights.First().GetType().GetProperties().Select(p => p.Name))
{
if (name.Equals("SrNo"))
{
headerName = "Sr #";
cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 8);
}
else if (name.Equals("Code"))
{
headerName = "Code";
cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 20);
}
else if (name.Equals("Name"))
{
headerName = "Name";
cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 57);
}
else if (name.Equals("HasRight"))
{
headerName = "Right";
cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 15);
}
rect = new Rectangle(cellLeft, top, cellWidth, headerHeight);
g.DrawString(headerName, new Font(FontFamily, 10, FontStyle.Bold), Brushes.Black, rect, stringFormatLeft);
cellLeft += cellWidth;
}
top += headerHeight;
cellLeft = left;
while (RowCounter < PrintableRights.Count())
{
object row = PrintableRights.ElementAt(RowCounter);
cellLeft = left;
foreach (string name in row.GetType().GetProperties().Select(prop => prop.Name))
{
if (name.Equals("SrNo"))
cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 8);
else if (name.Equals("Code"))
cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 20);
else if (name.Equals("Name"))
cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 57);
else if (name.Equals("HasRight"))
cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 15);
rect = new Rectangle(cellLeft, top, cellWidth, 20);
g.DrawRectangle(borderP, rect);
PropertyInfo? prop = row.GetType().GetProperty(name);
if (prop != null)
{
if (prop.PropertyType == typeof(bool))
{
var val = prop.GetValue(row, null);
if (val != null && (bool)val)
g.DrawString("Yes", font, Brushes.Black, rect, stringFormatLeft);
else if (val != null)
g.DrawString("No", font, Brushes.Black, rect, stringFormatLeft);
}
else if (prop.PropertyType == typeof(int))
{
var val = prop.GetValue(row, null);
if (val != null)
g.DrawString(((int)val).ToString("N0"), font, Brushes.Black, rect, stringFormatRight);
else
g.DrawString(string.Empty, font, Brushes.Black, rect, stringFormatRight);
}
else if (prop.PropertyType == typeof(string))
{
var val = prop.GetValue(row, null);
if(val != null)
g.DrawString((string)val, font, Brushes.Black, rect, stringFormatLeft);
else
g.DrawString(string.Empty, font, Brushes.Black, rect, stringFormatLeft);
}
}
cellLeft += cellWidth;
}
top += 20;
if (RowCounter < PrintableRights.Count() - 1)
{
if (top > pageHeight - 10)
{
RowCounter++;
break;
}
}
RowCounter++;
}
if (RowCounter <= PrintableRights.Count() - 1)
{
if (top + 10 > pageHeight)
{
g.DrawString("Continue....", new Font(FontFamily, 7f), Brushes.Black, e.PageSettings.PaperSize.Width - 200, e.PageSettings.PaperSize.Height - 60);
g.DrawString("Page # " + PageCounter.ToString(), new Font(FontFamily, 7f), Brushes.Black, e.PageSettings.PaperSize.Width - 100, e.PageSettings.PaperSize.Height - 60);
PageCounter++;
e.HasMorePages = true;
}
}
else if (e.HasMorePages)
{
g.DrawString("Continue....", new Font(FontFamily, 7f), Brushes.Black, e.PageSettings.PaperSize.Width - 200, e.PageSettings.PaperSize.Height - 60);
g.DrawString("Page # " + PageCounter.ToString(), new Font(FontFamily, 7f), Brushes.Black, e.PageSettings.PaperSize.Width - 100, e.PageSettings.PaperSize.Height - 60);
PageCounter++;
}
else
{
g.DrawString("Last Page.", new Font(FontFamily, 7f), Brushes.Black, e.PageSettings.PaperSize.Width - 200, e.PageSettings.PaperSize.Height - 60);
g.DrawString("Page # " + PageCounter.ToString(), new Font(FontFamily, 7f), Brushes.Black, e.PageSettings.PaperSize.Width - 100, e.PageSettings.PaperSize.Height - 60);
}
}

C# Save location of drawn Ellipse in List<Point>?

a small question here where I didn't find a proper answer to.
I want to save a drawn location of a Ellipse so I can later on draw a line between 2 ellipses, so I want to save this as a point if this is possible.
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
screen.Clear(Color.Black);
using (var p = new Pen(Color.White, 2))
{
for (int x = 0; x < p1List.Count; x++)
{
screen.DrawLine(p, p1List[x], p2List[x]);
}
}
List<Point> punten = new List<Point>();
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, 40, 40);
screen.DrawString("&", new Font(FontFamily.GenericMonospace, (float)28), whiteBrush, location.X - 11, location.Y - 13);
//input
screen.DrawLine(whitePen, location.X - 25, location.Y, location.X - 10, location.Y );
screen.FillEllipse(greenBrush, location.X - 30, location.Y - 5, 10, 10);
punten.Add(); //add the location of the "ellipse"
//input
screen.DrawLine(whitePen, location.X - 25, location.Y + 20, location.X - 10, location.Y + 20);
screen.FillEllipse(greenBrush, location.X - 30, location.Y + 15, 10, 10);
//output
screen.DrawLine(whitePen, location.X + 30, location.Y + 10, location.X + 45, location.Y + 10);
screen.FillEllipse(greenBrush, location.X + 40, location.Y + 5, 10, 10);
I already added the line "punten.Add()" because this is the line where I want to save the location.
If there is a better way, hit me up!
It seems alright to me, just replace your
punten.Add();
with
punten.Add(new Point(location.X, location.Y));
or something similar for drawing the lines between the locations.
Be aware that this will only add Points to your list, if the case 0 in the switch statement is hit, otherwise your List may not contain any Points (or you add Points to the list in the other case sections).

C# remove items in List<T> with mouse click in windows form

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.

Printing a Multi-page Listview

Ok, I have got a printDocument1_PrintPage method which is supposed to print out the contents of a ListView. During execution, the method only prints out the first page. Could someone help me to printout the remaining page(s)?
I have identified the end of the first page via the variable ypos and set the property e.HasMorePages = true.
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e){
Graphics graphics = e.Graphics;
int ypos = 78;
Font f1 = new Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Pixel);
Brush brush = new SolidBrush(Color.LightSlateGray);
graphics.FillRectangle(brush, new Rectangle(10, 10, 770, 50));
Pen blackpen = new Pen(Color.Black);
Pen graypen = new Pen(Color.LightGray);
Pen redpen = new Pen(Color.Red);
graphics.DrawRectangle(blackpen, new Rectangle(10, 10, 770, 50));
Brush B = new SolidBrush(listView1.ForeColor);
graphics.DrawLine(blackpen, 10, 10, 10, 1132);
graphics.DrawString("FORENAME", f1, Brushes.Black, new Point(20, 25));
graphics.DrawLine(blackpen, 130, 10, 130, 1132);
graphics.DrawString("SURNAME", f1, Brushes.Black, new Point(140, 25));
graphics.DrawLine(blackpen, 290, 10, 290, 1132);
graphics.DrawString("EXT.", f1, Brushes.Black, new Point(300, 25));
graphics.DrawLine(blackpen, 380, 10, 380, 1132);
graphics.DrawString("JOB TITLE", f1, Brushes.Black, new Point(410, 25));
graphics.DrawLine(blackpen, 780, 10, 780, 1132);
int[] X = { 15, 140, 300, 390, 720 };
int Y = 60;
f1 = listView1.Font;
for (int I = 0; I < listView1.Items.Count; I++){
for (int J = 0; J < listView1.Items[I].SubItems.Count - 1; J++){
graphics.DrawString(listView1.Items[I].SubItems[J].Text, f1, B, X[J], Y);
}
Y += f1.Height;
graphics.DrawLine(graypen, 10, ypos, 780, ypos);
ypos = ypos + 17;
if (ypos > 1132){
e.HasMorePages = true;
return;
}
}
graphics.Dispose();
}

Categories