Reputation: 37
So I making tic tac toe. In main file (Form1.cs) I want to have only button1_click,button2_click events in which I would call function from Class1. My problem is that I'm having loop so debuger returns error SystemStackOverflowException unhandelded.
Form1.cs:
Form1 frm1;
Class1 cl;
public Form1()
{
cl = new Class1();
frm1 = this;
InitializeComponent();
}
private void button5_Click(object sender, EventArgs e) //gumb za tip1 igro
{
cl.button1funkcija(frm1);
}
Class1.cs
Form1 form = new Form1(); //its needed so i can use it in my own made functions (prehod_stan2())
public void button1funkcija (Form1 form)
{
if (form.numericUpDown2.Value != 0)
{
form.label10.Text = "Preostale igre:" + form.numericUpDown2.Value;
tip = 3;
st_iger = form.numericUpDown2.Value;
prehod_stran2();
form.errorProvider6.Clear();
stanje();
}
else //če je število iger enako 0, prikaži napako
{
form.errorProvider5.Clear();
form.errorProvider6.SetError(form.numericUpDown2, "Število more biti večje od 0");
}
}
public void prehod_stran2()
{
if (form.textBox1.Text == "")
{
form.errorProvider1.SetError(form.textBox1, "Polje mora biti izpolnjeno");
if (form.textBox2.Text == "")
form.errorProvider2.SetError(form.textBox2, "Polje mora biti izpolnjeno");
}
}
Upvotes: 0
Views: 60
Reputation: 117175
The Form1 form = new Form1();
line is where you are probably going wrong. You do not want a new instance of the form - you want to use the existing instance. You would pass the instance of the form to the Class1
constructor.
When you call the code cl = new Class1();
you need to change it to cl = new Class1(this);
to pass the reference to the form through to Class1
. Of course you need to add the right constructor method in Class1
. The passed reference then replaces the new Form1()
code in Class1
.
Upvotes: 1