Hallo
Also ich hab für die Matura ein kleines Programm geschrieben um zu üben und zwar fährt ein Zug(rechteckiger Block) von links nach rechts und soll bei einem bestimmten Punkt ein Event auslösen und in diesen Fall die Schranken zu schließen. Das funktioniert bei mir leider nicht und bin deshalb schon ein bisschen am verzweifeln
Falls ihr mehr Code von meinen Programm braucht sagt bitte bescheid
Danke für eure Hilfe
[CSharp]
// Meine Form 1
public partial class Form1 : Form
{
public delegate void GateEvent(Graphics g, int high, int width);
public event GateEvent gatev;
Street street;
RailTracks rt;
Graphics g;
Gates gat;
EventPoints ep;
Diesellok lok;
ELok elok;
int a;
Random rand;
int index;
int trainnumber;
public Form1()
{
InitializeComponent();
street = new Street();
rt = new RailTracks();
gat = new Gates(this);
ep = new EventPoints();
lok = new Diesellok();
elok = new ELok();
a = 0;
rand = new Random();
index = rand.Next(1, 3);
trainnumber = rand.Next(100, 201);
}
private void timer1_Tick(object sender, EventArgs e)
{
a = a + 1;
this.pictureBox1.Invalidate();
if ((this.a+120) == (this.pictureBox1.Width / 2-100))
{
this.StartEvent(g);
this.pictureBox1.Invalidate();
}
if (a > this.pictureBox1.Width)
{
index = rand.Next(1, 3);
a = 0;
trainnumber = rand.Next(100, 201);
this.pictureBox1.Invalidate();
}
}
public void StartEvent(Graphics g)
{
g = this.CreateGraphics();
gatev(g, this.pictureBox1.Height, this.pictureBox1.Width);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Font f = new Font("Arial", 15);
g = e.Graphics;
street.Draw(g, this.pictureBox1.Height, this.pictureBox1.Width);
rt.Draw(g, this.pictureBox1.Height, this.pictureBox1.Width);
gat.Draw(g, this.pictureBox1.Height, this.pictureBox1.Width);
ep.Draw(g, this.pictureBox1.Height, this.pictureBox1.Width);
if (index == 1)
{
lok.Draw(g, this.pictureBox1.Height, this.pictureBox1.Width, this.a);
g.DrawString("Diesel", f, Brushes.Black, this.a + 10, this.pictureBox1.Height / 2 - 30);
g.DrawString(trainnumber.ToString(), f, Brushes.Black, this.a + 70, this.pictureBox1.Height / 2 - 30);
g.DrawString("1990", f, Brushes.Black, this.a + 30, this.pictureBox1.Height / 2 - 10);
g.DrawString("500l", f, Brushes.Black, this.a + 30, this.pictureBox1.Height / 2 + 10);
}
else
{
elok.Draw(g, this.pictureBox1.Height, this.pictureBox1.Width, this.a);
g.DrawString("ELok", f, Brushes.Black, this.a + 10, this.pictureBox1.Height / 2 - 30);
g.DrawString(trainnumber.ToString(), f, Brushes.Black, this.a + 70, this.pictureBox1.Height / 2 - 30);
g.DrawString("1990", f, Brushes.Black, this.a + 30, this.pictureBox1.Height / 2 - 10);
g.DrawString("2000V", f, Brushes.Black, this.a + 30, this.pictureBox1.Height / 2 + 10);
}
}
private void Form1_Resize(object sender, EventArgs e)
{
this.pictureBox1.Invalidate();
}
// Die Klasse Gates
class Gates : DrawingObject
{
public void Draw(Graphics g, int high, int width)
{
g.FillRectangle(new SolidBrush(Color.Olive), width / 2 - 60, high / 2 - 45, 10, 10);
g.FillRectangle(new SolidBrush(Color.Olive), width / 2 - 60, high / 2 + 45, 10, 10);
}
public Gates(Form1 f1)
{
f1.gatev += new Form1.GateEvent(f1_gatev);
}
public void f1_gatev(Graphics g, int high, int width)
{
g.FillRectangle(new SolidBrush(Color.Green), width / 2 - 60, high / 2 - 45, 125, 10);
g.FillRectangle(new SolidBrush(Color.Green), width / 2 - 60, high / 2 + 45, 125, 10);
}
}
[/CSharp]