Introduction
This is my first article, I intended it for beginners like me in C# as an example.
The code attached is an example of assigning events at run time and using delegates.
It is simple and sheds light
on the ideas used in graphics programs used by engineers. This code could be enhanced by adding rotation functionality around
three dimensional axes.
Detail
The image below shows the screen with some labels added and moved from their initial places by mouse and lines are drawn between them.
Using the code
In this sample I created a simple form with a button to add a label control at run time sequentially numbered.
When adding each label I assign to it labelMouseDown
and labelMouseMove
events. I use these two methods to draw lines connecting the added labels.
The lines drawn have two modes, either only one line is drawn from the last added label to the previous one, or the "LinkToAll
" mode, where a line is drawn to each existing label.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace MovableLabels
{
public partial class Form1 : Form
{
Label[] lblArray; Point move;
Graphics g;
Pen green = new Pen(Color.Green, 2);
int lblNum = 0;
const int Horizontal = 50;
int Vertical = 0;
delegate void callDrawDel();
callDrawDel callDraw;
public Form1()
{
InitializeComponent();
g = this.CreateGraphics();
callDraw = drawLines;
}
private void button1_Click(object sender, EventArgs e)
{
Label o = new Label();
o.Visible = true;
o.Top = Vertical*lblNum ;
Vertical += 10;
o.Left = Horizontal * lblNum;
o.Text = (++lblNum).ToString();
o.AutoSize = true;
o.MouseDown += new MouseEventHandler(this.labelMouseDown);
o.MouseMove += new MouseEventHandler(this.labelMouseMove);
this.Controls.Add(o);
List<Label> lblList = new List<Label>();
foreach (Control c in Controls)
if (c is Label)
lblList.Add((Label)c);
lblArray = lblList.ToArray();
callDraw(); }
private void labelMouseDown(object sender, MouseEventArgs e)
{
move = e.Location;
}
private void labelMouseMove(object sender, MouseEventArgs e)
{
Control o = (Control)sender;
if (e.Button == MouseButtons.Left)
{
o.Left += e.X - move.X;
o.Top += e.Y - move.Y;
callDraw();
}
}
private void drawLines()
{
g.Clear(this.BackColor);
for (int i = 1; i < lblArray.Length; i++)
lineDraw(lblArray[i - 1], lblArray[i]);
}
private void drawLinesAll()
{
g.Clear(this.BackColor);
for (int k = 0; k < lblArray.Length; k++)
{
Label L1 = lblArray[k];
for (int j = k + 1; j < lblArray.Length; j++)
lineDraw(L1, lblArray[j]);
}
}
private void lineDraw(Label L1, Label L2)
{
int x1, x2, y1, y2;
x1 = L1.Location.X + L1.Width / 2;
y1 = L1.Location.Y + L1.Height / 2;
x2 = L2.Location.X + L2.Width / 2;
y2 = L2.Location.Y + L2.Height / 2;
g.DrawLine(green, x1, y1, x2, y2);
}
private void LinkToAll_CheckedChanged(object sender, EventArgs e)
{
bool chk = LinkToAll.CheckState == CheckState.Checked;
if (chk)
callDraw = drawLinesAll;
else callDraw = drawLines;
callDraw();
}
private void btnClear_Click(object sender, EventArgs e)
{
foreach (Control c in lblArray)
this.Controls.Remove(c);
g.Clear(this.BackColor);
lblNum = Vertical = 0;
}
}
}