I have a label in Form1 I'm trying to modify. Here's my code:
namespace asst5
{
public partial class Form1 : Form
{
Robot robot1 = new Robot();
public Form1()
{
InitializeComponent();
label2.Location = new Point(100,100);
label1.Text = label2.Location.ToString();
}
private void button7_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
label2.Text = "↑";
robot1.direction = 1;
}
private void button2_Click(object sender, EventArgs e)
{
label2.Text = "↓";
robot1.direction = 2;
}
private void east_Click(object sender, EventArgs e)
{
label2.Text = "→";
robot1.direction = 4;
}
private void west_Click(object sender, EventArgs e)
{
label2.Text = "←";
robot1.direction = 3;
}
private void button6_Click(object sender, EventArgs e)
{
robot1.speed = 1;
robot1.move();
}
private void button5_Click(object sender, EventArgs e)
{
robot1.speed = 10;
robot1.move();
}
}
public class Robot
{
Form1 frm1 = new Form1();
public int direction = 1; //1 = north, 2 = south, 3 = west, 4 = east
public int speed = 1;
public void move()
{
if (direction == 1)
{
frm1.label2.Location = new Point(frm1.label2.Location.Y - speed);
}
if (direction == 2)
{
frm1.label2.Location = new Point(frm1.label2.Location.Y + speed);
}
if (direction == 3)
{
frm1.label2.Location = new Point(frm1.label2.Location.X - speed);
}
if (direction == 4)
{
frm1.label2.Location = new Point(frm1.label2.Location.X + speed);
}
}
}
}
Form1 frm1 = new Form1(); is where the stack overflow occurs. I'm sure this isn't a proper way of doing it but when I try to otherwise it tells me 'An object reference is required for the non-static field.'
frm1.label2.Location.Y -= speed;