I really need some help here. I'm trying to create a program similar to game known as "connecting the dots", where you have dots with numbers from (1...n+1) and you need to connect them with lines.
So I have a panel and I read from the file the coordinates of the dots. But I'm stuck because I can't figure out how to connect the dots with the lines.
My current outcome
To sum up what I want to do:
You press on dot 1 , you press on dot 2 and they connect with a line, otherwise they dont connect.
And you need to conect the dots in order from 1 to n+1.
I hope you will understand me. Tanks a lot in advance!!
private void panel1_Paint(object sender, PaintEventArgs e)
{
List<String> pav1;
pav1 = new List<String>();
StreamReader datafile = new StreamReader("pav1.txt");
int[] X = new int[100];
int[] Y = new int[100];
int k = 0;
string line;
while (datafile.Peek() >= 0)
{
line = datafile.ReadLine();
X[k] = Int16.Parse(line);
line = datafile.ReadLine();
Y[k] = Int16.Parse(line);
k++;
}
datafile.Close();
Brush aBrush = (Brush)Brushes.Black;
for (int i = 0; i < k; i++)
{
e.Graphics.FillEllipse(aBrush, X[i], Y[i], 10, 10);
e.Graphics.DrawString((i + 1).ToString(), new Font("Arial", 10),
System.Drawing.Brushes.Gray, new Point(X[i] + 20, Y[i]));
}
}
First of all, take points out of panel_paint method, and add additional property like ordinal. So, instead of arrays X[] and Y[], you should make class like this:
public class Dot
{
public Point Coordinates { get; set; }
public int Ordinal { get; set; }
}
and then
List<Dot> Dots { get; set; }
Make two props for first and second selected dots
private Dot FirstDot { get; set; }
private Dot SecondDot { get; set; }
Fill that list same way you're filling X[] and Y[] arrays.
Then add OnMouseClick handler on your panel and in it write something like this:
private void panel1_MouseClick(object sender, MouseEventArgs e)
{
//check if user clicked on any of dots in list
var selectedDot = Dots.FirstOrDefault(dot => e.X < dot.Coordinates.X + 10 && e.X > dot.Coordinates.X
&& e.Y < dot.Coordinates.Y + 10 && e.Y > dot.Coordinates.Y);
//dot is found, add it to selected first or second dot property
if (selectedDot != null)
{
if (FirstDot == null)
FirstDot = selectedDot;
else if (SecondDot == null)
SecondDot = selectedDot;
}
}
now, in your paint method you should check if both dots are set, and if they are, check if they are one next to other, something like
if (FirstDot.Ordinal + 1 == SecondDot.Ordinal)
then you can draw lines using
e.Graphics.DrawLine(aBrush, FirstDot.Coordinates, SecondDot.Coordinates);
That should be it. I hope you understand the way how to implement it. Apart from few checks, that should be it.
Use the Graphics.Draw() method, I don't know why you're using ellipse drawing. And your loop should look something like
var myFont = new Font("Arial", 10);
for (int i = 0; i < k; i += 2)
{
var point1 = new Point(X[i], Y[i]);
var point2 = new Point(X[i + 1], Y[i + 1]);
e.Graphics.DrawLine(aBrush, point1, point2);
e.Graphics.DrawString((i + 1).ToString(), myFont, System.Drawing.Brushes.Gray, point1);
e.Graphics.DrawString((i + 2).ToString(), myFont, System.Drawing.Brushes.Gray, point2);
}
Also, point 0, 0 is the upper left corner.
Related
I would like to be able to create X x Y number of boxes / circles / buttons inside a picturebox. EXACTLY like Windows Defrag tool.
I tried to create a layout and keep adding buttons or pictureboxes to it, but its extremely slow and after 200 or so pictureboxes it crashes, runs out of window handles or memory.
What's the alternative? Can you show me a simple piece of code to add boxes just like Defrag tool where I can easily access them like
box[x,y].Color = Green as my app makes progress?
Currently I have this:
private void ResetTableStyles()
{
boardPanel.Controls.Clear();
boardPanel.RowStyles.Clear();
boardPanel.ColumnStyles.Clear();
boardPanel.RowCount = Rows;
boardPanel.ColumnCount = Columns;
for (int i = 0; i < Rows; i++)
{
boardPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100f));
}
for (int j = 0; j < Columns; j++)
{
boardPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f));
}
}
private void CreateButtons()
{
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Columns; j++)
{
var button = new PictureBox
{
BackColor = Color.White,
Dock = DockStyle.Fill,
Margin = Padding.Empty,
Tag = new Point(i, j),
BackgroundImageLayout = ImageLayout.Stretch
};
//button.MouseDown += button_MouseDown;
boardPanel.Controls.Add(button, j, i);
}
}
}
which as I said doesn't work, after sometime it just crashes and it takes very long time.
Perhaps someone has a better answer, but you could use a Panel as the canvas and handle the Paint event to draw colored rectangles onto the Panel. You could then use the mouse events, such as MouseMove, to figure out which cell you're on.
Here is a super-simple proof of concept.
// Data.cs
namespace WindowsFormsApplication
{
public class Data
{
public int State { get; set; }
public string Tip { get; set; }
}
}
// Form1.cs
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var rng = new Random();
_data = new Data[30 * 30];
for (int i = 0; i < _data.Length; i++)
{
_data[i] = new Data
{
State = rng.Next(0, 3),
Tip = $"Data at index: {i}"
};
}
}
private Data[] _data;
private void panel1_Paint(object sender, PaintEventArgs e)
{
using (var brush = new SolidBrush(Color.Gray))
using (var buffer = BufferedGraphicsManager.Current.Allocate(e.Graphics,
new Rectangle(0, 0, 450, 450)))
{
for (int x = 0; x < 30; x++)
{
for (int y = 0; y < 30; y++)
{
int dataIdx = (y * 30) + x;
Data data = _data[dataIdx];
if (data.State == 1)
{
brush.Color = Color.Blue;
}
else if (data.State == 2)
{
brush.Color = Color.Red;
}
else
{
brush.Color = Color.Gray;
}
buffer.Graphics.FillRectangle(brush, x * 15, y * 15, 15, 15);
buffer.Graphics.DrawLine(Pens.Black, 0, y * 15, 450, y * 15); //Gridline
}
buffer.Graphics.DrawLine(Pens.Black, x * 15, 0, x * 15, 450); //Gridline
}
buffer.Render(e.Graphics);
}
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
var point = e.Location;
int x = point.X / 15;
int y = point.Y / 15;
int dataIdx = (y * 30) + x;
System.Diagnostics.Debug.WriteLine(_data[dataIdx].Tip);
}
}
}
The Data class represents the model behind each segment on the panel and is pretty straight forward.
Form1 has a single control placed on it: a Panel named panel1 with a size of 450 x 450. Each cell on the panel will be 15 x 15, so we have 30 columns and 30 rows. In Form1's constructor, we initialize the Data that will be used to draw the panel.
Inside of Form1.panel1_Paint we iterate through the cells and look up the instance of Data for the given cell. Then, based on the State, set a color for the cell and draw a rectangle of that color. This is done on a buffer so the panel doesn't flicker when painting.
Inside of Form1.panel1_MouseMove we use the location of the mouse pointer relative to panel1 to figure out which cell the mouse is over, get the instance of Data for that cell, and display the Tip in the debug window.
You might be able to take this and build upon it, perhaps with a custom UserControl so you can easily access each individual cell with the column and row... your box[x, y].Color = Colors.Green example above.
I am attempting to draw about 3600 points on a form, it is pretty slow using one thread so I decided I want to use 4 threads for it.
In my code I divide the 3600 points to the 4 threads and they are supposed to draw it. however for some reason an ArgumentOutOfRangeException is being thrown.
I tried to debug my code but I couldn't find the mistake.
here is the code :
(Ignore the class _3DPoint, it is just a point that has x,y,z values. when I draw them I only use the x,y values.)
code for drawing the points :
public Graphics g; //g = this.CreateGraphics() in form1.Load()
public void drawrectangle(_3DPoint)
float xCord = float.Parse(p.x.ToString());
float yCord = float.Parse(p.y.ToString());
Brush b = new SolidBrush(Color.White);
xCord = lsize * xCord + center.X;
yCord = lsize * yCord + 10 + center.Y;
g.FillRectangle(b, xCord, yCord, 2, 2);
}
lsize, center are just variables for aligning the points as I want them.
All of the multithread action code:
public List<_3DPoint[]> multiThreadsdata = new List<_3DPoint[]>();
public void handlemultithread(_3DPoint[] P)
{
g.Clear(Color.Black);
for (int i = 0; i < multiThreads.Length; i++)
{
multiThreadsdata.Add(new _3DPoint[P.Length / multiThreads.Length]);
}
for (int i = 0; i < multiThreads.Length; i++)
{
for (int j = (P.Length / multiThreads.Length) * (i); j < (P.Length / multiThreads.Length) * (i + 1); j++)
{
multiThreadsdata[i][j - ((P.Length / multiThreads.Length) * i)] = new _3DPoint(P[j]);
}
}
for (int i = 0; i < multiThreads.Length; i++)
{
multiThreads[i] = new Thread(() => drawPoints(multiThreadsdata[i]));
multiThreads[i].Start();
}
}
delegate void SetCallBackPoint(_3DPoint location);
public void drawPoints(_3DPoint[] locations)
{
for (int i = 0; i < locations.Length; i++)
{
if (this.InvokeRequired)
{
SetCallBackPoint e = new SetCallBackPoint(drawrectangle);
this.Invoke(e, new object[] { locations[i] });
}
else
{
drawrectangle(locations[i]);
}
}
}
P is a _3DPoint array that contains all the 3600 points.
mutliThreads is a Thread[] containing 4 threads.
I get the exception in handlemultithread method. in the third line of this for loop :
for (int i = 0; i < multiThreads.Length; i++)
{
multiThreads[i] = new Thread(() => drawPoints(multiThreadsdata[i])); // <- here.
multiThreads[i].Start();
}
I don't know what is the problem, my guess is that there is some problem with the multithreading because I'm just a beginner with multithreading.
Thanks a bunch.
It is entirely possible to Draw 3600 rectangles quickly on a form when you apply the suggestions in the comments.
If that doesn't give you enough time you can consider creating Images on a single background thread, store them in some sort of buffer until they are needed to e painted on the Graphics object of the Paint event of the form. That is only feasible if you can know upfront what needs to be painted on the next frame.
This example uses a simple Background worker to fill an ConcurrentQueue with images. The comments in the code explain what is going on.
public partial class Form1 : Form
{
static ConcurrentQueue<Image> buffer = new ConcurrentQueue<Image>();
static Random r = new Random();
public Form1()
{
InitializeComponent();
backgroundWorker1.RunWorkerAsync();
// this is already a great performance win ...
DoubleBuffered = true;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Image img =null;
// get from buffer ..
if (!buffer.TryDequeue(out img))
{
// nothing available
// direct random
for (var x = 0; x < e.ClipRectangle.Width; x++)
{
for (var y = 0; y < e.ClipRectangle.Height; y++)
{
using (var pen = new Pen(new SolidBrush(Color.FromArgb(r.Next(255), r.Next(255), r.Next(255)))))
{
e.Graphics.DrawRectangle(pen, x, y, 1, 1);
}
}
}
}
else
{
// otherwise Draw the prepared image
e.Graphics.DrawImage(img,0,0);
Trace.WriteLine(buffer.Count);
img.Dispose();
}
}
private void button1_Click(object sender, EventArgs e)
{
// force a repaint of the Form
Invalidate();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// as long as the form is not disposed
while (!IsDisposed)
{
// we keep 60 images in memory
if (buffer.Count < 60)
{
// bitmap
var bmp = new Bitmap(this.Width, this.Height);
var img = Graphics.FromImage(bmp);
// draw
for (int i = 0; i < 3600; i++)
{
using (var pen = new Pen(new SolidBrush(Color.FromArgb(r.Next(255), r.Next(255), r.Next(255)))))
{
img.DrawRectangle(pen, r.Next(Width),r.Next(Height), r.Next(Width), r.Next(Height));
}
}
// store the drawing in the buffer
buffer.Enqueue(bmp);
}
else
{
// simple and naive way to give other threads a bit of room
Thread.Sleep(0);
}
}
}
}
Keep in mind that when you have a CPU heavy process adding more threads will not by magic make your methods run quicker. You might even make it worse: more threads compete for time on the CPU.
Initially I display data in cells as user scrolls I need to load more data in DataGridView.
I am using DataGridView CellPainting for drawing lines.
When I start scrolling in datagridview the cells get overlapped and it completely changes the output.
public partial class Display : Form
{
public Display()
{
InitializeComponent();
LoadData();
}
// To create the rows and columns and fill some data
private void LoadData()
{
int columnSize = 10;
DataGridViewColumn[] columnName = new DataGridViewColumn[columnSize];
for (int index = 0; index < columnSize; index++)
{
columnName[index] = new DataGridViewTextBoxColumn();
if (index == 0)
{
columnName[index].Name = "Name";
columnName[index].HeaderText = "Name";
}
else
{
columnName[index].Name = (index).ToString();
columnName[index].HeaderText = (index).ToString();
}
columnName[index].FillWeight = 0.00001f;
columnName[index].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
dataGridView1.Columns.Add(columnName[index]);
}
for (int rowIndex = 0; rowIndex < columnSize; rowIndex++)
{
dataGridView1.Rows.Add((rowIndex + 1).ToString());
dataGridView1.Rows[rowIndex].HeaderCell.Value = (rowIndex + 1).ToString();
}
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
Rectangle rectPos1 = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
Pen graphPen = new Pen(Color.Red, 1);
Graphics graphics = this.dataGridView1.CreateGraphics();
Point[] points =
{
new Point(rectPos1.Left , rectPos1.Bottom),
new Point(rectPos1.Right, rectPos1.Bottom),
new Point(rectPos1.Right, rectPos1.Top)
};
graphics.DrawLines(graphPen, points);
e.PaintContent(rectPos1);
e.Handled = true;
}
}
Sample Download Link
Which I have shown in the below image
How can i avoid it please help me solve this issue.
A couple of issues. First and foremost, you should almost always use the provided Graphics object you get from the PaintEventArgs. CreateGraphics is a temporary canvas that gets easily erased. One of the parameters you get is the CellBounds rectangle, so you can use that. Your lines are actually drawing outside of the rectangle, and you aren't clearing the previous contents, so your code should look something like this:
Rectangle rectPos1 = e.CellBounds;
e.Graphics.FillRectangle(Brushes.White, rectPos1);
Graphics graphics = e.Graphics; // this.dataGridView1.CreateGraphics();
Point[] points =
{
new Point(rectPos1.Left , rectPos1.Bottom - 1),
new Point(rectPos1.Right - 1, rectPos1.Bottom - 1),
new Point(rectPos1.Right - 1, rectPos1.Top)
};
graphics.DrawLines(Pens.Red, points);
e.PaintContent(rectPos1);
e.Handled = true;
I have a c# DataGridView with some columns. I want to add an Action Column as the last Cell in each Row.
There should be buttons or icons like View,Delete,Update in each Row.
Here are a few options I can think of:
By far the simplest is to add one Column for each Button. (Recommended!)
Of you could create a custom cell type, maybe a Panel subclass with the Buttons you want. This involves implementing IDataGridViewEditingControl iterface with at least a dozen fields and methods and also two custom classes derived from DataGridViewColumn and DataGridViewCell with many more things to do. In short a huge amount of work! For a sophisticated editing control maybe worth it. For a few Buttons certainly not! (Not recommended!)
Or you could fake the Buttons by a little owner-drawing magic in the CellPainting event. See below..!
Or you could add one sopisticated control outside the DataGridView that acts on the current row. The usual way!
Here is a fun little example, that owner-draws the four 'SIDU' commands in the 4th Column of a DatagGridView DGV:
private void Form1_Load(object sender, EventArgs e)
{
DGV.Rows.Add(12);
for( int i = 0; i< DGV.Rows.Count; i++)
{
DGV[0, i].Value = i;
DGV[1, i].Value = R.Next(1000);
DGV[2, i].Value = rights[R.Next(rights.Count)];
DGV[3, i].ReadOnly = true;
}
}
List<string> rights = new List<string>
{ "SIDU", "SID-", "SI-U", "S-DU", "SI--", "S--U", "S-D-", "S---" };
Dictionary<char, string> rightsTexts = new Dictionary<char, string>
{ { 'S', "Select" }, { 'I', "Insert" }, { 'D', "Delete" }, { 'U', "Update" } };
Random R = new Random(1);
private void DGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 3 && e.RowIndex >= 0)
{
string r = DGV[2,e.RowIndex].Value.ToString();
StringFormat format = new StringFormat()
{ LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center};
int w = e.CellBounds.Width / 4;
int y = e.CellBounds.Y + 1;
int h = e.CellBounds.Height - 2;
e.PaintBackground(e.CellBounds, false);
for (int i = 0; i < 4; i++)
{
int x = e.CellBounds.X + i * w;
Rectangle rect = new Rectangle(x, y, w, h);
ControlPaint.DrawButton(e.Graphics, rect, ButtonState.Normal);
if (rightsTexts.ContainsKey(r[i]))
e.Graphics.DrawString(rightsTexts[r[i]], DGV.Font,
SystemBrushes.WindowText, rect ,format );
}
e.Handled = true;
}
}
private void DGV_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == 3 && e.RowIndex >= 0)
{
DataGridViewCell cell = DGV[e.ColumnIndex, e.RowIndex];
int w = cell.Size.Width;
int buttonIndex = e.X * 4 / w;
Text = rightsTexts.ElementAt(buttonIndex).Value;
}
}
The drawing stuff was thrown up rather q&d, so you could invest a lot more care to fine-tune it..
I have chosen to display the rights in a visible cell for demostration. For production the action cell's value would be the obvious place.
I'm trying to draw 10 rectangles, but when I use g.DrawRectangle() it is drawing a cross as shown below:
I'm creating Vertex objects that contain a getRectangle() function which returns a Rectangle object for that vertex.
I was hoping to create these objects and show them as Rectangles on the pictureBox.
Here's my code
private System.Drawing.Graphics g;
private System.Drawing.Pen pen1 = new System.Drawing.Pen(Color.Blue, 2F);
public Form1()
{
InitializeComponent();
pictureBox.Dock = DockStyle.Fill;
pictureBox.BackColor = Color.White;
}
private void paintPictureBox(object sender, PaintEventArgs e)
{
// Draw the vertex on the screen
g = e.Graphics;
// Create new graph object
Graph newGraph = new Graph();
for (int i = 0; i <= 10; i++)
{
// Tried this code too, but it still shows the cross
//g.DrawRectangle(pen1, Rectangle(10,10,10,10);
g.DrawRectangle(pen1, newGraph.verteces[0,i].getRectangle());
}
}
Code for Vertex class
class Vertex
{
public int locationX;
public int locationY;
public int height = 10;
public int width = 10;
// Empty overload constructor
public Vertex()
{
}
// Constructor for Vertex
public Vertex(int locX, int locY)
{
// Set the variables
this.locationX = locX;
this.locationY = locY;
}
public Rectangle getRectangle()
{
// Create a rectangle out of the vertex information
return new Rectangle(locationX, locationY, width, height);
}
}
Code for Graph class
class Graph
{
//verteces;
public Vertex[,] verteces = new Vertex[10, 10];
public Graph()
{
// Generate the graph, create the vertexs
for (int i = 0; i <= 10; i++)
{
// Create 10 Vertexes with different coordinates
verteces[0, i] = new Vertex(0, i);
}
}
}
Looks like an exception in your draw loop
last call to:
newGraph.verteces[0,i]
fails with OutOfRangeException
you shoul iterate not to i <= 10, but to i < 10
Red Cross Indicates that an Exception has been thrown, you are not seeing it because it's being handled. Configure Visual Studio to break on exception throw to catch it.
An exception has been thrown. At first look your code:
for (int i = 0; i <= 10; i++)
will generate an IndexOutOfRangeException because verteces has 10 items but it will cycle from 0 to 10 (included so it'll search for 11 elements). It depends on what you want to do but you have to change the cycle to (removing the = from <=):
for (int i = 0; i < 10; i++)
or to increment the size of verteces to 11.