Right now I'm trying to draw a printable form - I have the print working properly right now but I need to get lines to be drawn on the form. Right now I'm using a button with
[code]
private void button1_Click_1(object sender, EventArgs e)
{
int BiggestSize = 100;
panel5.Paint += new PaintEventHandler(LinePainter);
Value1 = 1;
Value2 = BiggestSize;
Value3 = panel5.Width;
Value4 = BiggestSize;
}
[/code]
[code]
private void LinePainter(object sender, PaintEventArgs Painte)
{
Pen ColourPen = new Pen(System.Drawing.Color.Red);
int ColourPosition = 1;
if (Company == "MCI")
{
switch(ColourPosition)
{
case 1:
ColourPen.Color = (System.Drawing.Color.MediumSlateBlue);
ColourPosition = 2;
break;
case 2:
ColourPen.Color = (System.Drawing.Color.DarkOliveGreen);
ColourPosition = 3;
break;
case 3:
ColourPen.Color = (System.Drawing.Color.DarkOrange);
ColourPosition = 1;
break;
}
}
else if (Company == "SteelWorks")
{
switch (ColourPosition)
{
case 1:
ColourPen = new Pen(System.Drawing.Color.MediumSlateBlue);
ColourPosition = 2;
break;
case 2:
ColourPen = new Pen(System.Drawing.Color.MediumSlateBlue);
ColourPosition = 3;
break;
case 3:
ColourPen = new Pen(System.Drawing.Color.MediumSlateBlue);
ColourPosition = 1;
break;
}
}
System.Drawing.Graphics graphicsOBJ;
graphicsOBJ = this.panel5.CreateGraphics();
graphicsOBJ.DrawLine(ColourPen, new Point(Value1, Value2),new Point(Value3, Value4));
//Painte.Graphics.DrawLine(ColourPen, V1, V2, V3, V4);
//Painte.Dispose();
label1.Text = ColourPosition.ToString();
}
[/code]
I can't get the switch to work as well, but that seems to be because the eventhandler throws itself as always on (if I change the label1.text in my code, it changes on the form without me pressing the button).
I've tried to find out how to turn this into an instanced version of a drawing creation but can't quite get the right info on it. Any help?
My end goal is to draw a line as many time as is required by the user - in this case, when they press button1.
Loading
FroglegPosted Apr 14, 2011, 3:36 AM
FroglegPosted Apr 14, 2011, 2:40 AM
And i moved - int ColourPosition = 1;
out of button click event so that every time you click button1 it increments - if you create it inside button1 event it is created at a value of 1 every time and will not increase in value
edit
included a pdf of basics
Note page 4-5
Charles MorissettePosted Apr 12, 2011, 7:53 AM
Suthish NairPosted Apr 12, 2011, 7:24 AM
FroglegPosted Apr 11, 2011, 3:48 PM
2: In painel paint event use e.Graphics tp draw anything
3: To make the panel draw call
panel1.Refresh(); or panel1.Invalidate();
public partial class Form1 : Form
{
int Value1, Value2, Value3, Value4;
string Company;
int ColourPosition = 1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Company = ("MCI");
comboBox1.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
int BiggestSize = 100;
//panel1.Paint += new PaintEventHandler(LinePainter);
panel1.Refresh();
Value1 = 1;
Value2 = BiggestSize;
Value3 = panel1.Width;
Value4 = BiggestSize;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
if (checkBox1.Checked)
{
DrawGrid(e);
}
Pen ColourPen = new Pen(System.Drawing.Color.Red);
if (Company == "MCI")
{
switch (ColourPosition)
{
case 1:
ColourPen.Color = (System.Drawing.Color.MediumSlateBlue);
ColourPosition = 2;
break;
case 2:
ColourPen.Color = (System.Drawing.Color.DarkOliveGreen);
ColourPosition = 3;
break;
case 3:
ColourPen.Color = (System.Drawing.Color.DarkOrange);
ColourPosition = 1;
break;
}
}
else if (Company == "SteelWorks")
{
switch (ColourPosition)
{
case 1:
ColourPen = new Pen(System.Drawing.Color.MediumSlateBlue);
ColourPosition = 2;
break;
case 2:
ColourPen = new Pen(System.Drawing.Color.MediumSlateBlue);
ColourPosition = 3;
break;
case 3:
ColourPen = new Pen(System.Drawing.Color.MediumSlateBlue);
ColourPosition = 1;
break;
}
}
e.Graphics.DrawLine(ColourPen, new Point(Value1, Value2), new Point(Value3, Value4));
//Painte.Graphics.DrawLine(ColourPen, V1, V2, V3, V4);
//Painte.Dispose();
label1.Text = ColourPosition.ToString();
}
public void DrawGrid(PaintEventArgs e)
{
for (int i = 20; i < 200; i=i+20)
{
e.Graphics.DrawLine(Pens.Blue, 20, i, 300, i);
}
for (int ii = 20; ii < 320; ii = ii + 20)
{
e.Graphics.DrawLine(Pens.Blue, ii, 20, ii, 180);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex==0)
{
Company = "MCI";
}
else if (comboBox1.SelectedIndex == 1)
{
Company = "SteelWorks";
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
panel1.Refresh();
}
I put a combobox on the form to choose between MCI and steelworks
Also a grid is drawn if checkbox is checked